FRENVersion française

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

  1. 1990sAnchor-based #section tables of contents exist from early HTML, at the top of the page.
  2. 2012Bootstrap 2 ships a Scrollspy plugin and makes automatic highlighting popular.
  3. 2017+position: sticky works everywhere, and the TOC moves to the side.
  4. 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.

Code

<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: sticky is enough to pin the TOC, no JS needed. Watch out: an ancestor with overflow: hidden or auto breaks it.
  • Scrollspy runs on IntersectionObserver. A negative bottom rootMargin (-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-top to 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> with aria-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="" means false. So toggleAttribute('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: smooth inside @media (prefers-reduced-motion: no-preference).
  • If you scroll with JS, move focus to the section too (tabindex="-1" then focus({ preventScroll: true })).
  • In a long TOC, keep the active link visible in the column: scroll the column too (scrollIntoView({ block: 'nearest' }) on the link).

Who uses it