Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="wrapper">
  <div id="text"></div>
  <div class="buttons">
    <button id="play">Play</button>
    <button id="pause" disabled>Pause</button>
    <button id="stop" disabled>Stop</button>
  </div>
</div>

              
            
!

CSS

              
                *, *::before, *::after {
  box-sizing: border-box;
}

body {
  display: grid;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  padding: 20px;
  color: #fff;
  background-image: linear-gradient(-45deg, #f4d03f, #16a085);
  font: 1.25rem/1 'Poppins', sans-serif;
}

.wrapper {
  width: 600px;
  max-width: calc(100vw - 40px);
  padding: 30px;
  border-radius: 10px;
  background-color: #0003;
}

#text {
  display: block;
  line-height: 1.75;
}

mark {
  color: #000;
  background-color: #f4d03f;
}

.buttons {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  justify-content: center;
  gap: 10px;
  margin-top: 30px;
  padding-top: 30px;
  border-top: 1px solid #fff3;
}

button {
  padding: 5px;
  border: 1px solid #fff;
  border-radius: 999px;
  color: #fff;
  background-color: #0009;
  font-size: inherit;
  font-family: inherit;
  cursor: pointer;
  appearance: none;
}

button:disabled {
  opacity: 0.25;
  cursor: not-allowed;
}

              
            
!

JS

              
                const text = 'So let me help you remember. I\'ve made charts and graphs that should finally make it clear. I\'ve prepared a lecture on why I have to leave.';

// grab the UI elements to work with
const textEl = document.getElementById('text');
const playEl = document.getElementById('play');
const pauseEl = document.getElementById('pause');
const stopEl = document.getElementById('stop');

// add UI event handlers
playEl.addEventListener('click', play);
pauseEl.addEventListener('click', pause);
stopEl.addEventListener('click', stop);

// set text
textEl.innerHTML = text;

function play() {
  if (window.speechSynthesis.speaking) {
    // there's an unfinished utterance
    window.speechSynthesis.resume();
  } else {
    // start new utterance
    const utterance = new SpeechSynthesisUtterance(text);
    utterance.addEventListener('start', handleStart);
    utterance.addEventListener('pause', handlePause);
    utterance.addEventListener('resume', handleResume);
    utterance.addEventListener('end', handleEnd);
    utterance.addEventListener('boundary', handleBoundary);
    window.speechSynthesis.speak(utterance);
  }
}

function pause() {
  window.speechSynthesis.pause();
}

function stop() {
  window.speechSynthesis.cancel();
  
  // Safari doesn't fire the 'end' event when cancelling, so call handler manually
  handleEnd();
}

function handleStart() {
  playEl.disabled = true;
  pauseEl.disabled = false;
  stopEl.disabled = false;
}

function handlePause() {
  playEl.disabled = false;
  pauseEl.disabled = true;
  stopEl.disabled = false;
}

function handleResume() {
  playEl.disabled = true;
  pauseEl.disabled = false;
  stopEl.disabled = false;
}

function handleEnd() {
  playEl.disabled = false;
  pauseEl.disabled = true;
  stopEl.disabled = true;
  
  // reset text to remove mark
  textEl.innerHTML = text;
}

function handleBoundary(event) {
  if (event.name === 'sentence') {
    // we only care about word boundaries
    return;
  }

  const wordStart = event.charIndex;

  let wordLength = event.charLength;
  if (wordLength === undefined) {
    // Safari doesn't provide charLength, so fall back to a regex to find the current word and its length (probably misses some edge cases, but good enough for this demo)
    const match = text.substring(wordStart).match(/^[a-z\d']*/i);
    wordLength = match[0].length;
  }
  
  // wrap word in <mark> tag
  const wordEnd = wordStart + wordLength;
  const word = text.substring(wordStart, wordEnd);
  const markedText = text.substring(0, wordStart) + '<mark>' + word + '</mark>' + text.substring(wordEnd);
  textEl.innerHTML = markedText;
}

              
            
!
999px

Console