A soft pill lights up behind one word at a time and the word takes the accent, like read-along captions or text to speech. Every word shares one keyframe, delayed by its --i index.
Also known as: word by word highlight, karaoke text highlight css, text to speech highlight.
<div class="txt-read-along-highlight"><span style="--i: 0">Read</span> <span style="--i: 1">along</span> <span style="--i: 2">with</span> <span style="--i: 3">me</span></div>
.txt-read-along-highlight {
--c: var(--txt-color, #3b82f6);
font-size: var(--txt-size, 32px);
font-family: system-ui, sans-serif;
font-weight: 700;
line-height: 1.2;
isolation: isolate;
white-space: nowrap;}
.txt-read-along-highlight span {
position: relative;
display: inline-block;
animation: txt-read-along-highlight-ink 3.2s ease-in-out infinite;
animation-delay: calc(var(--i) * 0.8s);
}
.txt-read-along-highlight span::before {
content: "";
position: absolute;
z-index: -1;
inset: 0.06em -0.12em;
border-radius: 0.24em;
background: color-mix(in srgb, var(--c) 22%, transparent);
opacity: 0;
transform: scale(0.85);
animation: txt-read-along-highlight-pill 3.2s ease-in-out infinite;
animation-delay: inherit;
}
/* each word owns a quarter of the loop: 0.8s of 3.2s */
@keyframes txt-read-along-highlight-pill {
0%, 30%, 100% { opacity: 0; transform: scale(0.9); }
5%, 24% { opacity: 1; transform: scale(1); }
}
@keyframes txt-read-along-highlight-ink {
0%, 30%, 100% { color: currentColor; }
5%, 24% { color: var(--c); }
}
@media (prefers-reduced-motion: reduce) {
.txt-read-along-highlight, .txt-read-along-highlight *, .txt-read-along-highlight::before, .txt-read-along-highlight::after, .txt-read-along-highlight *::before, .txt-read-along-highlight *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--txt-color on the element or a parent.
Currently previewing #3b82f6.--txt-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 40px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.