In-page navigation
Sticky table of contents
Table des matières sticky
Demo
Loading demo…
The list of the page's headings, pinned to the side while you read, with the current section highlighted. It tells people where they are in a long page and lets them jump straight to a section. The text-free variants have their own pages: dot navigation, minimap and reading progress indicator.
Story
Did you know
The word "scrollspy" comes from a Bootstrap 2 jQuery plugin. The pattern is much older, but that plugin gave it the name every dev still types into Google.
Timeline
- 1990sAnchor-based
#sectiontables of contents exist from early HTML, at the top of the page. - 2012Bootstrap 2 ships a Scrollspy plugin and makes automatic highlighting popular.
- 2017+
position: stickyworks everywhere, and the TOC moves to the side. - 2023English Wikipedia switches to the Vector 2022 skin: the contents box leaves the top of the article for a sticky side column.
Vocabulary
Names
- FR
- Table des matières sticky, sommaire, sommaire latéral, navigation par ancres, Sur cette page
- EN
- Sticky table of contents, TOC, On this page, scrollspy, anchor navigation, in-page navigation
Common uses
- Technical docs and long articles
- Settings pages split into sections
- Terms of service, privacy policies, contracts
- Specs, RFCs and reference guides
Often paired with
The components it usually sits with, by page type, in reading order.
Blog post
See the mock-upCode
<nav aria-label="On this page" class="toc">
<a href="#install">Installation</a>
<a href="#config">Configuration</a>
<a href="#faq">FAQ</a>
</nav>
<script>
const links = document.querySelectorAll('.toc a')
const io = new IntersectionObserver((entries) => {
for (const e of entries) {
if (!e.isIntersecting) continue
for (const a of links) {
if (a.hash === '#' + e.target.id) a.setAttribute('aria-current', 'true')
else a.removeAttribute('aria-current')
}
}
}, { rootMargin: '0px 0px -70% 0px' })
document.querySelectorAll('section[id]').forEach((s) => io.observe(s))
</script>
<style>
.toc { position: sticky; top: 1rem; }
.toc a[aria-current] { font-weight: 600; }
</style>Good to know
position: stickyis enough to pin the TOC, no JS needed. Watch out: an ancestor withoverflow: hiddenorautobreaks it.- Scrollspy runs on
IntersectionObserver. A negative bottomrootMargin(-70%) activates a section once it reaches the top third. Observe whole sections rather than headings, so the highlight stays right when scrolling back up. - Add
scroll-margin-topto sections if you have a fixed header, or the anchor lands underneath it. - Below a certain width, the side column no longer fits. Collapse the TOC into a
<details>at the top of the article instead of dropping it.
Accessibility
Must have
- Wrap the TOC in a
<nav>witharia-label="On this page", so it stands apart from the main navigation. - Mark the active link with
aria-current="true"(or"location")."page"means another page, not a section. - Use real
<a href="#id">links: Tab, Enter, a shareable URL and focus moving to the target all work out of the box.
Often forgotten
aria-current=""meansfalse. SotoggleAttribute('aria-current')marks nothing: write the value"true".- The active link must not stand out by colour alone: add a side rule or a bolder weight (WCAG 1.4.1).
- Reuse the exact heading text in the links. A shortened or different label is confusing once you land on the section.
Nice to have
- Only turn on
scroll-behavior: smoothinside@media (prefers-reduced-motion: no-preference). - If you scroll with JS, move focus to the section too (
tabindex="-1"thenfocus({ preventScroll: true })). - In a long TOC, keep the active link visible in the column: scroll the column too (
scrollIntoView({ block: 'nearest' })on the link).