Sub topic 8
Keep the Bar Visible with position: sticky
A navigation bar that scrolls out of view forces readers to scroll back to the top to go anywhere else. This chapter makes the bar stay in view with position: sticky. It also covers why the declaration sometimes seems to do nothing, and what a sticky bar changes for the rest of the page.
This chapter assumes you have the earlier chapters' navigation markup (nav, ul, li, a) and the Flexbox row layout. It also assumes you know the box model and basic CSS selectors. You do not need JavaScript for anything here.
What sticky positioning does
A sticky element behaves like a normal element until the reader scrolls it to a threshold you choose. Then it stops moving and stays at that threshold. It is a hybrid of two other values:
- It starts like
position: relative: it takes up space in the normal flow, so the content below it does not jump. - It then acts like
position: fixedafter the threshold is crossed.
Here is the whole technique, applied to the navigation bar from earlier chapters:
nav {
position: sticky;
top: 0;
z-index: 10;
}Each of the three lines has a job:
position: stickyturns on the behavior.top: 0sets the threshold: the bar stays 0 pixels from the top edge of the scrolling area.z-index: 10keeps the bar above page content that is positioned or that creates its own stacking layer.
Comparing the position values
Sticky is easiest to understand next to its neighbors.
| Value | Takes space in flow | Moves when the page scrolls | Positioned relative to |
|---|---|---|---|
| `static` (default) | Yes | Yes | Not positioned |
| `relative` | Yes | Yes | Its own normal position |
| `absolute` | No | Yes | Nearest positioned ancestor |
| `fixed` | No | No | The viewport |
| `sticky` | Yes | Until the threshold, then no | Nearest scrolling ancestor, limited by its parent |
The practical difference from fixed is the space. A fixed bar is removed from the flow, so the content underneath slides up behind it and you must add top padding to compensate. A sticky bar keeps its place in the flow, so nothing needs compensating.
How the browser decides where the bar goes
The browser tracks two boxes for a sticky element:
- The scrollport. This is the visible area of the nearest ancestor that scrolls. Usually that is the viewport.
- The containing block. This is the sticky element's parent.
The element sticks when its edge would cross the offset you set inside the scrollport. It can never leave its parent's box. When the reader scrolls past the bottom of the parent, the element is pushed out of view along with it.
That second rule causes most sticky bugs. The next section lists them.
Why sticky sometimes does nothing
When a sticky bar scrolls away like any other element, one of these is almost always the cause.
No offset is set
position: sticky with no top, bottom, left or right value never sticks. The offset is the threshold, and without it there is nothing to compare against. Add top: 0.
The parent is only as tall as the bar
If the nav sits alone inside a header, the header is exactly as tall as the nav. The bar has no room to travel inside its parent, so it never appears to stick. Put the nav where its parent spans the page, for example as a direct child of body, or make the sticky element the header itself.
<!-- Does not stick: the header is only as tall as the nav -->
<header>
<nav aria-label="Main">…</nav>
</header>
<main>…</main>
<!-- Sticks: the parent (body) contains all the page content -->
<nav aria-label="Main">…</nav>
<main>…</main>An ancestor creates its own scroll container
Any ancestor with overflow set to hidden, auto, scroll or clip can become the scroll container that the bar sticks within. If that ancestor does not itself scroll, the bar looks like it has no effect. Check every ancestor up to html. A common source is a wrapper with overflow-x: hidden, added to hide a horizontal scrollbar.
The bar has no background
A sticky bar overlaps the content that scrolls beneath it. If the bar's background is transparent, text shows through the links. The earlier chapters gave the list a background color (#1f2937). Keep that background on the sticky element or on the list inside it.
Other elements cover the bar
A sticky element joins the stacking order like any positioned element. Later positioned elements, such as images with position: relative, can paint over it unless it has a z-index. A modest value such as 10 is enough. Higher numbers do not make it more reliable, and they make later layering harder to manage.
Anchor links and the hidden heading
A sticky bar causes a side effect with in-page links. When a reader follows a link such as #pricing, the browser scrolls the target to the very top of the viewport. The sticky bar then covers the first lines of the target.
Fix this on the scrolling element with scroll-padding-top, set to at least the bar's height:
html {
scroll-padding-top: 3.5rem;
}Alternatively, set scroll-margin-top on the targets themselves, for example section[id] { scroll-margin-top: 3.5rem; }. Use scroll-padding-top when every target needs the same offset, because it is a single rule. The same offset also applies when keyboard users tab to a link near the top of the view.
The bar's height now appears in two places: its own styles and the scroll offset. The next chapter, on custom properties, shows how to define the height once.
A complete runnable page
This page combines the markup and styles from the earlier chapters with the sticky behavior. Save it as an .html file and open it in a browser. Scroll down and the bar stays at the top. Click "Details" in the bar and the heading lands below the bar, not under it.
<!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>
html {
scroll-padding-top: 3.5rem;
}
body {
margin: 0;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
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;
}
.nav-list a:hover,
.nav-list a:focus-visible {
background: #374151;
}
.nav-list a:focus-visible {
outline: 2px solid #38bdf8;
outline-offset: -2px;
}
main {
max-width: 40rem;
margin: 0 auto;
padding: 1rem;
}
section {
min-height: 60vh;
}
</style>
</head>
<body>
<nav aria-label="Main">
<ul class="nav-list">
<li><a href="#intro">Intro</a></li>
<li><a href="#details">Details</a></li>
<li><a href="#summary">Summary</a></li>
</ul>
</nav>
<main>
<section id="intro">
<h1>Intro</h1>
<p>Scroll down. The bar stays at the top of the window.</p>
</section>
<section id="details">
<h2>Details</h2>
<p>This heading is not hidden by the bar after you follow the link.</p>
</section>
<section id="summary">
<h2>Summary</h2>
<p>The bar occupies normal space, so no top padding is needed on the body.</p>
</section>
</main>
</body>
</html>Edge cases to plan for
A tall bar on small screens
When the mobile menu from a later chapter opens, the stacked links can be taller than a short phone screen in landscape. A sticky element taller than the viewport hides part of the menu with no way to scroll to it. Limit the open list's height and let it scroll on its own:
.nav-list.is-open {
max-height: calc(100vh - 3rem);
overflow-y: auto;
}This overflow is safe. It is set on the list inside the bar, not on an ancestor of the bar.
Dropdowns inside a sticky bar
A sticky bar with a z-index creates its own stacking context. Everything inside it, including an absolutely positioned submenu, is layered together as one unit. The dropdown appears above page content as long as the bar does. Avoid putting overflow: hidden on the bar, because it would clip the submenu.
Sticky and Flexbox parents
If the sticky element is a flex item in a column, its default align-self: stretch makes it as tall as the row. A stretched item has no room to move, so it never sticks. Set align-self: flex-start on it. This matters for sticky sidebars more than for top bars, but it is the same mechanism.
The bar takes screen space
A sticky bar uses vertical space on every screen, which hurts on small landscape phones. Keep it compact, usually 48 to 64 pixels tall. Consider turning stickiness off for very short viewports:
@media (max-height: 30rem) {
nav {
position: static;
}
}Choosing between sticky and fixed
Prefer sticky when the bar sits in the normal flow at the top of the page. It needs no spacing workarounds and it respects its container. Choose fixed only when the element must stay in place regardless of its position in the document, such as a floating chat button. For a navigation bar, sticky is almost always the better fit.
Exercise
A developer wraps the navigation in <header class="site-header"><nav>…</nav></header> and sets nav { position: sticky; top: 0; }. The page has main after the header with plenty of content. The bar scrolls away with the page. Why, and what are two ways to fix it?
Show answer
The nav is positioned inside its parent, the header, and a sticky element cannot leave its parent's box. The header is only as tall as the nav, so the nav has no room to travel. Fix 1: apply the sticky rules to .site-header instead of nav, so the header is the element that moves within body. Fix 2: move the nav out of the header so that its parent is body, which contains all the page content.
Exercise
A page with a sticky bar 3.5rem tall has a link to #pricing. After you click the link, the first line of the Pricing heading is hidden under the bar. Write the one CSS rule that fixes this for every in-page link on the page.
Show answer
html { scroll-padding-top: 3.5rem; } The scroll-padding-top property on the scrolling element tells the browser to leave that much space at the top when it scrolls a target into view. A value at or slightly above the bar height reveals the whole heading.
Exercise
You add overflow-x: hidden to a wrapper div that contains the sticky nav, and the nav stops sticking. Explain why, and name one way to keep the horizontal overflow hidden without breaking the bar.
Show answer
A non-visible overflow value makes the wrapper a scroll container. The nav then sticks relative to the wrapper's scrollport, and the wrapper does not scroll, so the nav appears not to stick. You can move the nav outside the wrapper. You can also use overflow-x: clip on the wrapper, which hides the overflow without creating a scroll container. Browsers older than about 2022 do not support clip, so test the browsers you target.