Animating an h1 with Scroll-Driven CSS

Scroll-driven animations (SDA) tie an element's movement and transformation to the user's scroll position. One experiment built on that idea animates the text of a main heading, swapping it for a new heading as the page scrolls. The effect requires a static, semantic fallback wherever SDA isn't supported, and it is disabled when the browser has reduced motion enabled or the page is accessed with assistive technology, so the full content stays fully semantic and accessible.

The approach relies on a few "magic numbers" for the keyframes. While they're surprisingly responsive, the method is best suited for static content and isn't ideal for highly dynamic sites.

The text-swapping mechanism

The text animation traces back to an earlier attempt at a typewriter effect. Most methods at the time animated the element's width, required a monospace font, or depended on a solid background color — none of which fit. The workable alternative was pseudo-elements: their content property can be changed dynamically, so the only thing that changes is the text itself, with no other tricks required.

Markup and accessibility baseline

Start with semantic markup: a main element wrapping individual sections, each with its own heading and content. The example uses four sections — text and images about Primary Colors — with only basic styling: a changed font, adjusted text and heading sizes, display for the main and sections, and image sizes fixed with object-fit. At this point the content is static, semantic and accessible, and the goal is to keep it that way.

The effect needs a second heading at the top of main to hold the animated text. This one is marked aria-hidden="true" so screen readers ignore it, and the other headings get a screen-reader-only class, .srOnly, so sighted users see only the animated heading while assistive technology users get the regular static headings. (The .srOnly style is based on Scott O'Hara's "Inclusively Hidden".)

<h1 class="scrollDrivenHeading" aria-hidden="true">Primary Colors</h1>

Feature detection and stickiness

The animated heading starts as display: none. A new @supports block checks for SDA support and, inside it, restores the heading's display and applies .srOnly — so both live only where the effect is active. Browsers without SDA support get the static version.

.scrollDrivenHeading {
  display: none;
}

@supports (animation-timeline: scroll()) {
  .scrollDrivenHeading {
    display: block;
  }
  
  /* Screen Readers Only */
  .srOnly {
    clip: rect(0 0 0 0); 
    clip-path: inset(50%);
    height: 1px;
    overflow: hidden;
    position: absolute;
    white-space: nowrap; 
    width: 1px;
  }
}

For stickiness, set the heading to position: sticky with top: 0. Add a background so the text doesn't blend into the page behind it, padding for spacing, and white-space: nowrap to keep it on one line.

/* inside the @supports block */
.scrollDrivenHeading {
  display: block;
  position: sticky;
  top: 0;
  background-image: linear-gradient(0deg, transparent, black 1em);
  padding: 0.5em 0.25em;
  white-space: nowrap;
}

Finding the Switch Points

With SDA, scrolling is the timeline, so the exact change points must be determined. A small diagnostic script animates a custom property, --scroll-position, from 0 to 100 based on scroll progress on the same timeline, and displays the value in its content — a small red square in the top-left corner showing the current scroll position as a percentage. Scroll to any section, read the percentage, and mark where each heading should begin.

@property --scroll-position {
  syntax: "<number>";
  inherits: false;
  initial-value: 0;
}

body::after {
  counter-reset: sp var(--scroll-position);
  content: counter(sp) "%";
  position: fixed;
  top: 0;
  left: 0;
  padding: 1em;
  background-color: maroon;
  animation: scrollPosition steps(100);
  animation-timeline: scroll();
}

@keyframes scrollPosition {
  0% { --scroll-position: 0; }
  100% { --scroll-position: 100; }
}

Through trial and error, this method produced switch points at 30%, 60% and 90%.

Keyframes and discrete timing

Empty the .scrollDrivenHeading element so it's ready for dynamic content, then add a pseudo-element with empty content, an animation-name, and animation-timeline: scroll(). Because content is a discrete type, it doesn't transition smoothly between values — it jumps. Setting animation-timing-function: step-end forces each change to land exactly at the defined keyframe instead of somewhere in between.

.scrollDrivenHeading {
  /* style */

  &::after {
    content: '';
    animation-name: headingContent;
    animation-timing-function: step-end;
    animation-timeline: scroll();
  }
}

The first frame (0%) holds the first heading; the other headings take the percentages found earlier.

@keyframes headingContent {
  0% { content: 'Primary Colors'}
  30% { content: 'Red Power'}
  60% { content: 'Blue Calm'}
  90%, 100% { content: 'Yellow Joy'}
}

That yields a sticky heading that updates on scroll — but it switches instantly. Since no JavaScript or string manipulation is involved, the intermediate frames must be written by hand, building backwards from each target heading. The example between the first and second heading steps back 1% at a time, removing or adding a letter as needed:

@keyframes headingContent {
  0% { content: 'Primary Colors'}
  
  9% { content: 'Primary Color'}
  10% { content: 'Primary Colo'}
  11% { content: 'Primary Col'}
  12% { content: 'Primary Co'}
  13% { content: 'Primary C'}
  14% { content: 'Primary '}
  15% { content: 'Primary'}
  16% { content: 'Primar'}
  17% { content: 'Prima'}
  18% { content: 'Prim'}
  19% { content: 'Pri'}
  20% { content: 'Pr'}
  21% { content: 'P'}
  
  22% { content: 'R'}
  23% { content: 'Re'}
  24% { content: 'Red'}
  25% { content: 'Red '}
  26% { content: 'Red P'}
  27% { content: 'Red Po'}
  28%{ content: 'Red Pow'}
  29% { content: 'Red Powe'}
  
  30% { content: 'Red Power'}
  60% { content: 'Blue Calm'}
  90%, 100% { content: 'Yellow Joy'}
}

The step size is adjustable: longer headings with more words will likely call for smaller steps. Repeating the process for every heading produces the full animation.

Reduced motion

Though not a strict WCAG requirement for this kind of animation, honoring prefers-reduced-motion can matter a great deal for people with vestibular sensitivities. For a non-animated alternative, wrap the @supports block in a prefers-reduced-motion query:

@media screen and (prefers-reduced-motion: no-preference) {
  @supports (animation-timeline: scroll()) {
    /* style */
  }
}

Variations

The whole heading doesn't have to be animated. Keeping the text "Primary Color" fixed and putting the animated text in a span after it lets additional animations enhance the effect — for instance, animating the span's color to match each value.

<h1 class="scrollDrivenHeading" aria-hidden="true">
  Primary Color<span></span>
</h1>

Alternatively, keep the text animation on the span and add a second scroll-driven animation on the heading itself to change its background color. Any number of animations can be layered, each changing something different.