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

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.

Making the Patterns Day website

The joy of getting hands-on with HTML and CSS.

Culture and style

Styling a document about The Culture novels of Iain M Banks.

Bugblogging

Also, tipblogging.

Related links

How would you build Wordle with just HTML and CSS? | Scott Jehl, Web Designer/Developer

This is a great thought exercise in progressive enhancement …that Scott then turns into a real exercise!

Tagged with

The quiet, pervasive devaluation of frontend - Josh Collinsworth blog

It’s like CSS exists in some bizarre quantum state; somehow both too complex to use, yet too simple to take seriously, all at once.

In many ways, CSS has greater impact than any other language on a user’s experience, which often directly influences success. Why, then, is its role so belittled?

Writing CSS seems to be regarded much like taking notes in a meeting, complete with the implicit sexism and devaluation of the note taker’s importance in the room.

Tagged with

CSS :has() Interactive Guide

This isn’t just a great explanation of :has(), it’s an excellent way of understanding selectors in general. I love how the examples are interactive!

Tagged with

Retrofitting fluid typography | Clagnut by Richard Rutter

Here’s a taste of what Rich will be delivering at Patterns Day on Thursday—can’t wait!

Tagged with

Offloading JavaScript With Custom Properties: HeydonWorks

With classes, we can send CSS static values but with custom properties we can send dynamic ones, which is a major shift in the way we can style state. This is something that has been true for some time—and is extremely well supported—but sometimes it takes solving a small real-world problem to make you appreciate the value of it.

I think we still haven’t come to fully appreciate the superpower of custom properties: dynamic values that are shared between CSS and JavaScript.

Tagged with

Previously on this day

9 years ago I wrote 100 word 096

Day ninety six.

10 years ago I wrote Responsive Day Out activities

Welcome to Brighton.

10 years ago I wrote The telescope in the woods

A trip to the Baldone Schmidt telescope in Latvia.

15 years ago I wrote From Boston to London

Straight from An Event Apart to @media.

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

21 years ago I wrote The ould country

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

21 years ago I wrote Such a perfect day

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