Sticky headers

I made a little tweak to The Session today. The navigation bar across the top is “sticky” now—it doesn’t scroll with the rest of the content.

I made sure that the stickiness only kicks in if the screen is both wide and tall enough to warrant it. Vertical media queries are your friend!

But it’s not enough to just put some position: fixed CSS inside a media query. There are some knock-on effects that I needed to mitigate.

I use the space bar to paginate through long pages. It drives me nuts when sites with sticky headers don’t accommodate this. I made use of Tim Murtaugh’s sticky pagination fixer. It makes sure that page-jumping with the keyboard (using the space bar or page down) still works. I remember when I linked to this script two years ago, thinking “I bet this will come in handy one day.” Past me was right!

The other “gotcha!” with having a sticky header is making sure that in-page anchors still work. Nicolas Gallagher covers the options for this in a post called Jump links and viewport positioning. Here’s the CSS I ended up using:

:target:before {
    content: '';
    display: block;
    height: 3em;
    margin: -3em 0 0;
}

I also needed to check any of my existing JavaScript to see if I was using scrollTo anywhere, and adjust the calculations to account for the newly-sticky header.

Anyway, just a few things to consider if you’re going to make a navigational element “sticky”:

  1. Use min-height in your media query,
  2. Take care of keyboard-initiated page scrolling,
  3. Adjust the positioning of in-page links.

Have you published a response to this? :

Responses

Aaron Parecki

“The other “gotcha!” with having a sticky header is making sure that in-page anchors still work. Nicolas Gallagher covers the options for this in a post called Jump links and viewport positioning. Here’s the CSS I ended up using: :target:before { content: ”; display: block; height: 3em; margin: -3em 0 0; }”

6 Likes

# Liked by Gunnar Bittersmann on Sunday, June 26th, 2016 at 7:35pm

# Liked by Gabor Lenard on Sunday, June 26th, 2016 at 7:36pm

# Liked by Henry Zeitler on Sunday, June 26th, 2016 at 8:33pm

# Liked by MrMartineau on Sunday, June 26th, 2016 at 9:08pm

# Liked by Front-End Front on Monday, June 27th, 2016 at 7:23pm

# Liked by Emyr Thomas on Wednesday, June 29th, 2016 at 10:43pm

Related posts

Announcing Web Day Out

A one-day event all about what you can in web browsers today: Brighton, March 12th, 2026. Tickets are just £225+VAT!

Making the website for Research By The Sea

Having fun with view transitions and scroll-driven animations.

Displaying HTML web components

You might want to use `display: contents` …maybe.

Progressive disclosure defaults

If you’re going to toggle the display of content with CSS, make sure the more complex selector does the hiding, not the showing.

Schooltijd

Going back to school in Amsterdam.

Related links

Hack to the Future - Frontend - Matt Hobbs

Put the kettle on. This is a long one!

Matt takes a trip down memory lane and looks at all the frontend tools, technologies, and techniques that have come and gone over the years.

But this isn’t about nostalgia (although it does make you appreciate how far we’ve come). He’s looking at whether anything from the past is worth keeping today.

Studying past best practices and legacy systems is crucial for understanding the evolution of technology and making informed decisions today.

There’s only one technique that makes the cut:

After discussing countless legacy approaches and techniques best left in the past, you’ve finally arrived at a truly timeless and Incredibly important methodology.

Tagged with

It’s time for modern CSS to kill the SPA - Jono Alderson

SPAs were a clever solution to a temporary limitation. But that limitation no longer exists.

Use modern server rendering. Use actual pages. Animate with CSS. Preload with intent. Ship less JavaScript.

Tagged with

I’m more proud of these 128 kilobytes than anything I’ve built since | by Mike Hall | Jul, 2025 | Medium

I don’t normally link to articles on Medium—I respect you too much—and I do wish this were written on Mike Hall’s own site, but this is just too good not to share.

And don’t dismiss this as a nostalgiac case study from the past:

At no point did the constraints make the product feel compromised. Users on modern devices got a smooth experience and instant feedback, while those on older devices got fast, reliable functionality. Users on feature phones got the same core experience without the bells and whistles.

The constraints forced us to solve problems in ways we wouldn’t have considered otherwise. Without those constraints, we could have just thrown bytes at the problem, but with them every feature had to justify itself. Core functionality had to work everywhere, and without JavaScript crutches proper markup became essential.

This experience changed how I approach design problems. Constraints aren’t a straitjacket, keeping us from doing our best work; they are the foundation that makes innovation possible. When you have to work within severe limitations, you find elegant solutions that scale beyond those limitations.

Tagged with

Kelp

A UI library for people who love HTML, powered by modern CSS and Web Components.

Tagged with

Close to the metal: web design and the browser

It seems like the misguided perception of needing to use complex tools and frameworks to build a website comes from a thinking that web browsers are inherently limited. When, in fact, browsers have evolved to a tremendous degree

Tagged with

Previously on this day

10 years ago I wrote 100 word 096

Day ninety six.

11 years ago I wrote Responsive Day Out activities

Welcome to Brighton.

11 years ago I wrote The telescope in the woods

A trip to the Baldone Schmidt telescope in Latvia.

16 years ago I wrote From Boston to London

Straight from An Event Apart to @media.

19 years ago I wrote Mashing up with microformats

I’ve transcribed the text of the microformats panel I sat in on at South by Southwest.

22 years ago I wrote The ould country

I’m off to Ireland tomorrow. I have duly updated my travel page.

22 years ago I wrote Such a perfect day

Yesterday really brought home to me just what a nice town Brighton is.