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

Previously on this day

4 years ago I wrote 100 word 096

Day ninety six.

5 years ago I wrote Responsive Day Out activities

Welcome to Brighton.

5 years ago I wrote The telescope in the woods

A trip to the Baldone Schmidt telescope in Latvia.

10 years ago I wrote From Boston to London

Straight from An Event Apart to @media.

13 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.

16 years ago I wrote The ould country

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

16 years ago I wrote Such a perfect day

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