More CSS answers

by Danny
Published 2 min read

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:

  1. It needs a threshold. Without top, bottom, left or right, the browser has no point at which to start sticking.
  2. It sticks to the nearest scroll container. Any ancestor with overflow set to hidden, auto or scroll becomes that container, even if it never actually scrolls. The element then waits for that box to scroll, which never happens, so it never sticks.
  3. 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:


_10
let el = $0.parentElement;
_10
while (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 overflow rule if it is not needed.
  • If you only used overflow: hidden to clip content or stop horizontal scrolling, switch to overflow: 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: hidden on html or body****. Often added to stop sideways scrolling. Use overflow-x: clip instead.
  • Sticky table headers. Put position: sticky on the th cells, not on thead or tr, for the most consistent results.
  • Using bottom: 0 and 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 section stops 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.
#

Was this resource helpful?