A reading progress bar in Astro can be pure CSS: a fixed bar with animation-timeline: scroll(root) grows from 0 to 100% as the page scrolls, with no JavaScript and no scroll listener. To track only the article rather than the whole page, give the article a view-timeline and use that instead. A few lines of script cover browsers without scroll-driven animations.
Key Takeaways
- CSS scroll-driven animations link an animation’s progress to scroll position.
scroll(root)tracks the whole page; a namedview-timelinetracks one element.- Animate
transform: scaleX()rather thanwidth, so the bar doesn’t trigger layout.- Wrap the CSS in
@supports (animation-timeline: scroll())and add a script fallback.
The whole-page version
---
// src/components/ReadingProgress.astro
---
<div class="reading-progress" aria-hidden="true"></div>
<style>
.reading-progress {
position: fixed;
inset: 0 0 auto 0;
z-index: 50;
height: 3px;
background: var(--color-accent, #6d28d9);
transform-origin: 0 50%;
transform: scaleX(0);
}
@supports (animation-timeline: scroll()) {
.reading-progress {
animation: reading-progress linear both;
animation-timeline: scroll(root block);
}
}
@keyframes reading-progress {
to { transform: scaleX(1); }
}
</style>
Add <ReadingProgress /> to your post layout. The animation has no duration; instead animation-timeline maps it to the root scroller, so the bar is empty at the top of the page and full at the bottom. It runs off the main thread in supporting browsers, which keeps scrolling smooth. The bar is decorative, so aria-hidden keeps it out of the accessibility tree.
Track only the article
On pages with a long footer or comments, the whole-page bar reaches 100% well after the article ends. A named view timeline fixes that:
---
// src/layouts/PostLayout.astro (excerpt)
import ReadingProgress from '../components/ReadingProgress.astro';
---
<body class="post-page">
<ReadingProgress />
<article class="post-body">
<slot />
</article>
</body>
<style is:global>
.post-page { timeline-scope: --article; }
.post-body { view-timeline: --article block; }
@supports (animation-timeline: view()) {
.post-page .reading-progress {
animation-timeline: --article;
animation-range: contain 0% contain 100%;
}
}
</style>
view-timelineon the article creates a timeline named--articlebased on its position in the viewport.timeline-scopeon a shared ancestor lets the bar, which isn’t inside the article, use that name.animation-range: contain 0% contain 100%starts when the article’s top reaches the top of the viewport and ends when its bottom reaches the bottom.
The styles are global because they connect elements from two components; scoped styles would only match one side.
A JavaScript fallback
Browsers without scroll-driven animations leave the bar at scaleX(0). Fill the gap with a throttled listener that only runs when CSS can’t do it:
<script>
if (!CSS.supports('animation-timeline: scroll()')) {
const bar = document.querySelector<HTMLElement>('.reading-progress');
const article = document.querySelector<HTMLElement>('.post-body');
let ticking = false;
const update = () => {
ticking = false;
if (!bar) return;
const el = article ?? document.documentElement;
const rect = el.getBoundingClientRect();
const total = rect.height - window.innerHeight;
const progress = total > 0 ? Math.min(Math.max(-rect.top / total, 0), 1) : 1;
bar.style.transform = `scaleX(${progress})`;
};
window.addEventListener('scroll', () => {
if (!ticking) { ticking = true; requestAnimationFrame(update); }
}, { passive: true });
update();
}
</script>
Put the script in the component. Astro bundles it once, and in browsers that support the CSS version it exits on the first line. The script tag guide covers how component scripts are processed.
Design notes
- Keep it thin (2 to 4px) and above the sticky header with a higher
z-index, or place it along the header’s bottom edge. - Animating
transformavoids layout work on every frame; animatingwidthwould recalculate layout as you scroll. - Only show it on long articles. On short pages it jumps from empty to full in one flick and adds nothing.
- Pair it with a back-to-top button or an estimated reading time for long reads.
MDN’s scroll-driven animations guide documents the timeline and range options. The component fits the post layout of any blog theme.
FAQ
Can I make a reading progress bar without JavaScript?
Yes, in browsers that support CSS scroll-driven animations. Set animation-timeline: scroll(root) on a fixed bar that animates transform: scaleX(0) to scaleX(1). Add a small script fallback for other browsers.
How do I make the progress bar track only the article?
Give the article view-timeline: --article block, set timeline-scope: --article on a common ancestor, and use animation-timeline: --article with animation-range: contain 0% contain 100% on the bar.
Does a reading progress bar hurt performance?
Not when built this way. The CSS version runs on the compositor with no script, and the fallback updates a transform at most once per animation frame. Avoid animating width or reading layout in an unthrottled scroll handler.