A sticky element that will not stick almost always has a missing top offset, an ancestor with overflow set, or a parent with no room to scroll. Here is how to find which.
The short answer
#position: sticky needs an offset such as top: 0, and it only sticks within its nearest scrolling ancestor and inside its own parent. If any ancestor has overflow: hidden, auto or scroll, or the parent is no taller than the sticky element, it will look like nothing happens.
_10.header {_10 position: sticky;_10 top: 0; /* required */_10 z-index: 10;_10}
Why it happens
#A sticky element behaves like a normal element until its scroll container scrolls it to the offset you set. Then it holds that position, but only while its parent box is still on screen. Three rules follow from that:
- It needs a threshold. Without
top,bottom,leftorright, the browser has no point at which to start sticking. - It sticks to the nearest scroll container. Any ancestor with
overflowset tohidden,autoorscrollbecomes that container, even if it never actually scrolls. The element then waits for that box to scroll, which never happens, so it never sticks. - It cannot leave its parent. The sticky element is constrained to its containing block. If the parent is exactly as tall as the sticky element, there is no distance to travel.
How to fix it
#Add an offset
The most common cause is simply forgetting the offset.
_10.sidebar {_10 position: sticky;_10 top: 1rem;_10}
Find the ancestor with overflow
Open your browser's developer tools, select the sticky element, and walk up the tree checking the computed overflow value of each ancestor. A quick way is to run this in the console with the element selected:
_10let el = $0.parentElement;_10while (el) {_10 const { overflow, overflowX, overflowY } = getComputedStyle(el);_10 if ([overflow, overflowX, overflowY].some((v) => v !== 'visible')) {_10 console.log(el, overflow, overflowX, overflowY);_10 }_10 el = el.parentElement;_10}
Once you find it, you have a few options:
- Remove the
overflowrule if it is not needed. - If you only used
overflow: hiddento clip content or stop horizontal scrolling, switch tooverflow: clip. It clips in the same way but does not create a scroll container, so sticky keeps working.
_10.page {_10 overflow-x: clip; /* instead of overflow-x: hidden */_10}
- If the ancestor really is meant to scroll, that is fine, but the element will stick relative to that box, not the window.
Give the parent room
In a two-column layout the sidebar's parent is often a flex or grid container. By default flex and grid items stretch to fill the row, so the sticky sidebar becomes as tall as its column and has nowhere to move. Stop it stretching:
_10.layout {_10 display: grid;_10 grid-template-columns: 16rem 1fr;_10 align-items: start; /* do not stretch the sidebar */_10}_10_10.sidebar {_10 position: sticky;_10 top: 1rem;_10}
For a single item in Flexbox you can use align-self: flex-start on the sticky element.
Add a z-index and a background
Sticky headers often do work, but content scrolls over the top of them so they seem to vanish. Give the header a background colour and a z-index so it paints above the content that follows it.
Common pitfalls
#overflow: hiddenonhtmlorbody****. Often added to stop sideways scrolling. Useoverflow-x: clipinstead.- Sticky table headers. Put
position: stickyon thethcells, not ontheadortr, for the most consistent results. - Using
bottom: 0and expecting it to stick to the top. The offset you choose decides which edge it sticks to. - Expecting it to escape its section. A sticky heading inside a short
sectionstops sticking when that section scrolls away. That is by design. - Transforms on ancestors. These do not break sticky by themselves, but they do create a new stacking context, which can affect your
z-index.
Related reading
#Was this resource helpful?