position: sticky Not Working – Five Causes and How to Find Them
You have a filter bar above a product list. It should stick to the top of the screen as the user scrolls the list – a textbook case for position: sticky:
css
.filters {position: sticky;top: 0;background: #fff;z-index: 10;}
You reload, scroll – and the bar slides away with the list as if the rule weren't there. In devtools the rule is active, nothing is crossed out, the console is empty. The guessing begins: maybe z-index, maybe the -webkit- prefix, maybe it needs display: block. None of these changes help, because the problem almost never lies in the element itself.
In the scroll-driven animations article I wrote that position: sticky was created to replace a scroll listener toggling position: fixed – and that's exactly why it scrolls smoothly on the compositor thread. The price is a model that assumes quite a lot about the element's surroundings. When those assumptions aren't met, sticky doesn't report an error; it just quietly behaves like position: relative. To find the cause quickly, you need to know what sticky is looking at.
How sticky works under the hood: two rectangles
An element with position: sticky behaves like a regular in-flow element most of the time. It only moves when scrolling would push it past a given threshold. The browser computes this from two rectangles:
- The nearest scrolling ancestor (the scrollport) – the element that
top: 0is measured against. It's not always the browser window. It's the first ancestor that is a scroll container, even if in practice it never scrolls. - The containing block – usually the direct parent. The element can only "stick" within its bounds. When the parent leaves the screen, it takes the element with it.
Each of the five causes below is a situation where one of these rectangles is different from what you assume.
Cause 1: no top, bottom, left or right
The simplest, and the most common on a first attempt. position: sticky alone, without any offset, has no threshold at which to stick, so the element behaves exactly like position: relative.
css
/* Doesn't work – no threshold */.filters {position: sticky;}/* Works – sticks when its top edge touches the top of the scrollport */.filters {position: sticky;top: 0;}
It's also worth remembering that top is the distance from the edge of the scrollport, not from the element. If there's a 64px sticky site header above the list, the filters need top: 64px – otherwise they'll hide underneath it. The cleanest approach is to keep that height in one CSS variable used by both elements.
Cause 2: an ancestor with overflow other than visible
This is the number one cause in existing projects. Any ancestor with overflow: hidden, auto or scroll becomes a scroll container – and therefore the scrollport for sticky. The element now sticks to that ancestor's top edge, not to the window. When that ancestor doesn't scroll (because it has overflow: hidden and grows with its content), there's no scrolling for sticky to react to. The window scrolls, and the ancestor – together with the "stuck" element – moves up as a whole.
html
<main class="layout"> <!-- overflow: hidden, to clip card shadows --><section class="products"><div class="filters">…</div> <!-- position: sticky; top: 0 – doesn't work --><ul class="product-list">…</ul></section></main>
The culprit is usually a wrapper added for an entirely different reason: clipping shadows, rounded corners with overflow: hidden, hiding an animation that slides in from off-screen. Nobody connects it to a sticky element three levels down.
The fix is overflow: clip. It clips content exactly like hidden, but it doesn't create a scroll container – so sticky inside still sees the window as its scrollport.
css
.layout {overflow: clip; /* instead of overflow: hidden */}
The difference is subtle but fundamental: an element with overflow: hidden can't be scrolled with the mouse, but it can be scrolled by script (scrollTop) – it's a full-fledged scroll container. An element with overflow: clip has no scrolling at all, not even programmatic. For sticky, only the latter value is neutral.
Cause 3: overflow-x: hidden quietly changing overflow-y
This cause deserves its own section, because it's the sneakiest. The page has horizontal scrolling – for example because of an element with width: 100vw, which I wrote about in the viewport units article – so someone adds a quick fix:
css
body {overflow-x: hidden;}
The horizontal scroll disappears. So does every sticky element on the page. The reason: overflow-x and overflow-y can't take arbitrary combinations. If one axis has a value other than visible (or clip) and the other is visible, the browser turns visible into auto. You only wrote overflow-x: hidden, but the computed value of overflow-y is now auto – and body has become a scroll container. In devtools you only see this in the Computed tab, not next to the rule you wrote.
The fix is the same as above, just for one axis:
css
body {overflow-x: clip; /* clips horizontally, doesn't create a scroll container */}
The combination of clip with visible is allowed without any conversion, so overflow-y stays visible. Better still, remove the cause of the horizontal scroll – usually a single element wider than the page – instead of clipping the whole body.
Cause 4: a parent with no room
The second rectangle, the containing block, causes trouble in a completely different way. Sticky can only move within its parent's bounds. If the parent is exactly as tall as the element itself, there's nowhere to move.
html
<!-- Doesn't work: <header> is as tall as the nav, so there'sno room for .site-nav to "travel" with the scroll --><header class="site-header"><nav class="site-nav">…</nav> <!-- position: sticky; top: 0 --></header><main>…</main>
The nav sticks for a split second and slides away with its parent as soon as the <header> leaves the screen – which is immediately. The fixes: move position: sticky onto the <header> itself (whose parent is <body>, as tall as the whole page), or remove the unnecessary wrapper.
The same cause explains the often-reported "sticky sticks, but slides away halfway down the page": the element has reached its parent's bottom edge. That's not a bug – it's an intentional boundary that lets a section heading stop sticking when the next section begins.
Cause 5: a grid or flex item stretched to full height
A classic example: a sticky sidebar next to article content.
css
.article-layout {display: grid;grid-template-columns: 1fr 280px;gap: 32px;}.sidebar {position: sticky;top: 24px;}
It doesn't work, even though the parent is as tall as the whole article and no ancestor has overflow. The problem: in grid (and in flex) the default alignment is stretch. The sidebar is stretched to the full height of the row – meaning it's exactly as tall as the article. From sticky's point of view, this is cause 4 in disguise: the element fills its entire containing block and has no room to move within it.
css
.sidebar {position: sticky;top: 24px;align-self: start; /* the sidebar is as tall as its content, not the whole row */}
With align-self: start, the sidebar is as tall as its content, so it has room to move. If its content is taller than the screen, another problem appears – you'll never see the bottom part, because the element is stuck by its top edge. Limiting the height and scrolling inside helps here:
css
.sidebar {position: sticky;top: 24px;align-self: start;max-height: calc(100svh - 48px);overflow-y: auto;}
100svh – from the viewport units article – guarantees the sidebar fits on screen even on a phone with expanded browser toolbars.
The pitfall: finding the culprit in a minute
Causes 2 and 3 are the hardest, because the guilty element is far away from the sticky one – often in a different file, a different component, or even in a library's CSS. Instead of inspecting ancestors one by one in devtools, paste this into the console:
javascript
// Select the sticky element in the Elements tab – available in the console as $0let el = $0while ((el = el.parentElement)) {const { overflowX, overflowY } = getComputedStyle(el)const ok = ["visible", "clip"]if (!ok.includes(overflowX) || !ok.includes(overflowY)) {console.log(el, { overflowX, overflowY })}}
The script walks up the tree and logs every ancestor that is a scroll container – with computed values, so it also catches overflow-y: auto produced by a lone overflow-x: hidden. The first element logged is your sticky element's scrollport. If it's not the one you expect, you've found the cause.
Chrome also shows a scroll badge next to scroll containers in the Elements tab – a good second way to quickly spot where an unexpected scrollport begins.
Pitfalls and best practices
- Always set a threshold.
position: stickywithouttop,bottom,leftorrightis effectivelyposition: relative. - Use
overflow: clipfor clipping, nothidden. It clips the same way but doesn't create a scroll container. This applies especially to layout wrappers andbody. - Don't cure horizontal scrolling with
overflow-x: hiddenonbody. It turnsoverflow-yintoautoand disables every sticky element on the page. Find the element that's too wide – often it's100vw. - Check how much room the parent has. Sticky only moves within its containing block. A wrapper as tall as the element itself leaves it no space.
- In grid and flex, add
align-self: start. The defaultstretchstretches the element across the whole row and takes away its room to move. - A tall sticky element needs
max-heightand internal scrolling, or the bottom of its content will be unreachable. - Browser support.
position: stickyhas worked without prefixes in every browser for years –-webkit-stickywas only needed in Safari before version 13.overflow: clipis available since Chrome 90, Firefox 81 and Safari 16.
Summary
position: sticky is almost never broken by itself. It relies on two rectangles: the nearest scrolling ancestor it sticks to, and the containing block within which it can move. All five causes are situations where one of them is different from what you assume: a missing threshold, an ancestor with overflow: hidden, overflow-x: hidden quietly turning overflow-y into auto, a parent with no free space, and a grid item stretched across the whole row. Most of them are fixed by a single change – hidden to clip, or adding align-self: start – and a console script points to the culprit faster than inspecting ancestors one by one. And once sticky works, the next question comes up: how do you style an element only when it's actually stuck? I'll cover that in a separate article on scroll-state().