Yesterday evening I was working on some documentation pages. The page layout is quite classic, as it consists of a content pane on the left and a sidebar navigation on the right. Looking for a way to make the page less dull I decided to add a few small things to it:

Smooth Scrolling when clicking internal links A Sticky Navigation, so that the sidebar navigation always stays in view A “ScrollSpy” to update the active state of the navigation

These three additions make the page more delightful, and best of all is: they’re really easy to implement! In this post I’ll lay out the details.

~

The result

First things first, here’s a recording of the end result so that you get an idea of what I’m talking about:

~

The Markup

The markup is really basic:

A main element surrounds our content div and nav .

element surrounds our content and . Each piece of content is wrapped in a section which gets an id attribute. The sidebar navigation then links to its id

<main> <div> <h1>Smooth Scrolling Sticky ScrollSpy Navigation</h1> <section id="introduction"> <h2>Introduction</h2> <p>…</p> </section> <section id="request-response"> <h2>Request & Response</h2> <p>…</p> </section> <section id="authentication"> <h2>Authentication</h2> <p>…</p> </section> … <section id="filters"> <h2>Filters</h2> <p>…</p> </section> </div> <nav class="section-nav"> <ol> <li><a href="#introduction">Introduction</a></li> <li><a href="#request-response">Request & Response</a></li> <li><a href="#authentication">Authentication</a></li> … <li class=""><a href="#filters">Filters</a></li> </ol> </nav> </main>

Sprinkle some CSS on top to lay everything out – using CSS Grid here – and you have a fully working – albeit dull – page:

See the Pen

Smooth Scrolling Sticky ScrollSpy Navigation (base layer) by Bramus (@bramus)

on CodePen.

~

1. Smooth Scrolling

Enabling smooth scrolling is really easy, it you can enable it using a single line of CSS:

html { scroll-behavior: smooth; }

😱 Yes, that’s it!

In the demo embedded below, click any of the links in the nav and see how smooth it scrolls:

See the Pen

Smooth Scrolling Sticky ScrollSpy Navigation (base layer) by Bramus (@bramus)

on CodePen.

For browsers that don’t support this you could add this JS fallback:

// Smooth scrolling for browsers that don't support CSS smooth scrolling if (window.getComputedStyle(document.documentElement).scrollBehavior !== 'smooth') { document.querySelectorAll('a[href^="#"]').forEach(internalLink => { const targetElement = document.querySelector(internalLink.getAttribute('href')); if (targetElement) { internalLink.addEventListener('click', (e) => { targetElement.scrollIntoView({ behavior: 'smooth', }); e.preventDefault(); }); } }); }

However, browser’s that don’t support scroll-behavior: smooth; also don’t support behavior: "smooth" for Element#scrollIntoView , so there’s not real advantage to adding this JS fallback.

~

2. Sticky Navigation

To make the navigation stay in place as you scroll we can rely on position: sticky; . As with Smooth Scrolling, this is a really simple CSS addition:

main > nav { position: sticky; top: 2rem; align-self: start; }

💁‍♂️ Since we’re using CSS Grid to lay out the children of <main> , adding align-self: start; to <nav> is an important one here. If we would omit it, the nav element would be as high as the enclosing main element. If that were the case, then nav would never be able to stick.

In the demo embedded below, click any of the links in the nav and see how the nav now also stays in view while the rest of the page scrolls:

See the Pen

Smooth Scrolling Sticky ScrollSpy Navigation (base layer + smooth scrolling + sticky nav) by Bramus (@bramus)

on CodePen.

~

👋 Like what you see so far? Follow @bramusblog on Twitter or subscribe to the RSS feed to stay up-to-date.

~

3. ScrollSpy with IntersectionObserver

Thanks to the almighty IntersectionObserver we can implement a ScrollSpy. Basically we use it to watch all section["id"] elements. If they are intersecting, we add the .active class to any link that links to it. For styling purposes we don’t add .active to the link itself, but to its surrounding li element.

In code, that becomes this little snippet:

window.addEventListener('DOMContentLoaded', () => { const observer = new IntersectionObserver(entries => { entries.forEach(entry => { const id = entry.target.getAttribute('id'); if (entry.intersectionRatio > 0) { document.querySelector(`nav li a[href="#${id}"]`).parentElement.classList.add('active'); } else { document.querySelector(`nav li a[href="#${id}"]`).parentElement.classList.remove('active'); } }); }); // Track all sections that have an `id` applied document.querySelectorAll('section[id]').forEach((section) => { observer.observe(section); }); });

💡 To make the transition to and from .active not too abrupt, add a little blob of CSS to ease things: .section-nav a { transition: all 100ms ease-in-out; }

~

Complete Demo

Putting everything together, we end up with this:

See the Pen

Smooth Scrolling Sticky ScrollSpy Navigation by Bramus (@bramus)

on CodePen.

Delightful, no? 😊

~

💡 If you’re also looking for more inspiration to make your interfaces more delightful, be sure to check Hakim El Hattab’s “Building Better Interfaces” talk. Recommended stuff!