You all know these websites with a sticky header, right? They have a horizontal navigation at the top, often with the site logo on the left – which is also often way too big, so not too many navigation items fit beside them. And then you scroll down these pages with this large header … and the header does not scroll off the page. Many people want that for their own website, but many people using this page don’t like it as much. I’m one of them.
Issues with sticky headers
There are multiple issues with such a sticky header. While there are valid reasons to have them, most of the time they are used for the wrong reasons – like website owners always want to see their logos or important navigation items. In my opinion: if people need to use your main navigation to find things, you are doing content wrong on your site. But what are the actual issues?
Issue 1: Elements appear behind or before the sticky header
Sure, when your website is designed with a sticky header, it probably pushed the content down. But since the sticky header needs to above the content when scrolling down, it might also cover some content that you can’t interact with anymore, like a popup that has the close button in the top right and that is now behind the header. And since often with popups, you can’t scroll the page while they are open, you can’t hit that closing button.
But you could also have issues with content that appears before the header, when you scroll down the page. That happens quite often with embedded elements, like a video, PDF or map embed.
Issue 2: Anchor links don’t work anymore
When you use an anchor link to navigate to parts of your site – which is really popular on “one pager websites”. Such an anchor element scrolls the website down (or up) so that the element is at the top of the page. And then it is behind your header.
Issue 3: Too little vertical space
When those headers are really high, you end up having not too much vertical space left on the page to view the content.
Let’s take an 11″ laptop with 720px height. Not as uncommon as you might think. Then we calculate the available vertical height:
- Total: 720px
- Windows taskbar: 66px
- Chrome tabbar: 40px
- Chrome address bar: 48px
- Chrome bookmark bar: 34px
That leaves us with just 532px remaining vertical space. And now reduce this by another 60px only – that is about the current height of the header on my blog, which is really not high. This leaves you with only 472px of available space. And have we talked about those sticky cookie banners at the bottom already? That’s about the space that many of us have in landscape mode on our phones, and who really enjoys browsing a website in landscape mode?
Solutions
There are multiple solutions for the issues mentioned. Here are some that might work in your cases.
Issue 1
If a single (type of) embedded element is overlapping your header, you have to find a good z-index
value for this element and overwrite it. Or you increase the z-index
value of your header. But this can the lead to the issue with elements behind your header. You probably end up trying around with many manual z-index
changes for multiple elements and still you find some elements that don’t appear as you want them to.
Issue 2
Most websites use some CSS like this to make a header sticky and move the content down:
.fixed-header {
position: fixed;
top: 0;
width: 100%;
z-index: 999;
}
html {
margin-top: 60px;
}
Something like this would work for my header. It’s also what WordPress does, when it shows the “adminbar”. But it’s not the best solution to create a sticky header.
There is a relatively new value sticky
for the position
CSS property, and this is how the CSS code for a sticky header would look like with this new value:
.fixed-header {
position: sticky;
top: 0;
}
The great thing about it: the header does not need to be the first/top element on the page. You could have a logo, followed by the sticky header, and once that header reached the top of your browser viewport when scrolling down, it “sticks” to the top. In the past, this was only possible by toggling the position
to fixed
with JavaScript, and adding the margin-top
at the same time. But now, modern browsers can do this kind of effect automatically.
No matter how we make our header sticky, we still have the issue with anchor links not working. The margin
of 60px
will not push any element down by that value, but only the beginning of the body.
To solve this, you could use an additional CSS property, to add an offset for the scrolling to the anchor like this:
html {
scroll-padding-top: 60px;
}
This additional scroll-padding-top
should get a value of at least the height of the header. And here you see another issue with this solution: if the header height changes, e.g. when the items don’t fit into one line anymore and break into two lines, then those static values don’t work anymore. You could now add some JavaScript to dynamically update these two values, but that’s not a great solution.
Issue 3
My favorite solution for this issue: just don’t use a sticky header. It also solves all the other issues. No, seriously. Does your header really need to be sticky? Does it make your website more usable? Can people read your content better or even find their way around? Please think twice before making a header sticky, and not just “because you like it that way”.
So in case you really need to make it sticky, there are some ways to make you website more usable:
- Reduce the height of the header, once your site scrolls down – this is what many themes do, and they often also shrink the logo
- Hide the sticky header after some scrolling, and show it again, once you scroll up – many apps do that for sticky header and footer bars
- Make the header “not sticky”, if you detect a too low viewport height or if a device is in portrait mode
Conclusion
Some people love sticky headers, other hate them. You have to decide for yourself, if you want to have them. But please do one thing: don’t only test your website on your large desktop/laptop screen. Grab your smartphone and browse your website in landscape mode. Do you still think a sticky header is a good idea? Then use the techniques mentioned above to solve the usual issues with them.