Fixed and Sticky Positioning: Headers, Modals and Sticky Sections
position: fixed for elements that stay on screen and what traps them inside a container; position: sticky for headers, table heads and sidebars, why sticky often fails, and keeping stuck content from hiding focus and anchors.
kvi5hnuCode Sprout team member9 min readOn this page
Two positioning schemes keep elements on screen while the page scrolls. fixed pins an element to the viewport: it never moves, whatever scrolls. sticky is a hybrid: the element scrolls normally until it reaches an edge you choose, then sticks there while its section is on screen. Sticky headers, table headers, section titles and sidebars all use sticky; floating buttons, cookie banners and toasts use fixed.
position: fixed
A fixed element is removed from normal flow, like an absolutely positioned one (article 56), but its containing block is the viewport. It doesn't scroll with the page:
.back-to-top {
position: fixed;
inset-block-end: 1rem;
inset-inline-end: 1rem;
}
Everything from article 56 applies: offsets, inset, stretching between opposite offsets, shrink-to-fit sizing, blockification and place-self. A full-screen layer is position: fixed; inset: 0.
What traps fixed elements
Some properties on an ancestor make it the containing block for fixed descendants, so they're fixed relative to that ancestor and scroll with it (article 44):
transform,translate,rotate,scale(other thannone), andperspectivefilterandbackdrop-filtercontain: paint,layout,strictorcontentwill-changenaming any of these
This is the most common reason a "fixed" header or modal suddenly scrolls away: a wrapper got a transform for an animation. Move the fixed element out of that wrapper, or, for dialogs and popovers, use the top layer (article 60), which no ancestor can trap.

The transform and filter in the last two do nothing visible, yet they make the wrapper the header's containing block.
Fixed elements on phones
Mobile browsers' toolbars shrink and grow as you scroll, changing the viewport's height. A fixed bottom bar follows the visible viewport, but anything sized with 100vh doesn't. Use the dynamic viewport units (article 25): block-size: 100dvh, or inset: 0 instead of a height. On phones with a notch or home indicator, keep fixed bars clear of them with env(safe-area-inset-bottom), after adding viewport-fit=cover to the viewport meta tag.
When the on-screen keyboard opens, it usually covers fixed bottom bars rather than resizing the layout. The interactive-widget key in the viewport meta tag can change that, in Chrome for Android, Firefox for Android and Safari 27.3 on iOS.
position: sticky
.section-title {
position: sticky;
inset-block-start: 0; /* stick 0px from the top of the scrolling area */
background: white; /* opaque, so content scrolls behind it, not through it */
}
A sticky element is in flow and takes its normal space. While scrolling, it acts like relative until its edge would pass the offset you set, then it sticks there. It stops sticking when the end of its parent reaches it, and scrolls away with the parent. So a section title sticks while its section is on screen, then the next section's title pushes it out.
Three things decide how sticky behaves:
- An offset is required. Without
top,bottom,leftorright(or their logical forms), a sticky element never sticks.bottom: 0makes it stick to the bottom edge instead, for example a "Save" bar at the end of a long form. - It sticks within its nearest scroll container: the closest ancestor with
overflowother thanvisibleorclip, or the viewport if there's none. - It can only move within its parent: its parent's content box is the limit.
body { font: 0.9rem system-ui, sans-serif; margin: 1rem; }
.list { block-size: 14rem; overflow: auto; border: 1px solid #d8cfc0; border-radius: 8px; max-inline-size: 18rem; }
h3 {
position: sticky;
inset-block-start: 0;
margin: 0;
padding: 0.4rem 0.75rem;
background: #8a3b12; color: white; font-size: 0.85rem;
}
p { margin: 0; padding: 0.5rem 0.75rem; border-bottom: 1px solid #eee5d6; }
.fab {
position: fixed;
inset-block-end: 1rem; inset-inline-end: 1rem;
font: inherit; border: 0; border-radius: 99px; padding: 0.5rem 0.9rem;
background: #2f7d4f; color: white;
}
Scroll the list: each heading sticks to the top until its section ends, then the next one replaces it. The button is fixed to the preview's viewport, which is the frame it runs in.

The red outlines are the limits: a heading moves only inside its own section, so bottom: 0 on a heading at the top of its section never has room to act.
Why sticky doesn't work
When position: sticky seems to do nothing, check these, in order:
| Cause | Fix |
|---|---|
| no offset | add inset-block-start: 0 (or top: 0) |
an ancestor has overflow: hidden, auto or scroll, so the element sticks within that box, which may not scroll at all | remove it, or use overflow: clip, which doesn't create a scroll container (article 43) |
| the parent is no taller than the sticky element, so there's no room to move | put the sticky element in a taller parent |
| it's a stretched flex or grid item, as tall as its row, so there's nowhere to stick | add align-self: start |
it's inside a table row (<tr>) in some older browsers | make the cells (<th>) sticky instead |
The grid case is common with sidebars: a grid item stretches to the full height of the row by default. In Chrome, a position: sticky; top: 0 sidebar in a stretched grid cell scrolls away; with align-self: start it sticks.

Only the second and third panels differ from the working ones by a single declaration: the missing offset, and the default stretch alignment.
.layout { display: grid; grid-template-columns: 16rem 1fr; align-items: start; }
.layout .sidebar { position: sticky; inset-block-start: 1rem; }
Sticky table headers
Table header cells can stick, which keeps column titles visible in long tables:
.table-wrap { max-block-size: 70vh; overflow: auto; }
thead th { position: sticky; inset-block-start: 0; background: white; }
tbody th { position: sticky; inset-inline-start: 0; background: white; } /* a sticky first column */
With border-collapse: collapse, a shared border belongs to two cells, so sticky cells can lose their borders while scrolling; use border-collapse: separate with border-spacing: 0 if that matters.
Stuck content and accessibility
A sticky or fixed header covers the top of the page. That causes two problems:
- Anchors: clicking a
#sectionlink scrolls the heading to the top of the viewport, underneath the header. - Focus: tabbing to a link near the top can hide it completely under the header, which fails WCAG 2.4.11, "focus not obscured" (article 42).
Both are fixed with scroll-padding on the scroll container, the viewport's being the <html> element:
html { scroll-padding-block-start: 4.5rem; } /* the header's height, plus a little */
scroll-margin-block-start on the targets does the same for individual elements. Also keep sticky and fixed bars small, especially on phones, where they can take a third of the screen; consider a header that hides on scroll down and returns on scroll up. And a fixed element should never cover content permanently: give the page enough bottom padding to scroll past a fixed footer.
What's current
position: stickyworks everywhere, including on table cells.- Dynamic viewport units (
dvh,svh,lvh) andenv(safe-area-inset-*)are Baseline. - Scroll-state container queries (article 79) can style a sticky element while it's stuck, such as adding a shadow, in Chrome.
- The top layer (article 60) has replaced
position: fixedfor modal dialogs and popovers.
Common questions
Why does my fixed header scroll with the page?
An ancestor has transform, filter, contain: paint or will-change for one of them, which makes it the containing block. Move the header out of that ancestor.
Why doesn't position: sticky work?
Most often: no offset, an ancestor with overflow: hidden or auto, or a stretched flex or grid item. See the table above.
What's the difference between fixed and sticky?
fixed is removed from flow and stays on screen permanently. sticky stays in flow and only sticks while its parent is on screen.
Why do anchor links scroll headings under my sticky header?
Set scroll-padding-block-start on html to the header's height.
Can a sticky element stick to the bottom?
Yes: position: sticky; inset-block-end: 0. It sticks to the bottom edge until its parent's start scrolls past.
Summary
fixedpositions an element against the viewport and removes it from flow.transform,filter,containand similar properties on an ancestor trap it.- Size full-height fixed layers with
inset: 0ordvh, and respect safe areas on phones. stickykeeps an element in flow and sticks it at an offset within its nearest scroll container, until its parent ends.- Sticky needs an offset, no clipping ancestors in between, room in its parent, and
align-self: startin flex and grid. - Use
scroll-paddingso stuck headers don't hide anchor targets or focused elements.
Practice questions
Write CSS for a header that sticks to the top of the page while scrolling, with content scrolling behind it.
Answer
.site-header {
position: sticky;
inset-block-start: 0;
background: white;
z-index: 10;
}
html { scroll-padding-block-start: 5rem; } /* about the header's height */
A sticky sidebar in a two-column grid scrolls away with the page. It has position: sticky; top: 1rem;. What's the likely cause and fix?
Answer
The sidebar is a grid item stretched to the row's full height, so it has no room to stick. Add align-self: start to it, or align-items: start to the grid.
A modal with position: fixed; inset: 0; appears inside a card instead of covering the screen. Why?
Answer
An ancestor, probably the card or a wrapper, has a transform, filter or similar property, which makes it the containing block for fixed descendants. Move the modal out of it, or use a <dialog> shown with showModal(), which goes in the top layer.
Which of these let a position: sticky child stick to the page while scrolling: an ancestor with overflow: hidden, overflow: clip, or overflow: auto?
Answer
Only overflow: clip. hidden and auto create a scroll container, so the child sticks inside that box instead of the page.
Previous: Positioning: relative, absolute, Offsets, inset and the Containing Block
Next: Stacking Contexts and z-index: Why Your z-index Doesn't Work

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.