Sticky table of contents with scrolling active states

A documentation-style layout often pairs a content-heavy main column with a sidebar that has plenty of empty room. That space makes the sidebar a safe home for a position: sticky; table of contents, which stays in view while the reader scrolls through the sections of the main column.

Bramus Van Damme has published a tutorial that builds this pattern in stages: it starts from semantic markup, handles most of the behavior with HTML and CSS, and adds the final active-navigation touch with JavaScript.

That last touch is needed because of a case :target styling cannot cover. If the reader does not click down to a section — the one situation where :target might serve for active navigation — JavaScript must track the scroll position and highlight the active item. Van Damme uses IntersectionObserver for the job, describing it as the perfect API for this purpose.

The result resembles a similar demo from Hakim El Hattab called Progress Nav. Both share the same design pattern, but El Hattab's version draws an SVG path along the way, indenting for sub-nav. It does not use IntersectionObserver, so anyone wanting to hack on the idea could combine the two approaches.

CSS-Tricks star logo

We've got lots of ways to help you connect with the greatest front-end community in all the land.

CSS-Tricks Newsletter

Sign up to get front-end roundups, browser updates, and exclusive content sent to your inbox bi-weekly.

Ads are self-hosted