Scrollspy Table of Contents
A table of contents that highlights the section you are reading and scrolls to a section when clicked.
AI prompt
Clean documentation page layout with a narrow sticky table of contents on the left titled On this page, four short entries where the second is highlighted in bold dark text with a thin vertical bar on the left, and a long article on the right with section headings Overview, Setup, Usage and FAQ, light theme, flat modern style, plenty of white space. Variants: the first and last entries active. Negative: nested deep lists, gradients, heavy borders, text errors, clutter. Reduced motion: static layout.About this pattern
Definition
Scrollspy keeps a table of contents in sync with the reader. As the page scrolls, the entry for the section nearest the top is highlighted, so people always know where they are in a long document and can jump elsewhere with one click.
Purpose & UX principles
- Highlight the last section whose top has passed a line a little below the top of the view.
- Clicking an entry scrolls smoothly and moves focus to the section heading.
- Keep the list sticky so it stays visible; mark the active link with aria-current.
Interaction states
- A scrollable document with four sections and a contents list that follows it.
Motion specification
- The marker slides between entries over 200ms; clicking scrolls smoothly.
- Reduced motion: no slide and the jump is instant.
Responsive behaviour
- On narrow screens collapse the list into a menu or a horizontal strip.
- Do not highlight two entries at once.
Code
<div class="bx"><div class="dc"><nav class="tc" aria-label="On this page"><b>On this page</b><ul id="tc"></ul></nav><div class="ar" id="ar"></div></div></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}
.dc{display:grid;grid-template-columns:96px 1fr;gap:10px;height:200px;border:1px solid var(--line);border-radius:12px;background:var(--surface);overflow:hidden}
.tc{padding:10px 0 10px 10px;font-size:11.5px}.tc b{display:block;font-size:11px;color:var(--muted);margin-bottom:6px}.tc ul{list-style:none;border-left:1px solid var(--line)}.tc a{display:block;padding:4px 8px;margin-left:-1px;border-left:2px solid transparent;color:var(--muted);transition:color .2s,border-color .2s}.tc a[aria-current=true]{color:#171717;font-weight:600;border-left-color:#171717}
.ar{overflow-y:auto;padding:10px 12px 10px 0;font-size:12px;line-height:1.5;color:#444;scroll-behavior:smooth}.ar h4{font-size:14px;color:#171717;margin:0 0 4px;scroll-margin-top:8px}.ar section{min-height:170px;padding-bottom:8px}.ar section:last-child{min-height:210px}
@media (prefers-reduced-motion:reduce){.ar{scroll-behavior:auto}.tc a{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const S=['Overview','Setup','Usage','FAQ'],tc=document.getElementById('tc'),ar=document.getElementById('ar');
ar.innerHTML=S.map((s,i)=>'<section id="s'+i+'"><h4 tabindex="-1">'+s+'</h4><p>This section explains '+s.toLowerCase()+' in a few sentences. Scroll the document and watch the contents list follow along, so the reader always knows where they are.</p><p>Short paragraphs keep each section easy to scan and give the page enough length to scroll.</p></section>').join('');
tc.innerHTML=S.map((s,i)=>'<li><a href="#s'+i+'" data-i="'+i+'">'+s+'</a></li>').join('');
const secs=[...ar.querySelectorAll('section')],links=[...tc.querySelectorAll('a')];
function spy(){let a=0;const line=ar.getBoundingClientRect().top+30;secs.forEach((s,i)=>{if(s.getBoundingClientRect().top<=line)a=i;});if(ar.scrollTop+ar.clientHeight>=ar.scrollHeight-2)a=secs.length-1;links.forEach((l,i)=>l.setAttribute('aria-current',String(i===a)));}
ar.addEventListener('scroll',spy);tc.addEventListener('click',e=>{const l=e.target.closest('a');if(!l)return;e.preventDefault();const s=secs[+l.dataset.i];ar.scrollTo({top:s.offsetTop-ar.offsetTop,behavior:matchMedia('(prefers-reduced-motion:reduce)').matches?'auto':'smooth'});s.querySelector('h4').focus({preventScroll:true});});spy();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>Scrollspy Table of Contents — 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}
.dc{display:grid;grid-template-columns:96px 1fr;gap:10px;height:200px;border:1px solid var(--line);border-radius:12px;background:var(--surface);overflow:hidden}
.tc{padding:10px 0 10px 10px;font-size:11.5px}.tc b{display:block;font-size:11px;color:var(--muted);margin-bottom:6px}.tc ul{list-style:none;border-left:1px solid var(--line)}.tc a{display:block;padding:4px 8px;margin-left:-1px;border-left:2px solid transparent;color:var(--muted);transition:color .2s,border-color .2s}.tc a[aria-current=true]{color:#171717;font-weight:600;border-left-color:#171717}
.ar{overflow-y:auto;padding:10px 12px 10px 0;font-size:12px;line-height:1.5;color:#444;scroll-behavior:smooth}.ar h4{font-size:14px;color:#171717;margin:0 0 4px;scroll-margin-top:8px}.ar section{min-height:170px;padding-bottom:8px}.ar section:last-child{min-height:210px}
@media (prefers-reduced-motion:reduce){.ar{scroll-behavior:auto}.tc a{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="dc"><nav class="tc" aria-label="On this page"><b>On this page</b><ul id="tc"></ul></nav><div class="ar" id="ar"></div></div></div>
</div>
<script>
const S=['Overview','Setup','Usage','FAQ'],tc=document.getElementById('tc'),ar=document.getElementById('ar');
ar.innerHTML=S.map((s,i)=>'<section id="s'+i+'"><h4 tabindex="-1">'+s+'</h4><p>This section explains '+s.toLowerCase()+' in a few sentences. Scroll the document and watch the contents list follow along, so the reader always knows where they are.</p><p>Short paragraphs keep each section easy to scan and give the page enough length to scroll.</p></section>').join('');
tc.innerHTML=S.map((s,i)=>'<li><a href="#s'+i+'" data-i="'+i+'">'+s+'</a></li>').join('');
const secs=[...ar.querySelectorAll('section')],links=[...tc.querySelectorAll('a')];
function spy(){let a=0;const line=ar.getBoundingClientRect().top+30;secs.forEach((s,i)=>{if(s.getBoundingClientRect().top<=line)a=i;});if(ar.scrollTop+ar.clientHeight>=ar.scrollHeight-2)a=secs.length-1;links.forEach((l,i)=>l.setAttribute('aria-current',String(i===a)));}
ar.addEventListener('scroll',spy);tc.addEventListener('click',e=>{const l=e.target.closest('a');if(!l)return;e.preventDefault();const s=secs[+l.dataset.i];ar.scrollTo({top:s.offsetTop-ar.offsetTop,behavior:matchMedia('(prefers-reduced-motion:reduce)').matches?'auto':'smooth'});s.querySelector('h4').focus({preventScroll:true});});spy();
</script>
</body></html>