Long pages can leave readers unsure how much content remains. A scroll-driven CSS animation turns the page's scroll position into a progress bar that fills as the reader moves toward the bottom.
The animation-timeline property connects keyframes to scroll progress instead of elapsed time. With scroll(root block), the document's vertical scroll range controls a fixed bar, so scrolling upward also shrinks it without a JavaScript scroll listener.
Older browsers may ignore scroll timelines, so the indicator stays hidden unless the browser supports them. The page content remains readable without the bar, and a reduced-motion preference hides the animation rather than showing a misleading full progress bar.
Steps to create a scroll-driven CSS progress animation:
- Create a file named index.html in your text editor with the following page structure.
- index.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Scroll progress</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="scroll-progress" aria-hidden="true"></div> <main> <h1>Release notes</h1> <section><h2>Overview</h2><p>A calmer workspace for everyday tasks.</p></section> <section><h2>New features</h2><p>Find recent projects and resume your work.</p></section> <section><h2>Fixes</h2><p>Keyboard navigation now follows the page order.</p></section> <p>End of release notes.</p> </main> </body> </html>
The empty scroll-progress element is decorative. Its aria-hidden=“true” attribute keeps it out of the accessibility tree; the release notes remain ordinary readable content.
- Create styles.css in the same folder with the page layout and hidden progress bar.
- styles.css
body { margin: 0; font: 1.1rem/1.6 system-ui, sans-serif; color: #16324f; background: #f5f8fc; } main { max-width: 38rem; margin: auto; padding: 2rem; } section { min-height: 70vh; border-top: 1px solid #bcccdc; } .scroll-progress { display: none; position: fixed; inset: 0 0 auto; height: 0.5rem; background: linear-gradient(90deg, #0f766e, #2563eb); transform: scaleX(0); transform-origin: left center; z-index: 1000; }
The sections' minimum height gives the document a scroll range. A page that fits entirely in the viewport has no scroll progress to animate. The bar is fixed at the top and scales from its left edge.
- Append the animation rules at the end of styles.css.
- styles.css
@keyframes page-scroll-progress { from { transform: scaleX(0); } to { transform: scaleX(1); } } @supports (animation-timeline: scroll(root block)) { .scroll-progress { display: block; animation: page-scroll-progress 1ms linear both; animation-timeline: scroll(root block); } }
The keyframes scale the bar from empty to full. linear keeps its length proportional to scroll progress; the 1ms duration accommodates browsers that require a nonzero duration and does not set the scrolling speed. The animation-timeline declaration follows the animation shorthand because that shorthand resets the timeline. Unsupported browsers retain display: none.
Related: How to add CSS feature-query fallbacks - Append the reduced-motion override after the support query in styles.css.
- styles.css
@media (prefers-reduced-motion: reduce) { .scroll-progress { display: none; animation: none; } }
When the device requests reduced motion, the bar disappears and the release notes still scroll normally.
- Save index.html and styles.css in the same folder.
- Open index.html in a current browser that supports CSS scroll timelines.

- Scroll down until the New features section is visible.
The bar starts empty at the top. If it remains hidden while scrolling, check whether reduced motion is enabled or the browser lacks scroll-timeline support.
- Scroll to the bottom of the release notes.
Partway down, the bar fills only part of the width. Its length follows the fraction of the available scroll range, rather than which heading is visible.
- Confirm that the bar spans the viewport width above End of release notes.
The full bar marks the end of the page's scroll range; it does not depend on how long the page has been open.