How to add a reading progress bar
Build a thin pink bar that fills across the top of the screen as someone reads your article, with a small JavaScript version and a pure CSS version for browsers that support scroll-driven animations.

Long posts are easier to settle into when you can see how much is left. In this tutorial you'll build a reading progress bar: a thin pink line fixed to the top of the screen that fills from left to right as you read. It tracks the article itself, not the whole page, so it hits 100% at the end of the post instead of at the bottom of the footer. You'll write a small JavaScript version that works everywhere, then a pure CSS version using scroll-driven animations, and let the browser pick the one it can run.
Add the markup
You only need two divs. The outer one is the track that sits across the top of the screen, and the inner one is the bar that fills up.
Put them inside your article, as the first child. That looks a bit odd for something that sits at the top of the screen, but it matters later: the CSS version can only read the article's scroll position if the bar lives inside it. The aria-hidden attribute is on purpose, and we'll get to why near the end.
<article class="post">
<div class="reading-progress" aria-hidden="true">
<div class="reading-progress__bar"></div>
</div>
<h1>Your post title</h1>
<p>Your post content goes here.</p>
</article>Style the bar
The track is fixed to the top edge, 4px tall, with a faint pink tint so you can see where the bar is headed. pointer-events: none means it never blocks a click on your header.
The bar is always full width. Instead of changing its width, you shrink it with transform: scaleX(0) and grow it back to scaleX(1). Transforms don't make the browser recalculate the page layout, so they stay smooth while you scroll. transform-origin: 0 50% pins the bar to the left edge so it grows to the right instead of from the middle.
.reading-progress {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 4px;
z-index: 1000;
pointer-events: none;
background: rgb(214 42 159 / .15);
}
.reading-progress__bar {
height: 100%;
background: #d62a9f;
transform: scaleX(0);
transform-origin: 0 50%;
}Measure progress through the article
The bar should read 0 when the top of the article reaches the top of the screen, and 1 when the bottom of the article reaches the bottom of the screen. getBoundingClientRect gives you the article's position, and the distance you can scroll through it is its height minus the window height. Divide how far the top has moved past the screen edge by that distance and you have your progress. Math.min and Math.max keep it between 0 and 1, so the header and footer don't push it out of range.
Scroll events can fire many times per frame, so the listener doesn't do the math itself. It asks for one requestAnimationFrame, and the ticking flag skips any extra requests until that frame runs. The listener is passive, which tells the browser it will never cancel scrolling. The resize listener catches a window size change, and the ResizeObserver catches the article growing when images load in.
const article = document.querySelector('.post');
const bar = document.querySelector('.reading-progress__bar');
let ticking = false;
function update() {
const rect = article.getBoundingClientRect();
const scrollable = rect.height - window.innerHeight;
let progress = scrollable > 0 ? -rect.top / scrollable : (rect.top <= 0 ? 1 : 0);
progress = Math.min(1, Math.max(0, progress));
bar.style.transform = `scaleX(${progress})`;
ticking = false;
}
function requestUpdate() {
if (!ticking) {
ticking = true;
requestAnimationFrame(update);
}
}
window.addEventListener('scroll', requestUpdate, { passive: true });
window.addEventListener('resize', requestUpdate);
new ResizeObserver(requestUpdate).observe(article);
update();Let CSS do it with scroll-driven animations
Newer browsers can link an animation to scrolling with no JavaScript at all. view-timeline on the article creates a timeline named --article that follows the article through the screen. The bar uses it with animation-timeline, and animation-range: contain 0% contain 100% maps the animation to the same stretch the JavaScript measures: article top at the screen top, to article bottom at the screen bottom. Put animation-timeline after the animation shorthand, because the shorthand resets it.
The catch is support. According to the browser data MDN publishes, animation-timeline works in Chrome and Edge 115 and up and in Safari 26, while Firefox only has it in preview builds. So everything sits inside @supports, and the JavaScript only runs when the browser says no to the same check. Every reader gets a working bar, and most skip the script.
@keyframes grow-progress {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
@supports (animation-timeline: view()) {
.post {
view-timeline: --article block;
}
.reading-progress__bar {
animation: grow-progress linear both;
animation-timeline: --article;
animation-range: contain 0% contain 100%;
}
}if (!CSS.supports('animation-timeline: view()')) {
// paste the scroll listener code from the previous step here
}Accessibility and reduced motion
You could mark the bar up as role="progressbar" with aria-valuenow, but then you'd have to update that number on every scroll, and a screen reader would have one more thing to announce that the scrollbar already tells people. This bar is decoration, so aria-hidden="true" keeps it out of the way. If you want to share reading time, a line of text like "6 min read" under the title works for everyone.
The bar moves exactly as far as the reader scrolls, so it doesn't add motion of its own. If you'd like to smooth the JavaScript version with a short transition, wrap it in a prefers-reduced-motion: no-preference media query so it only applies to people who haven't asked for less motion.
@media (prefers-reduced-motion: no-preference) {
.reading-progress__bar {
transition: transform 80ms linear;
}
}One last tip. If a parent of the bar has a transform or filter on it, position: fixed stops sticking to the screen and starts sticking to that parent instead. So if your bar scrolls away with the page, check the article's ancestors for a stray transform first.
More articles

How to make a headline type itself out
Build a headline that types itself out letter by letter with a blinking cursor, using plain HTML, CSS and a few lines of JavaScript.

How to add a copy button to code blocks
Add a small Copy button to every code block on your page that copies the code, says Copied for a moment, and tells screen reader users what happened.

How to make a confetti burst on click
A no-library tutorial for a small burst of colorful confetti that pops out from the exact spot where someone clicks a link or button.

How to build a dark mode toggle
A step-by-step tutorial for a dark mode that follows your visitor's device setting, adds a toggle button that saves their pick in localStorage, and never flashes the wrong theme on load.

How to make a glowing border spin
A pure CSS tutorial for a card with a pink, purple and blue gradient border that spins around the edge and glows softly, with a calm static version for people who prefer less motion.

How to make cards lift as you scroll on a phone
Phones can't hover, so our blog cards used to sit flat on mobile. Here's the small CSS class and IntersectionObserver we use to lift whichever card is in the middle of the screen as you scroll.

How to build a thought-bubble tooltip with pure CSS
A step-by-step CSS tutorial for a tooltip that reads its label from a data attribute, pops up in a rounded bubble, trails off in three little circles and stays clickable, with no JavaScript.

What is Hermes?
Hermes is an open source AI agent harness from Nous Research that runs on your own computer, uses tools, remembers past sessions and learns skills. Here is what it does and how to install and set it up.

Meet the Night Sky UI Kit
Three animated React components for dark, night-sky themed sites: a star cursor, a twinkling starfield and a soft aurora glow. Here is what's inside and how to add it.

Behind the Star Cursor
How the Star Cursor draws its comet tail and sparkle dust, and why it quietly steps aside on touch screens and for anyone who prefers reduced motion.

The moon is, in fact, an inanimate, dusty old rock
The moon is a layered ball of rock about 4.5 billion years old, wrapped in sharp gray dust and almost no air. Here is what it is made of, where it probably came from and why it barely changes.

How solar storms mess with satellites
The sun has weather, and some of it reaches us. Here is how solar flares and the storms that follow can sink satellites, scramble GPS and even trip power grids, and how people keep watch.

Patterns in sound
Sprinkle salt on a drum, play a steady note, and the grains slide into neat lines and rings. Here is why sound draws pictures, who first noticed, how to try it at home, and what all this has in common with a snowflake.

Most of the sound around you goes unheard
Human ears catch only a middle band of all the sound there is. Here is what we miss, which animals hear it, and why the hums and whines from our own gadgets can still get under our skin.

What is LaserSETI?
LaserSETI is a growing network of rooftop cameras that watch huge patches of the night sky for a single-color flash of light. Here is how it works, where the stations are in 2026, and what it has (and has not) found.

Most of the light around you is invisible
Our eyes catch only a thin band of all the light there is, from violet to red. Here is what we miss, which animals see more of it, and how we use the invisible rest every day.

The science of music
Why a good chorus gives you chills, why the Mozart effect fizzled, and why some songs refuse to leave your head. Here is what the research actually says about music, mood and focus.

Bill Nye is still teaching
A science show from the 1990s is still turning up in classrooms. Here is what Bill Nye the Science Guy was, why so many grownups remember it so fondly, and why teachers keep pressing play.

Why is the sky blue?
The sky is blue because the tiny bits of gas in the air reflect blue sunlight around much more than red light, so blue reaches our eyes from every part of the sky.

Why the sun and moon look the same size
The sun is about 400 times wider than the moon and also about 400 times farther away, so the two discs look almost the same size in our sky. That match is a coincidence of timing, and it will not last forever.

Bees fly more like drones than planes
A bumblebee stays in the air the way a drone or helicopter does, by moving its wings fast instead of racing forward over a stiff wing.

The earth is, in fact, actually round
You can check that Earth is round from a beach, a campsite or a phone call to a friend in another time zone. People worked it out more than two thousand years ago, and one of them even measured it with shadows.

5 trendy UI effects
A star cursor with a comet tail, frosted glass panels, scroll-driven animation, smooth page transitions and gradients that actually move, plus the one tip we would give for each.

What llms.txt is, and how we added one to our site
A plain-English look at llms.txt, the proposed markdown file that gives AI assistants a short map of your site, what it can and can't promise, and how we wrote and tested our own.

Tell Google who you are with a few lines of code
Small business names collide, and search engines have to guess which one you meant. Organization structured data is one way a site owner can stop them guessing. Here's what it is, what we put in ours, and how to test it.

Building night sky effects that know when to sit out
How the Night Sky UI Kit's star cursor, starfield and aurora check the visitor's device and settings before they draw a single frame.

How you can set up pull requests to get an automatic first review
Give every pull request an automatic first review the moment it opens, and send each new pull request and its review to a dedicated Slack channel. Here is how the pieces fit.

How visitors book a call on our site
Picking a time with us takes a few clicks and never leaves the page. Here is how the booking calendar on our site works, and the small checks that stop two people grabbing the same hour.

Inside our resumable, review-gated video production pipeline
How we built a local seven-step pipeline that turns a history topic into a captioned video, with resumable state, bounded retries, and a human review gate.