Key points
- A sticky element needs a threshold (
top,bottom…) to know where to stick. - An ancestor with
overflow: hiddenorautois by far the most common culprit;overflow: clipsidesteps it. - In grid or flexbox, an item stretched to its parent’s height has no room to move: use
align-self: start.
Everyone has been here. You add position: sticky to a table of contents or a header, it works perfectly on a test page, then does nothing once it’s inside the real theme. No error anywhere; the element just scrolls away like everything else. In nearly every case we come across, the cause is one of the four below, and it’s quick to find.
Cause 1: no threshold
A sticky element behaves normally until it reaches a threshold, then stays put. With no threshold, there’s nothing to reach:
.toc {
position: sticky;
top: 1rem; /* no threshold, no sticking */
}
It sounds too obvious, but it happens regularly when a more specific rule or a responsive variant resets top: auto without anyone noticing. The Computed tab in your browser’s dev tools shows the value actually applied.
Cause 2: an ancestor creates a scroll container
A sticky element sticks relative to its nearest scrolling ancestor. If a parent has overflow: hidden, auto or scroll, that parent becomes the reference, and since it doesn’t scroll itself, nothing happens. It can sit several levels up, added by the theme or a plugin. This snippet lists them all:
// Paste into the browser console
function scrollingAncestors(el) {
for (let p = el.parentElement; p; p = p.parentElement) {
const { overflowX, overflowY } = getComputedStyle(p);
if (/auto|scroll|hidden/.test(overflowX + ' ' + overflowY)) {
console.log(p, overflowX, overflowY);
}
}
}
scrollingAncestors(document.querySelector('.toc'));
The offender we meet most is overflow-x: hidden on the theme’s main wrapper (.site here), added to hide a stray horizontal scrollbar. There’s a twist: once one axis is hidden, the other computes to auto, so the element becomes a scroll container. clip, supported in current browsers, cuts off the overflow without that side effect:
/* Before: breaks every sticky element on the page */
.site { overflow-x: hidden; }
/* After: clips the overflow without creating a scroll container */
.site { overflow-x: clip; }
Better still, find whatever is overflowing and fix it, rather than hiding the symptom.
Cause 3: the parent leaves no room
A sticky element never escapes its parent: it sticks while the parent is on screen, then leaves with it. If the parent is exactly as tall as the element, there’s simply no room to travel.
The textbook case is a sidebar in a grid or flex layout. Items stretch to the row height by default, so the sidebar is as tall as the article and has nowhere to go:
.article-layout {
display: grid;
grid-template-columns: 1fr 280px;
gap: 2rem;
}
.article-layout aside {
position: sticky;
top: 1rem;
align-self: start; /* otherwise the column is as tall as the article */
}
With align-self: start, the sidebar returns to its natural height and can slide along the article.
Cause 4: it sticks, but hides your content
The last one isn’t strictly a bug: the header sticks fine, but anchor links land the target heading right underneath it, out of sight. One line fixes it across the page:
html {
scroll-padding-top: 5rem; /* sticky header height plus a little room */
}
And keep sticky headers modest on mobile: on a small screen, a header that eats a quarter of the viewport gets in the way more than it helps.
FAQ
Does sticky work in tables?
Yes, on header cells: th { position: sticky; top: 0; } keeps the header row visible on a long table. Give those cells a background, or the rows will show through as they scroll past.
Can CSS tell me when the element is currently stuck?
Not reliably across browsers yet. The usual approach is to watch a tiny sentinel element placed just before it with an IntersectionObserver, and toggle a class when it leaves the viewport.
Layout behaving differently in production than in the mockup? We build and fix custom themes, tested on the browsers your visitors actually use.



