CSS Text Highlight Animation

A marker sweep that draws behind a phrase, staggered so each highlight lands after the last.


CSS Text Highlight Animation


A text highlight animation draws a marker stroke behind a few words, left to right, the way a pen would. It is the effect people want when a sentence has one phrase that should survive skimming. It is also easy to wreck: highlight everything and you have highlighted nothing.

The mark is a real <mark> element. The yellow is a pseudo-element, not background-color, because a background cannot sweep. The pseudo-element starts at scaleX(0) with its origin on the left and animates to scaleX(1). The text color does not change. The words were already readable. The color arrives behind them.

This is quieter than the split text reveal and the scroll-triggered text reveal. Those change the words’ visibility. A marker assumes the sentence is already on the page.

The sweep

mark { background: none; color: inherit; position: relative; }
mark::after {
  content: "";
  position: absolute;
  z-index: -1;
  left: -0.08em;
  right: -0.08em;
  bottom: 0.08em;
  height: 0.42em;
  background: var(--ink, #fde047);
  transform: scaleX(0);
  transform-origin: left center;
  animation: sweep .7s cubic-bezier(.2,.7,.2,1) forwards;
  animation-delay: var(--d, .2s);
}
@keyframes sweep { to { transform: scaleX(1); } }

height: 0.42em sits on the lower half of the line, which is where a real marker lands. A full-line background looks like a selection, not a pen. z-index: -1 puts the ink behind the glyphs. The parent of the article needs a background, or the negative z-index can slip behind the page. The demo’s article does not create a stacking context that hides it, and the body background is the paper. If your highlight disappears, add isolation: isolate on the paragraph and keep the mark’s z-index inside that context, or drop the negative index and paint the mark with a translucent ink that does not cover the letterforms.

--d staggers multiple marks. 150ms, then 450ms, then 800ms is enough that they read as a sequence. --ink changes the pen without a new class: yellow for the first claim, green for a result, pink for a warning.

HTML

<h1>Highlight the <mark style="--d:.15s">phrase</mark> a reader should keep.</h1>
<p>A marker is a <mark style="--ink:#bbf7d0; --d:.45s">sweep of color</mark> behind the words.</p>

<mark> has a default yellow background and a meaning: this text is marked. Resetting background and color keeps that meaning and replaces the paint. Do not use a span with no role if you want the highlight to mean “this part matters”. The element already says that.

One mark per sentence is the working limit. Three marks in a paragraph is the demo being a demo. On a product page, mark the offer, the constraint, or the name. Not all three.

When to play it

Play it when the paragraph enters the viewport if the block is below the fold. The demo plays on load because the paragraph is the page. A highlight that finished before the reader arrived is a static marker, which is also fine. You do not need to replay it.

Under reduced motion, drop the animation and leave transform: none so the ink is simply there. The mark still marks.

Questions about highlight animations

How do I animate a text highlighter in CSS?

Wrap the phrase in <mark>, remove the default background, and scale a pseudo-element from 0 to 1 on the X axis with the origin at the left. Keep the text color as it was.

Why is my highlight covering the letters?

The pseudo-element is painting on top of the glyphs. Give it z-index: -1 and make sure a parent background does not hide it. Or use a translucent marker color and leave it behind the text in document order by placing the ink under the line rather than across the whole em box.

Can each phrase have a different marker color?

Yes. Set --ink on the mark. The sweep uses background: var(--ink, #fde047), so a missing variable falls back to yellow.