CSS components

Sticky Table of Contents Generator

Turn your article headings into a sticky sidebar that highlights the section you are reading. Indented lines become sub-sections, and on phones the table collapses into a tap-to-open box.

Live preview

Runs entirely in your browser. Nothing is uploaded to any server.

Why add a sticky table of contents

Easy navigation

Readers jump straight to the section they need.

Know where you are

Scroll-spy highlights the section on screen.

Better long reads

Docs, guides and blog posts feel shorter and clearer.

How to build a sticky TOC

  1. List your headingsOne heading per line; start a line with - to make it a sub-section.
  2. Set the layoutChoose the side, width, sticky offset and active style.
  3. Copy the codeReplace the sample paragraphs with your article and keep the heading ids.

How scroll-spy works here

The sidebar uses position: sticky inside a two-column grid, so it scrolls with the page until it reaches the top offset and then stays put. If the list is taller than the screen, it scrolls on its own.

On every scroll frame the script finds the last heading that has passed the top offset and marks its link with aria-current="true", which also drives the highlight style. At the very bottom of the page the last section is always selected, even if its heading never reaches the top.

Heading offset

scroll-margin-top keeps headings visible below sticky headers.

Mobile

Below the breakpoint the TOC becomes a collapsible box above the article.

Progress bar

A thin bar at the top shows how far through the page you are.

Sticky table of contents FAQ

Why is my sidebar not sticky?

position: sticky stops working when a parent has overflow hidden or auto. Check the containers above the layout.

Can I use it with my existing article?

Yes. Keep the aside and the script, give each heading in your article an id, and point the links at those ids.

How do I account for a fixed site header?

Increase the sticky top offset to the height of your header. The script and scroll-margin-top use the same value.

Does it work without JavaScript?

The links, smooth scrolling and sticky sidebar work with CSS only. Only the highlight and the progress bar need the script.

How deep can the nesting go?

This generator supports two levels, which covers h2 and h3 headings in most articles.

Try other CSS component tools

More free tools from My Panda Toolbox.