Fixed and sticky
Overview
Some elements should stay visible while the page scrolls: a site header, a “back to top” button, a table of contents, a cookie banner. CSS has two positioning values for this — fixed and sticky — and they behave quite differently.
position: fixed
A fixed element is removed from the flow and positioned relative to the viewport. It stays in the same place on screen no matter how far you scroll:
.back-to-top {
position: fixed;
right: 1.5rem;
bottom: 1.5rem;
}
.cookie-banner {
position: fixed;
inset-inline: 0;
bottom: 0;
}Because fixed elements leave the flow, they cover whatever is underneath. A fixed header needs matching padding on the body, or the top of the page hides beneath it:
.site-header { position: fixed; top: 0; inset-inline: 0; height: 4rem; }
body { padding-top: 4rem; } /* keep content out from under the header */That coupling is fragile — if the header’s height changes (longer text, larger font), the padding is wrong. For headers, sticky is usually better.
Gotcha: like absolute positioning,
fixedis relative to an ancestor withtransform,filterorcontaininstead of the viewport. A “fixed” element inside an animated, transformed container scrolls with it.
position: sticky
A sticky element is a hybrid. It stays in the normal flow (taking up its space), scrolls normally — and when scrolling would move it past its offset, it sticks there:
.site-header {
position: sticky;
top: 0;
z-index: 10;
}Because the header still occupies its space, the content below never hides under it — no body padding, no magic numbers.
The rules of sticky
- It needs an offset.
top,bottom,leftorright(or their logical versions) — without one, sticky behaves likerelative. - It sticks within its parent. Once the parent’s bottom edge scrolls past, the sticky element goes with it. That’s perfect for section headings in a long list — each heading sticks until its section ends — but it means a sticky element whose parent is the same height as itself never visibly sticks.
- It sticks relative to the nearest scroll container. If any ancestor between the sticky element and the page has
overflow: hidden,autoorscroll, the element sticks inside that ancestor instead. If that ancestor doesn’t actually scroll, the element appears not to stick at all. (overflow: clipdoesn’t create a scroll container, so it’s safe.)
Sticky in grid and flex layouts
Grid and flex items stretch to fill their row by default. A sidebar stretched to the full height of the article has nowhere to move, so it can’t stick. Stop it stretching:
.sidebar {
position: sticky;
top: 5rem;
align-self: start;
}Sticky headers and anchor links
When a link jumps to #section-2, the browser scrolls the heading to the very top — underneath your sticky header. Leave room for it:
h2, h3 { scroll-margin-top: 5rem; }
/* or for everything: */
html { scroll-padding-top: 5rem; }Debugging “sticky doesn’t work”
Work through this checklist:
- Does it have
top(or another offset)? - Is its parent taller than it? If the parent is exactly its height, there’s nowhere to stick.
- Is it a grid/flex item being stretched? Add
align-self: start. - Does any ancestor have
overflow: hidden/auto? Change it toclip, or move the sticky element out. - Is something covering it? Add a
z-indexand a background colour.
Layering and backgrounds
Sticky and fixed elements usually sit above scrolling content, so give them:
- a background colour — a transparent header lets text show through,
- a
z-index— positioned content later in the page would otherwise paint over them (see Stacking and z-index).
Mobile considerations
- Keep sticky headers short. On a small phone in landscape, a 100px header plus browser toolbars can leave almost no room for content.
- Consider hiding the header when scrolling down and showing it when scrolling up (this needs a little JavaScript).
- Fixed bottom bars must leave room for the device’s home indicator:
padding-bottom: env(safe-area-inset-bottom)(withviewport-fit=coverin the viewport meta tag).
Default to sticky for headers and sidebars: it keeps elements in the flow, so nothing needs to compensate for them. Use
fixedfor things that truly float above the page, like a chat button.
A sticky header and table of contents
Build a page with a site header that stays at the top while scrolling, and an article layout with a sidebar table of contents (.toc) that sticks 1rem below the header while the article scrolls. Make sure in-page links (#section-2) don’t scroll their headings underneath the sticky header. Assume the header is 4rem tall.
<header class="site-header">Academy</header>
<div class="layout">
<nav class="toc">
<a href="#section-1">Section 1</a>
<a href="#section-2">Section 2</a>
</nav>
<article>
<h2 id="section-1">Section 1</h2>
<p style="height: 120vh">…</p>
<h2 id="section-2">Section 2</h2>
<p style="height: 120vh">…</p>
</article>
</div>
<style>
* { box-sizing: border-box; }
body { margin: 0; }
.site-header { height: 4rem; background: #1b1d21; color: white; padding: 1rem; }
.layout { display: grid; grid-template-columns: 12rem 1fr; gap: 2rem; padding: 1rem; }
.toc { display: flex; flex-direction: column; gap: 0.5rem; }
/* your code here */
</style><header class="site-header">Academy</header>
<div class="layout">
<nav class="toc">
<a href="#section-1">Section 1</a>
<a href="#section-2">Section 2</a>
</nav>
<article>
<h2 id="section-1">Section 1</h2>
<p style="height: 120vh">…</p>
<h2 id="section-2">Section 2</h2>
<p style="height: 120vh">…</p>
</article>
</div>
<style>
* { box-sizing: border-box; }
body { margin: 0; }
.site-header { height: 4rem; background: #1b1d21; color: white; padding: 1rem; }
.layout { display: grid; grid-template-columns: 12rem 1fr; gap: 2rem; padding: 1rem; }
.toc { display: flex; flex-direction: column; gap: 0.5rem; }
.site-header {
position: sticky;
top: 0;
z-index: 10;
}
.toc {
position: sticky;
top: 5rem; /* 4rem header + 1rem space */
align-self: start; /* otherwise the grid stretches it to full height */
}
h2 {
scroll-margin-top: 5rem; /* in-page links stop below the header */
}
</style>Summary
position: fixedremoves an element from the flow and pins it to the viewport.position: stickykeeps the element in the flow and sticks it once scrolling reaches itstop(or other) offset.- A sticky element can only stick within its parent, and relative to its nearest scrolling ancestor.
- Sticky fails silently when an ancestor has
overflow: hidden, when no offset is set, or when the parent is no taller than the element. - Use
scroll-margin-topso anchor links don’t hide headings under sticky headers.