speech-driven emphasis
When a browser reads text aloud with speech synthesis, nothing on the page shows which word is being spoken. speechType listens to the Web Speech API’s boundary events and marks each word as it is spoken: by default with weight, optical size and spacing on a plain span around each word, or, with the opt-in highlight mode available in 1.2.0, by painting a range without adding word wrappers. The text stays real text either way: links keep their names, and the accessibility tree keeps the paragraph you wrote.
Live demo — step with the Word slider or press Speak if supported
What splitting text costs — measured on this page
speechType’s span mode wraps each word so it can be styled separately. The DOM-based movement techniques compared here use independently styled pieces too. Until version 1.1.0, speechType also hid its spans from screen readers, a mistake several of our tools made. Below, compare one sentence as written, split by a published library running in your browser, and wrapped in place. Each version reports its measured width and the structure it offers assistive technology.
How it works
The audio-text gap
When the browser speaks text via SpeechSynthesisUtterance, words are heard but not seen. There is no CSS property that tracks synthesis progress — the visual and the auditory are decoupled by design. speechType closes that gap.
Boundary events drive emphasis
The Web Speech API fires boundary events as each word begins. speechType maps the reported character index to the word and applies typographic emphasis (wider tracking, heavier weight, larger optical size) while dimming the other words. Not every voice sends word boundaries; with one that doesn't, the text is spoken and nothing is marked.
Imperative for performance
During active speech, startSpeechType handles each boundary event directly — updating span styles without touching React state or triggering re-renders. The boundary event fires, the span style changes, and the frame is painted. No scheduling, no batching delay.
React or vanilla JS
SpeechTypeText and useSpeechType manage span preparation and React-driven emphasis. startSpeechType is the vanilla JS entry point — it wraps, speaks, and syncs, returning a stop function.
Usage
TypeScript + React · Vanilla JS
Drop-in component
import { SpeechTypeText, startSpeechType } from '@overpunch/speechtype'
import { useRef, useState } from 'react'
// Drive activeWordIndex via startSpeechType or your own state
const [activeWordIndex, setActiveWordIndex] = useState(-1)
const ref = useRef<HTMLParagraphElement>(null)
// startSpeechType updates the spans imperatively during speech:
<SpeechTypeText ref={ref} activeWordIndex={activeWordIndex}>
The voice knows where it is. The page does not.
</SpeechTypeText>Start speech synthesis and sync emphasis
import { startSpeechType } from '@overpunch/speechtype'
const el = document.querySelector('p')
const stop = startSpeechType(el, { rate: 0.9, activeWeight: 700 })
// Later — cancel speech and restore styles:
// stop()Hook — manual control with your own active index
import { useSpeechType } from '@overpunch/speechtype'
import { useRef } from 'react'
// Inside a React component:
const ref = useRef(null)
useSpeechType(ref, activeWordIndex, { inactiveOpacity: 0.4 })
return <p ref={ref}>Every word spoken carries its own weight.</p>Options
| Option | Default | Description |
|---|---|---|
| activeTracking | 0.06 | Letter-spacing on the active word in em. |
| activeWeight | +300 | wght axis value on the active word. Unset: the word's own weight plus 300 (400 becomes 700). |
| activeOpsz | ×1.5 | opsz axis value on the active word. Unset: 1.5 times the word's own optical size. |
| inactiveOpacity | 0.6 | Opacity of words not currently spoken. The published 1.2.0 default is 0.6. Dimming lowers contrast while a word is active and can still fail WCAG AA on your palette; check the actual composited colours. Set to 1 for no dimming. |
| transitionMs | 80 | CSS transition duration in ms for style changes. |
| mode | 'spans' | Available in 1.2.0. 'spans' remains the default; opt into 'highlight' to paint the spoken word without wrappers using ::highlight(speechtype). The API supports restricted painting properties, including colour, background, text decoration and text shadow where supported, not font weight, font size or transforms. Falls back to spans where the API is missing. |
| rate | 0.9 | Speech rate (0.1–10). Accepted by all paths; speech options (rate, pitch, volume) are silently ignored by SpeechTypeText and useSpeechType. |
| pitch | 1 | Speech pitch (0–2). Silently ignored by SpeechTypeText and useSpeechType. |
| volume | 1 | Speech volume (0–1). Silently ignored by SpeechTypeText and useSpeechType. |
| onUnsupported | — | Called when the browser does not support SpeechSynthesis. |
| onError | — | Called with the SpeechSynthesisErrorEvent if synthesis errors. |
| as | 'p' | HTML element to render. (SpeechTypeText only) |
Limits
- We have not tested speechType with a screen reader. What we checked is Chrome's accessibility tree, where names and roles match the unwrapped text. A span per word does split the text into more pieces in that tree, and a 2018 report found VoiceOver on iOS stopping at inline elements; the highlight mode avoids spans altogether.
- The default emphasis makes the spoken word heavier and wider, so lines can rewrap while it plays. The highlight mode can't move text, and can't change weight or size either.
- Dimming the other words lowers their contrast for as long as a word is active. The published 1.2.0 default is 0.6, an accessibility-motivated change that can still fail WCAG AA on particular foreground/background colours. Check the actual composited contrast: the demo above shows the ratio for this page. Set
inactiveOpacityto 1 for no dimming. - Speech synthesis, its voices and its word-boundary events come from the browser and the operating system, and differ between them.
no-code
Use it in Webflow
The same effect, no build step — drop it straight into your design tool.
Webflow
One script tag, then mark any element with data-speechtype. Configure it with data-* attributes.
<!-- Site Settings → Custom Code → Footer, or an Embed element -->
<script src="https://cdn.jsdelivr.net/npm/@overpunch/speechtype/dist/speechtype.webflow.min.js"></script>
<!-- Then add data-speechtype to any text element -->
<h1 data-speechtype>Your headline</h1>