Sub topic 8
Keep the bar visible with position: sticky
This chapter adds position: sticky to the navigation bar so it stays in view while the reader scrolls. It assumes you can already write the semantic nav markup and the Flexbox layout from the earlier chapters. You should also know the CSS box model and how normal document flow works.
What sticky positioning does
A sticky element behaves like a normal, in-flow element until the reader scrolls it to a set offset from the edge of its scrolling container. At that point it stops moving and stays put. It is a hybrid of position: relative and position: fixed.
Here is the rule that makes the navigation bar stick:
nav {
position: sticky;
top: 0;
z-index: 10;
}Each declaration has a job:
position: stickyturns on the behavior. The bar keeps its place in the page layout, so content below it does not jump.top: 0sets the threshold. The bar sticks when its top edge reaches 0 pixels from the top of the viewport.z-index: 10keeps the bar above the page content. Without it, positioned elements later in the page can paint over the bar as they scroll beneath it.
Sticky differs from fixed in one important way. A fixed element is removed from the flow, so the content under it slides up and the bar covers the first lines of the page. A sticky element still occupies its space, so you do not need to add padding to the page.
A complete example
The following page has a sticky bar and enough content to scroll. Save it as an HTML file and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sticky navigation demo</title>
<style>
body {
margin: 0;
font-family: system-ui, sans-serif;
}
nav {
position: sticky;
top: 0;
z-index: 10;
}
.nav-list {
display: flex;
gap: 0.25rem;
margin: 0;
padding: 0;
list-style: none;
background: #1f2937;
}
.nav-list a {
display: block;
padding: 0.75rem 1rem;
color: #ffffff;
text-decoration: none;
}
main {
padding: 1rem;
}
section {
min-height: 60vh;
}
</style>
</head>
<body>
<header>
<h1>My site</h1>
</header>
<nav aria-label="Main">
<ul class="nav-list">
<li><a href="#one">One</a></li>
<li><a href="#two">Two</a></li>
<li><a href="#three">Three</a></li>
</ul>
</nav>
<main>
<section id="one"><h2>One</h2><p>First section.</p></section>
<section id="two"><h2>Two</h2><p>Second section.</p></section>
<section id="three"><h2>Three</h2><p>Third section.</p></section>
</main>
</body>
</html>Scroll down. The heading scrolls away, and the bar locks to the top edge once it gets there.
The bar is a direct child of body, so it can stick for the whole length of the page.
Why sticky sometimes does nothing
Most sticky problems come from a few causes. Check them in this order.
- No offset. Sticky needs at least one of
top,bottom,leftorright. Without one, the element never has a threshold and acts like a normal element. - The parent is too short. A sticky element can only move inside its parent's box. If the
navsits in a wrapper that is only as tall as the bar, there is no room to travel, and it appears not to stick. Place thenavin a tall container, such asbody. - An ancestor clips overflow. If any ancestor sets
overflow: hidden,overflow: autooroverflow: scroll, that ancestor becomes the scrolling container. The bar then sticks relative to it, not to the viewport. Remove the overflow rule or move thenavout of that ancestor. - A stretched flex or grid item. In a flex or grid parent, an item stretches to the full height of its row by default, which leaves no room to move. Add
align-self: startto the sticky item.
Side effects to plan for
A sticky bar permanently uses part of the screen. On a phone in landscape orientation, a tall bar can leave little room for content. Keep the bar's height modest, and consider making it sticky only on larger screens. The responsive chapter shows how to do that with a media query.
Anchor links also need care. When a reader follows a link such as #two, the browser scrolls the target to the very top of the viewport, which puts its heading under the sticky bar. The scroll-margin-top property fixes this by adding space above the target:
section {
scroll-margin-top: 3rem;
}Set the value to at least the height of your bar. Add it to the demo page above and click "Two" to see the heading stop below the bar.
Exercise
Your sticky nav scrolls away with the page. The nav is the only child of a div whose height is the same as the bar, and top: 0 is set. What is the most likely cause, and how do you fix it?
Show answer
The sticky element can only move within its parent's box. The div is exactly as tall as the bar, so the bar has no room to travel. Move the nav out of the div so that it is a child of a taller container such as body, or make the div as tall as the content that should scroll under the bar.
Exercise
A reader follows the link #two and the section heading is hidden behind the sticky bar. Which CSS property fixes this, on which element, and what value should it have?
Show answer
Set scroll-margin-top on the link target, here the section element. Use a value at least as large as the bar's height, for example 3rem for a bar about 48 pixels tall.