12k
All articles

Why position: sticky Isn't Working

Fix position: sticky when it fails. Check overflow ancestors, missing insets, short containing blocks and flex or grid stretching, then use a console snippet to find blockers.

OpenReplay Team
OpenReplay Team
Why position: sticky Isn't Working

position: sticky usually fails for one of three reasons: an ancestor has an overflow value that turns it into a scroll container, no inset such as top is set, or the element’s containing block leaves it no room to move.

You add position: sticky; top: 0 to a header, scroll, and the header scrolls away like any other block. The browser shows no error, and the rule is crossed out nowhere in DevTools. This gets worse when the culprit sits three wrappers up, inside a theme or an app shell you didn’t write.

This article covers the six causes behind that failure. Each one has a minimal broken example and its fix, and the last section gives a console snippet that finds the ancestor causing the problem. If you need to build a sticky header from scratch instead, see building a responsive sticky navbar with CSS.

Key Takeaways

  • A sticky element sticks to the nearest ancestor whose overflow is hidden, scroll, auto or overlay, even when that ancestor never scrolls.
  • overflow: clip hides overflow without creating a scroll container, so it can replace overflow: hidden without breaking sticky descendants.
  • Without a non-auto inset on the scroll axis, a sticky element behaves exactly like position: relative.
  • A sticky element can never leave its containing block. A short parent, a fixed-height parent or a stretched flex/grid item all leave it with no room to travel.
  • In flex layouts, align-self: flex-start stops a sticky sidebar from stretching. In grid layouts, use align-self: start.
What you seeLikely cause
Never sticks at allOverflow ancestor, missing inset, parent too short, or flex/grid stretch
Sticks, then scrolls away partway downContaining block ends (fixed-height parent)
Sticks but is covered or see-throughMissing background or z-index

An Ancestor Has overflow hidden, auto or scroll

An ancestor with an overflow value other than visible or clip captures a sticky element. MDN’s position reference says a sticky element attaches to the closest ancestor with a “scrolling mechanism”, meaning overflow set to hidden, scroll, auto or overlay. This happens whether or not that ancestor ever scrolls. The ancestor becomes the sticky reference. Because it doesn’t scroll, the element just moves with the page.

The classic trap is overflow-x: hidden on a page wrapper, added to stop horizontal scrollbars. MDN’s overflow-y page explains why this also affects vertical scrolling: if overflow-x is hidden, scroll or auto and overflow-y is visible (the default), overflow-y computes to auto. The wrapper becomes a scroll container on both axes.

<div class="page">
  <header class="site-header">Header</header>
  <main style="height: 200vh">Content</main>
</div>
/* Broken: .page captures the sticky header */
.page { overflow-x: hidden; }
.site-header { position: sticky; top: 0; background: #fff; }

/* Fixed */
.page { overflow-x: clip; } /* or remove the overflow rule */

clip works because it clips content without creating a scroll container. The MDN overflow page makes the same point: an element with clip never becomes a scroll container, so the browser ignores it when it looks for the sticky element’s scrolling ancestor. The CSS Overflow Module Level 3 also lets clip paired with visible stay as it is. Only a value that is neither visible nor clip changes the other axis: a visible there computes to auto, and a clip computes to hidden.

A scroll wrapper you added on purpose, such as a scrollable table container, is fine. The element sticks inside that wrapper, which is usually what you want.

No top, bottom, left or right Is Set

position: sticky needs at least one non-auto inset on the axis you scroll. Use top or bottom for vertical scrolling, left or right for horizontal, or a logical property such as inset-block-start. Without one, the element behaves exactly like position: relative. Setting left: 0 will not make a header stick vertically.

.site-header { position: sticky; }          /* acts like relative */
.site-header { position: sticky; top: 0; }  /* sticks */

If the inset is in your stylesheet, check the Computed panel anyway. A later rule or a utility class can reset top to auto.

The Containing Block Is Too Short

A sticky element can only move within its containing block, which is usually its parent. “Never moves” and “stops halfway” come from the same constraint at different sizes.

The Parent Is No Taller Than the Element

When the parent is exactly as tall as the sticky element, the element has no distance to travel, so it never appears to stick.

<!-- Broken: wrapper height equals header height -->
<div class="header-wrap">
  <header class="site-header">Header</header>
</div>
<main>…</main>

Fix it by moving the header up so its parent is the tall layout root, or by putting position: sticky; top: 0 on .header-wrap itself.

A Fixed-Height Parent Ends Too Soon

When a sticky element pins correctly and then scrolls away partway down the page, it has reached the bottom edge of its containing block. That containing block is usually a parent with a fixed height.

.panel { height: 600px; }      /* sticky child releases after 600px */
.panel { min-height: 600px; }  /* grows with content */

Flex or Grid Items Stretch to Fill the Container

Flex and grid items stretch to the full cross-axis size of their container by default. That leaves a sticky sidebar as tall as the content beside it, so it has no room to move. align-self overrides the container’s align-items for a single item. Setting it to flex-start in a flex container, or start in a grid container, sizes the item to its content so it can stick.

.layout { display: flex; gap: 2rem; }
.sidebar { position: sticky; top: 1rem; }                          /* stretched */
.sidebar { position: sticky; top: 1rem; align-self: flex-start; }  /* sticks */

/* Grid layout */
.sidebar { position: sticky; top: 1rem; align-self: start; }

How Do You Make Table Headers Sticky?

For a sticky table header, put position: sticky and a solid background on the th cells, not on the table element. Without a background, rows show through as they scroll underneath.

<div class="table-wrap">
  <table>
    <thead><tr><th>Name</th><th>Status</th></tr></thead>
    <tbody><!-- many rows --></tbody>
  </table>
</div>
.table-wrap { max-height: 300px; overflow: auto; } /* intentional scroll container */
thead th { position: sticky; top: 0; background: #fff; z-index: 1; }

The headers stick to the top of .table-wrap, not the viewport, because the wrapper is the scrolling ancestor.

Why Is a Sticky Element Hidden Behind Content?

A sticky element always creates a stacking context, and because the element is positioned, z-index applies to it. If scrolled content paints over it or shows through it, give it a background color and a z-index higher than the content it should cover. Any value works as long as it beats the competing elements in the same stacking context. A large number isn’t needed.

Finding the Ancestor When position: sticky Is Not Working

To find the ancestor that captures a sticky element, select the element in DevTools and walk up the Elements tree. For each ancestor, check the computed overflow-x and overflow-y. Any value other than visible or clip points to the cause.

The snippet below does the same walk in the console. It takes a selector or an element. If nothing matches, it logs a warning instead of throwing. It also confirms the computed position really is sticky.

function findStickyBlockers(target) {
  const el = typeof target === 'string' ? document.querySelector(target) : target;
  if (!el) {
    console.warn('findStickyBlockers: no element found for', target);
    return;
  }
  const pos = getComputedStyle(el).position;
  if (pos !== 'sticky') {
    console.warn('Computed position is', pos, '- another rule may override sticky');
  }
  const safe = (v) => v === 'visible' || v === 'clip';
  const root = document.documentElement;
  const rootStyle = getComputedStyle(root);
  // <body> overflow moves to the viewport when <html> overflow is visible
  const bodyPropagates = rootStyle.overflowX === 'visible' && rootStyle.overflowY === 'visible';
  for (let node = el.parentElement; node && node !== root; node = node.parentElement) {
    if (node === document.body && bodyPropagates) continue;
    const { overflowX, overflowY } = getComputedStyle(node);
    if (!safe(overflowX) || !safe(overflowY)) {
      console.log(node, { overflowX, overflowY });
    }
  }
}

findStickyBlockers('.site-header');

findStickyBlockers checks overflowX and overflowY separately, so clip is never flagged by mistake. It stops before <html> and skips <body> when the body’s overflow passes to the viewport, because in that case neither element is a scroll container. If the walk logs nothing, overflow isn’t the cause. Check the parent’s height and flex/grid stretching next.

Conclusion

Almost every sticky failure comes back to two facts: the element attaches to the nearest scroll container, and it can’t leave its containing block. Run the snippet against your element first. If it names an ancestor, change that ancestor’s hidden to clip or remove the overflow rule. If it logs nothing, check the inset, the parent’s height and align-self, in that order.

FAQs

What is the difference between position: sticky and position: fixed?

A sticky element stays in normal flow and keeps its space. It only pins once it reaches its inset threshold, and it releases at the edge of its containing block. A fixed element leaves normal flow, so no space is reserved for it. It is positioned against the viewport and stays pinned for the whole page, unless an ancestor with a transform, filter or perspective becomes its containing block.

How do I stack multiple sticky headers without them overlapping?

Give each sticky element a different top value, offset by the combined height of the sticky elements above it. For example, a site header with top: 0 and 60px of height pairs with a sub-navigation set to top: 60px. Section headings that each sit inside their own section push each other out naturally, because each one releases when its section, its containing block, ends.

Can CSS detect when a sticky element is stuck?

Yes, in Chromium browsers. Set container-type: scroll-state on the sticky element, then style its descendants inside @container scroll-state(stuck: top). Chrome and Edge 133 added support. Firefox and Safari do not support it, so wrap it in @supports (container-type: scroll-state) and use an IntersectionObserver on a sentinel element when you need the same effect in every browser.

Open-source session replay

Complete picture for complete understanding

Capture every clue your frontend is leaving so you can instantly get to the root cause of any issue with OpenReplay — the open-source session replay tool for developers. Self-host it in minutes, and have complete control over your customer data.

Star on GitHub12k

We use cookies to improve your experience. By using our site, you accept cookies.