Tevpro insights

Scroll-Driven Animations With CSS

Build and test a CSS scroll-driven reading-progress bar with reduced-motion handling and a small fallback.

Technology

Key takeaways

  • animation-timeline: scroll(root block) binds animation progress to document scrolling instead of elapsed time.
  • Set animation-timeline after the animation shorthand; otherwise the shorthand can reset the timeline.
  • The example supplies a JavaScript fallback and disables decorative motion for reduced-motion preferences.

A reading-progress bar should advance when the reader scrolls, not when a timer elapses. CSS scroll timelines connect animation progress to document position.

1. Start with a decorative bar

Progress element
html
<div class="track" aria-hidden="true">
  <div class="progress"></div>
</div>

The bar is decorative, so it is hidden from screen readers. It should not be the only way to navigate the article.

2. Give it a scroll timeline

Scroll-driven progress
css
.track { position: fixed; inset: 0 0 auto; height: .45rem; }
.progress {
  height: 100%;
  background: #0d9fbd;
  transform: scaleX(0);
  transform-origin: left center;
}
@supports (animation-timeline: scroll()) {
  @media (prefers-reduced-motion: no-preference) {
    .progress {
      animation: fill-progress linear both;
      animation-timeline: scroll(root block);
      animation-range: 0% 100%;
    }
    @keyframes fill-progress { to { transform: scaleX(1); } }
  }
}

scroll(root block) uses the document’s vertical scroll position as the clock. animation-range maps the full scroll range to the bar. Put animation-timeline after the animation shorthand because the shorthand resets timeline-related properties.

3. Degrade without hiding the article

Without scroll-timeline support, the bar remains at its baseline unless you add a small scroll listener to update its transform. The article itself stays fully readable. If you add that fallback, disable the decorative update when prefers-reduced-motion: reduce is requested. Test short pages, changing article height, zoom, and viewport resizing.

Try scrolling inside the preview below. The bar at the top follows the document in supporting browsers. Edit styles.css to change the bar or timeline, and compare the result with reduced-motion settings.

Interactive example

Try a scroll-driven reading progress bar

Scroll inside the preview to move the bar, then edit styles.css. In browsers without scroll-timeline support, the article remains readable and the bar stays at its baseline.

For browser compatibility and the full specification, see the primary references in Sources below.

Sources

Written by Keith Kikta

Why work with us

Why Tevpro?

Whether you’re a startup with a bold product idea or an established company seeking a stronger delivery partner, Tevpro delivers results. Our expert consultants specialize in building secure, scalable applications that simplify operations and drive real ROI.

FAQ

Frequently asked questions

No. It connects animation progress to scrolling. Stopping the scroll stops progress, and scrolling upward moves the progress indicator backward.