Reading Progress Bar
A thin bar at the top of an article that fills as you read, plus a back-to-top button.
AI prompt
Build a scrolling article card 300x200px (overflow auto) with six short paragraphs and a 3px progress bar pinned to the top inside the card (position sticky, top 0, full width, dark fill). Update the bar on the container's scroll event with requestAnimationFrame: scale = scrollTop / (scrollHeight - clientHeight) applied as transform scaleX with transform-origin left and no transition. When progress passes 30 percent show a 40px circular "Back to top" button in the bottom right corner (opacity and translateY(8px) over 200ms); pressing it scrolls the container to the top with smooth behaviour. The bar has aria-hidden true; the button has aria-label "Back to top" and a visible focus ring. Show the percentage as small text only if useful. Under prefers-reduced-motion drop the button rise and use instant scrolling.About this pattern
Definition
A bar fixed to the top of a scrolling area whose width follows how far the reader has scrolled. A back-to-top button appears once the reader is well into the content.
Purpose & UX principles
- Shows how much is left in long articles and documentation, which makes people more willing to continue.
- Decorative for sighted readers: it carries no unique information, so it is hidden from assistive technology; the button is a real labelled control.
- Scroll-linked effects should be cheap: only a transform changes.
Interaction states
- At top (bar empty, button hidden), reading (bar partly filled), near the end (button visible), focus-visible on the button.
Motion specification
- The bar scales with
transform: scaleXon every scroll frame (no transition) so it tracks exactly. - The button fades and rises 8px in 200ms.
- Reduced motion: the bar still tracks (it is not motion), the button appears without the rise and scrolling to the top is instant.
Responsive behaviour
- The bar is 3px tall on all sizes; on touch devices the back-to-top button is 44px.
- Works for the page or any scroll container.
Code
<div class="wrap"><div class="sc" id="sc"><div class="bar" id="bar" aria-hidden="true"></div><div class="tx" id="tx"></div></div><button class="up" id="up" aria-label="Back to top" tabindex="-1">↑</button></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.wrap{position:relative;width:min(300px,100%)}
.sc{height:200px;overflow:auto;border:1px solid var(--line);border-radius:14px;background:var(--surface);scrollbar-width:thin}
.bar{position:sticky;top:0;z-index:2;height:3px;background:#171717;transform:scaleX(0);transform-origin:left}
.tx{padding:14px 16px 20px}
.tx h3{font-size:15px;font-weight:600;margin-bottom:8px}
.tx p{font-size:13px;line-height:1.6;color:#3d3d3d;margin-bottom:10px}
.up{position:absolute;right:10px;bottom:10px;width:40px;height:40px;border-radius:50%;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:16px;cursor:pointer;opacity:0;transform:translateY(8px);pointer-events:none;transition:opacity .2s,transform .2s}
.up.on{opacity:1;transform:none;pointer-events:auto}
.up:hover{border-color:var(--accent)}
.up:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.up{transition:none;transform:none}}const sc=document.getElementById('sc'),bar=document.getElementById('bar'),up=document.getElementById('up'),tx=document.getElementById('tx');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
tx.innerHTML='<h3>Why details matter</h3>'+['Small interactions are where an interface earns trust. A button that answers instantly feels reliable.','Progress cues remove uncertainty. Readers continue when they know how much is left.','Motion should explain, not decorate. Every movement answers a question: where did that come from, where did it go.','Accessible patterns are not extra work. They are the same patterns, built with the right element in the first place.','Test with the keyboard, with a screen reader and with reduced motion on. Most problems appear within minutes.','Ship the smallest version that works, then refine the details people actually touch.'].map(t=>'<p>'+t+'</p>').join('');
let q=0;function upd(){q=0;const m=sc.scrollHeight-sc.clientHeight,p=m>0?sc.scrollTop/m:0;bar.style.transform='scaleX('+p.toFixed(4)+')';const on=p>.3;up.classList.toggle('on',on);up.tabIndex=on?0:-1;}
sc.addEventListener('scroll',()=>{if(!q)q=requestAnimationFrame(upd);},{passive:true});upd();
up.addEventListener('click',()=>sc.scrollTo({top:0,behavior:reduce?'auto':'smooth'}));One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Reading Progress Bar — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.wrap{position:relative;width:min(300px,100%)}
.sc{height:200px;overflow:auto;border:1px solid var(--line);border-radius:14px;background:var(--surface);scrollbar-width:thin}
.bar{position:sticky;top:0;z-index:2;height:3px;background:#171717;transform:scaleX(0);transform-origin:left}
.tx{padding:14px 16px 20px}
.tx h3{font-size:15px;font-weight:600;margin-bottom:8px}
.tx p{font-size:13px;line-height:1.6;color:#3d3d3d;margin-bottom:10px}
.up{position:absolute;right:10px;bottom:10px;width:40px;height:40px;border-radius:50%;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:16px;cursor:pointer;opacity:0;transform:translateY(8px);pointer-events:none;transition:opacity .2s,transform .2s}
.up.on{opacity:1;transform:none;pointer-events:auto}
.up:hover{border-color:var(--accent)}
.up:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.up{transition:none;transform:none}}
</style></head>
<body><div class="stage">
<div class="wrap"><div class="sc" id="sc"><div class="bar" id="bar" aria-hidden="true"></div><div class="tx" id="tx"></div></div><button class="up" id="up" aria-label="Back to top" tabindex="-1">↑</button></div>
</div>
<script>
const sc=document.getElementById('sc'),bar=document.getElementById('bar'),up=document.getElementById('up'),tx=document.getElementById('tx');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
tx.innerHTML='<h3>Why details matter</h3>'+['Small interactions are where an interface earns trust. A button that answers instantly feels reliable.','Progress cues remove uncertainty. Readers continue when they know how much is left.','Motion should explain, not decorate. Every movement answers a question: where did that come from, where did it go.','Accessible patterns are not extra work. They are the same patterns, built with the right element in the first place.','Test with the keyboard, with a screen reader and with reduced motion on. Most problems appear within minutes.','Ship the smallest version that works, then refine the details people actually touch.'].map(t=>'<p>'+t+'</p>').join('');
let q=0;function upd(){q=0;const m=sc.scrollHeight-sc.clientHeight,p=m>0?sc.scrollTop/m:0;bar.style.transform='scaleX('+p.toFixed(4)+')';const on=p>.3;up.classList.toggle('on',on);up.tabIndex=on?0:-1;}
sc.addEventListener('scroll',()=>{if(!q)q=requestAnimationFrame(upd);},{passive:true});upd();
up.addEventListener('click',()=>sc.scrollTo({top:0,behavior:reduce?'auto':'smooth'}));
</script>
</body></html>