Back to Top Button
A round button appears after you scroll, shows how far you are with a progress ring, and returns you to the top.
AI prompt
Clean UI mockup of a long article page with a small round white button in the lower right corner showing an upward arrow and a thin blue progress ring around its edge filled to about two thirds, a soft shadow beneath, the article text slightly faded behind, light theme, flat modern style, generous spacing. Variants: the button hidden at the top of the page, and a pill version with the words Back to top. Negative: heavy shadows, clutter, text errors, device frames. Reduced motion: static design.About this pattern
Definition
On a long page a back-to-top button saves a long scroll. It stays hidden near the top, appears after the reader has scrolled a screen or so, and often includes a ring showing progress through the page. Activating it scrolls to the top and moves focus there.
Purpose & UX principles
- Show it after about one viewport of scrolling; hide it near the top.
- Place it in a corner, away from other floating actions and the cookie bar.
- A progress ring is optional but helpful on long reads.
- Move focus to the top of the page after activating it.
Interaction states
- A long scrolling article with the floating button.
Motion specification
- The button fades and slides in over 200ms; the scroll is smooth.
- Reduced motion: no fade and an instant jump.
Responsive behaviour
- Label it Back to top for screen readers; an arrow icon alone is not enough.
- Use instant scrolling when reduced motion is on.
Code
<div class="bx"><div class="sc" id="sc" tabindex="0" aria-label="Article"><div id="ar"></div><button class="bt" id="bt" type="button" aria-label="Back to top" hidden><svg viewBox="0 0 40 40" aria-hidden="true"><circle cx="20" cy="20" r="17" fill="none" stroke="#e7e7e4" stroke-width="3"/><circle id="rg" cx="20" cy="20" r="17" fill="none" stroke="#2b59d6" stroke-width="3" stroke-dasharray="106.8" stroke-dashoffset="106.8" transform="rotate(-90 20 20)"/><path d="M14 22l6-6 6 6" fill="none" stroke="#171717" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"/></svg></button></div><p class="out" id="out" role="status"></p></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}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.sc{position:relative;height:170px;overflow-y:auto;border:1px solid var(--line);border-radius:12px;background:var(--surface);padding:10px 12px}.sc p{font-size:12px;line-height:1.55;margin-bottom:10px}.sc h4{font-size:13px;margin:6px 0 4px}.bt{position:sticky;float:right;bottom:6px;width:40px;height:40px;border-radius:50%;background:#fff;box-shadow:0 3px 10px rgba(0,0,0,.18);transition:opacity .2s,transform .2s;margin-top:-40px}.bt svg{width:100%;height:100%}.bt[hidden]{display:none}.bt.in{opacity:1;transform:none}.bt.pre{opacity:0;transform:translateY(8px)}@media (prefers-reduced-motion:reduce){.bt{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const sc=document.getElementById('sc'),bt=document.getElementById('bt'),ar=document.getElementById('ar'),rg=document.getElementById('rg'),calm=matchMedia('(prefers-reduced-motion:reduce)').matches;
ar.innerHTML=Array.from({length:8},(_,i)=>'<h4>Section '+(i+1)+'</h4><p>Long articles invite people to scroll, and returning to the start by hand takes a while. A small control in the corner makes that trip a single action.</p>').join('');
let shown=false;function up(){const m=sc.scrollHeight-sc.clientHeight,r=m?sc.scrollTop/m:0,want=sc.scrollTop>70;rg.setAttribute('stroke-dashoffset',(106.8*(1-r)).toFixed(1));if(want&&!shown){shown=true;bt.hidden=false;bt.className='bt pre';void bt.offsetWidth;bt.className='bt in';}else if(!want&&shown){shown=false;bt.hidden=true;}document.getElementById('out').textContent=want?'Scrolled '+Math.round(r*100)+'% of the article.':'Scroll down to reveal the button.';}
sc.addEventListener('scroll',up);bt.addEventListener('click',()=>{sc.scrollTo({top:0,behavior:calm?'auto':'smooth'});sc.focus({preventScroll:true});});up();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>Back to Top Button — 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}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.sc{position:relative;height:170px;overflow-y:auto;border:1px solid var(--line);border-radius:12px;background:var(--surface);padding:10px 12px}.sc p{font-size:12px;line-height:1.55;margin-bottom:10px}.sc h4{font-size:13px;margin:6px 0 4px}.bt{position:sticky;float:right;bottom:6px;width:40px;height:40px;border-radius:50%;background:#fff;box-shadow:0 3px 10px rgba(0,0,0,.18);transition:opacity .2s,transform .2s;margin-top:-40px}.bt svg{width:100%;height:100%}.bt[hidden]{display:none}.bt.in{opacity:1;transform:none}.bt.pre{opacity:0;transform:translateY(8px)}@media (prefers-reduced-motion:reduce){.bt{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="sc" id="sc" tabindex="0" aria-label="Article"><div id="ar"></div><button class="bt" id="bt" type="button" aria-label="Back to top" hidden><svg viewBox="0 0 40 40" aria-hidden="true"><circle cx="20" cy="20" r="17" fill="none" stroke="#e7e7e4" stroke-width="3"/><circle id="rg" cx="20" cy="20" r="17" fill="none" stroke="#2b59d6" stroke-width="3" stroke-dasharray="106.8" stroke-dashoffset="106.8" transform="rotate(-90 20 20)"/><path d="M14 22l6-6 6 6" fill="none" stroke="#171717" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"/></svg></button></div><p class="out" id="out" role="status"></p></div>
</div>
<script>
const sc=document.getElementById('sc'),bt=document.getElementById('bt'),ar=document.getElementById('ar'),rg=document.getElementById('rg'),calm=matchMedia('(prefers-reduced-motion:reduce)').matches;
ar.innerHTML=Array.from({length:8},(_,i)=>'<h4>Section '+(i+1)+'</h4><p>Long articles invite people to scroll, and returning to the start by hand takes a while. A small control in the corner makes that trip a single action.</p>').join('');
let shown=false;function up(){const m=sc.scrollHeight-sc.clientHeight,r=m?sc.scrollTop/m:0,want=sc.scrollTop>70;rg.setAttribute('stroke-dashoffset',(106.8*(1-r)).toFixed(1));if(want&&!shown){shown=true;bt.hidden=false;bt.className='bt pre';void bt.offsetWidth;bt.className='bt in';}else if(!want&&shown){shown=false;bt.hidden=true;}document.getElementById('out').textContent=want?'Scrolled '+Math.round(r*100)+'% of the article.':'Scroll down to reveal the button.';}
sc.addEventListener('scroll',up);bt.addEventListener('click',()=>{sc.scrollTo({top:0,behavior:calm?'auto':'smooth'});sc.focus({preventScroll:true});});up();
</script>
</body></html>