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

Speaking at Web Day Out

Have you got the perfect talk for this event? Let me know!

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.

Related links

CSS-in-JS: The Great Betrayal of Frontend Sanity - The New Stack

This is a spot-on analysis of how CSS-in-JS failed to deliver on any of its promises:

CSS-in-JS was born out of good intentions — modularity, predictability and componentization. But what we got was complexity disguised as progress.

Tagged with

The only frontend stack we should talk about

Explore the platform. Challenge yourself to discover what the modern web can do natively. Pure HTML, CSS, and a bit of vanilla JS…

Tagged with

Custom Asidenotes – Eric’s Archived Thoughts

An excellent example of an HTML web component from Eric:

Extend HTML to do things automatically!

He layers on the functionality and styling, considering potential gotchas at every stage. This is resilient web design in action.

Tagged with

Write Code That Runs in the Browser, or Write Code the Browser Runs - Jim Nielsen’s Blog

So instead of asking yourself, “How can I write code that does what I want?” Consider asking yourself, “Can I write code that ties together things the browser already does to accomplish what I want (or close enough to it)?”

Tagged with

Interop Feature Ranking

This is a nifty initiative:

This site lets you rank the proposals you care about, giving us data we can use when reviewing which proposals should be taken on for 2026.

For the record, here’s my top ten:

  1. Cross-document view transitions
  2. Speculation Rules API
  3. img sizes="auto" loading="lazy"
  4. Customizable/stylable select
  5. Invoker commands
  6. Interoperable rendering of HTML fieldset/legend
  7. Web Share API
  8. CSS scroll-driven animations
  9. CSS accent-color property
  10. CSS hanging-punctuation property

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.