All articles
CSSWeb DevLayout

Why position: sticky Is Not Working (and How to Fix It)

position: sticky silently doing nothing? Find the overflow ancestor, add a threshold, give it room to stick and stop flex stretching. Fixes for each cause.

Tushar Pachouri11 min read

position: sticky is one of those properties that either works instantly or does absolutely nothing, with no error and no warning in DevTools. The element just scrolls away like a normal block. Almost every case comes down to a handful of causes, and once you know them you can diagnose a broken sticky element in under a minute.

TL;DR

  • Sticky needs a threshold: set at least one of top, bottom, left or right.
  • An ancestor with overflow: hidden, auto or scroll becomes the element's scroll container, and sticky only sticks inside that container. Use overflow: clip instead of hidden when you only want clipping.
  • The sticky element can only move within its parent. If the parent is no taller than the element, there is nowhere to stick.
  • In flex and grid layouts, children stretch to the full row height by default. Add align-self: start (Tailwind self-start).
  • Transforms, filters and contain break position: fixed, not sticky. Do not waste time on them.

How sticky actually works

A sticky element behaves like position: relative until its edge reaches the threshold you set, measured against its nearest scroll container. From then on it is held in place, but only while its parent (its containing block) is still on screen. When the parent's bottom edge scrolls past, the sticky element goes with it.

sticky element parent (its containing block) top threshold top top top 1. Scrolls normally 2. Held at the threshold 3. Leaves with its parent edge has not reached top parent still on screen parent's bottom passed
A sticky element scrolls, holds at its threshold, then leaves when its parent does.

So there are always two boxes in play:

BoxWhat it controls
Nearest scroll containerThe viewport the threshold is measured against
Parent elementThe range within which the element can move

Every cause below breaks one of those two boxes, or forgets the threshold.

Cause 1: no threshold

Without an offset, the browser has nothing to compare against, so the element never sticks.

CSS
/* Broken */
.site-header {
  position: sticky;
}
CSS
/* Fixed */
.site-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: white;
}

The z-index and background are not required for sticking, but without them content scrolls visibly underneath the header, which often gets reported as "sticky is broken". In Tailwind:

HTML
<header class="sticky top-0 z-10 bg-white">...</header>

Cause 2: an overflow ancestor

This is the most common cause and the least obvious one, because the offending CSS is often several levels up, in a layout component someone else wrote.

Any ancestor with overflow set to hidden, auto or scroll becomes a scroll container. The sticky element then sticks relative to that box, not the viewport. If that box never actually scrolls (because it grows with its content), the sticky element never has anything to react to.

The sidebar measures its threshold against .page-wrapper, which never scrolls.
HTML
<div class="page-wrapper">
  <aside class="sidebar">Sticky sidebar</aside>
  <main class="content">Long content</main>
</div>
CSS
/* Broken: added to stop a horizontal scrollbar from an animation */
.page-wrapper {
  overflow: hidden;
}

.sidebar {
  position: sticky;
  top: 1rem;
}

overflow: hidden surprises people because the box does not show scrollbars. But it is still a scroll container: it can be scrolled with JavaScript, and it captures sticky descendants.

The fix: overflow clip

overflow: clip clips content exactly like hidden but does not create a scroll container, so sticky descendants keep working against the viewport.

Diff
 .page-wrapper {
-  overflow: hidden;
+  overflow: clip;
 }

In Tailwind, swap overflow-hidden for overflow-clip (or overflow-x-clip if you only need the horizontal axis):

Diff
- <div class="overflow-hidden">
+ <div class="overflow-x-clip">

overflow: clip is supported in current versions of all major browsers. Safari was the last to add it, in version 16, so if you need to support older Safari, restructure so the clipping element is not an ancestor of the sticky one.

Note: overflow: clip also disables programmatic scrolling of that element. If you were relying on scrollTo or scrollLeft on the wrapper, you actually need a scroll container, and the sticky element must live in a layout where that container is what scrolls.

Why overflow-x hidden breaks the vertical axis

Setting only one axis does not save you. If one axis is hidden, auto or scroll and the other is visible, the browser computes the visible one as auto. So this:

CSS
.wrapper {
  overflow-x: hidden;
}

computes to overflow-x: hidden; overflow-y: auto, and .wrapper is now a vertical scroll container too.

The classic version of this bug is the "kill horizontal scroll" fix applied to both root elements:

CSS
/* Broken: body becomes a scroll container that never scrolls */
html,
body {
  overflow-x: hidden;
}

Normally overflow on body is propagated to the viewport, which is harmless. But when html has its own non-visible overflow, it is html's value that propagates, and body keeps its own overflow-x: hidden; overflow-y: auto. Every sticky element on the page now sticks relative to body, which grows with its content and never scrolls.

CSS
/* Fixed */
html,
body {
  overflow-x: clip;
}

clip paired with visible does not trigger the conversion to auto, so the vertical axis stays untouched. Better still, find the element that overflows horizontally and fix it at the source.

Cause 3: the parent has no room

A sticky element can never leave its parent. If the parent is exactly as tall as the sticky element, it sticks over a distance of zero pixels, which looks the same as not working.

HTML
<!-- Broken: the wrapper div is only as tall as the nav -->
<div>
  <nav class="toc">Table of contents</nav>
</div>
<article>Very long article</article>
CSS
.toc {
  position: sticky;
  top: 1rem;
}

The fix is to make the sticky element a child of the tall container, or make its wrapper span the full height of the content next to it:

HTML
<!-- Fixed: the nav's parent is as tall as the article -->
<div class="layout">
  <nav class="toc">Table of contents</nav>
  <article>Very long article</article>
</div>

This also explains a common header bug: a sticky <nav> inside a <header> that only wraps the nav. The header is the parent, so the nav scrolls away with it. Make the <header> itself sticky instead.

Cause 4: flex and grid stretching

This one is sneaky because the parent is tall enough. In a flex row or a grid, align-items defaults to stretching, so the sidebar becomes exactly as tall as the row. Its parent has room, but the element itself fills all of it, and a box that already fills its range cannot move within it.

A stretched sidebar fills the row and cannot move; flex-start leaves it room to stick.
CSS
/* Broken */
.layout {
  display: flex;
  gap: 2rem;
}

.sidebar {
  position: sticky;
  top: 1rem;
  width: 16rem;
}
Diff
 .sidebar {
   position: sticky;
   top: 1rem;
   width: 16rem;
+  align-self: flex-start; /* use align-self: start in grid */
 }

In Tailwind, add self-start to the sticky child, or items-start to the parent:

HTML
<div class="flex gap-8">
  <aside class="sticky top-4 w-64 self-start">Sidebar</aside>
  <main class="flex-1">Long content</main>
</div>

If you open DevTools and the sticky element's highlighted box is as tall as the content next to it, this is your problem.

Cause 5: sticky inside tables

Sticky table headers are a common request, and they mostly work if you put position: sticky on the right element.

  • th cells: works reliably in modern browsers. Use this.
  • thead and tr: historically inconsistent across browsers. Support has improved, but th is the safe choice.
CSS
/* Inconsistent */
thead {
  position: sticky;
  top: 0;
}

/* Reliable */
thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: white;
}

Two more table traps:

  1. Borders scroll away. With border-collapse: collapse, cell borders belong to the table, not the sticky cell, so they do not move with the header. Use border-collapse: separate; border-spacing: 0; and put borders on the cells.
  2. The responsive wrapper. Tables are often wrapped in a div with overflow-x: auto so they can scroll sideways on mobile. As we saw above, that wrapper is now a vertical scroll container too, so the header will not stick to the viewport. Give the wrapper a height and let it scroll in both directions; the header then sticks inside it.
HTML
<div class="max-h-[70vh] overflow-auto">
  <table class="w-full border-separate border-spacing-0">
    <thead>
      <tr>
        <th class="sticky top-0 z-10 border-b bg-white px-4 py-2 text-left">Name</th>
        <th class="sticky top-0 z-10 border-b bg-white px-4 py-2 text-left">Status</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="border-b px-4 py-2">Example row</td>
        <td class="border-b px-4 py-2">Active</td>
      </tr>
    </tbody>
  </table>
</div>

Not the cause: transforms and contain

You will find advice online to remove transform, filter, perspective, will-change: transform or contain from ancestors. That advice is about position: fixed, and applying it to sticky problems wastes time.

PropertyBreaks fixedBreaks sticky
transform, filter, perspective on an ancestorYes: the ancestor becomes the containing blockNo
contain: paint or contain: layout on an ancestorYes, same reasonNo
overflow: hidden, auto or scroll on an ancestorNoYes: the ancestor becomes the scroll container

A fixed element is positioned against the viewport unless an ancestor creates a new containing block for it, which is what those properties do. A sticky element is positioned against its nearest scroll container, and only overflow decides that.

Old iOS Safari and the prefix

Older Safari releases only understood the prefixed value. Safari 13 dropped the need for it, so unless you support very old iPhones you can skip it. If you do, put the prefixed declaration first:

CSS
.site-header {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
}

If you use Autoprefixer with a browserslist that includes those old versions, it adds this for you.

Find the scroll container in DevTools

Instead of clicking through ancestors one by one, paste this into the console. It walks up the tree and returns the first ancestor that creates a scroll container, along with whether it actually scrolls.

JavaScript
function findScrollParent(el) {
  const html = document.documentElement;
  const htmlStyle = getComputedStyle(html);
  const htmlIsVisible =
    htmlStyle.overflowX === 'visible' && htmlStyle.overflowY === 'visible';

  let node = el.parentElement;
  while (node && node !== html) {
    // Read each axis separately: computed values already include
    // the visible -> auto conversion, unlike the shorthand
    const { overflowX, overflowY } = getComputedStyle(node);
    const createsScroller = /(auto|scroll|hidden|overlay)/.test(`${overflowX} ${overflowY}`);

    // body's overflow is propagated to the viewport when html is visible
    const propagatedToViewport = node === document.body && htmlIsVisible;

    if (createsScroller && !propagatedToViewport) {
      return {
        node,
        overflowX,
        overflowY,
        actuallyScrolls: node.scrollHeight > node.clientHeight,
      };
    }
    node = node.parentElement;
  }
  return null; // sticks relative to the viewport, which is usually what you want
}

findScrollParent($0);

Select the sticky element in the Elements panel first; $0 refers to it. If the result is an element with actuallyScrolls: false, you have found your bug: switch it to overflow: clip or restructure. If it returns null, the scroll container is fine and you should look at the threshold, the parent's height or flex stretching.

A sidebar that sticks correctly

Here is a complete documentation-style layout with a table of contents that sticks next to long content and handles a TOC taller than the screen.

HTML
<div class="docs-layout">
  <article class="docs-content">
    <h2 id="intro">Introduction</h2>
    <p>Long content goes here.</p>
    <h2 id="setup">Setup</h2>
    <p>More long content.</p>
  </article>

  <aside class="docs-toc">
    <nav aria-label="On this page">
      <a href="#intro">Introduction</a>
      <a href="#setup">Setup</a>
    </nav>
  </aside>
</div>
CSS
.docs-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 16rem;
  gap: 3rem;
  max-width: 72rem;
  margin: 0 auto;
  padding: 0 1rem;
  /* no overflow here; use overflow: clip if you must clip */
}

.docs-toc {
  position: sticky;
  top: 5rem; /* height of the sticky site header plus a gap */
  align-self: start; /* do not stretch to the article's height */
  max-height: calc(100vh - 6rem);
  overflow-y: auto; /* fine: this only affects the TOC's own children */
}

.docs-toc nav {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

@media (max-width: 1024px) {
  .docs-layout {
    grid-template-columns: minmax(0, 1fr);
  }

  .docs-toc {
    display: none;
  }
}

The max-height and overflow-y: auto on the TOC matter for long lists: without them, the bottom of a TOC taller than the viewport is unreachable until the user scrolls to the end of the article. Making the TOC a scroll container is fine because sticky only cares about the scroll containers above it.

The same layout in Tailwind:

HTML
<div class="mx-auto grid max-w-6xl grid-cols-1 gap-12 px-4 lg:grid-cols-[minmax(0,1fr)_16rem]">
  <article class="min-w-0">Long content</article>

  <aside class="sticky top-20 hidden max-h-[calc(100vh-6rem)] self-start overflow-y-auto lg:block">
    <nav aria-label="On this page" class="flex flex-col gap-2">
      <a href="#intro">Introduction</a>
      <a href="#setup">Setup</a>
    </nav>
  </aside>
</div>

Checklist

  1. The element has top, bottom, left or right set.
  2. No ancestor has overflow: hidden, auto or scroll unless you want it to be the scroll container. Use overflow: clip for clipping.
  3. html and body do not both have overflow-x: hidden.
  4. The sticky element's parent is taller than the element itself.
  5. In flex or grid, the sticky child has align-self: start (Tailwind self-start).
  6. Table headers use position: sticky on th, with border-collapse: separate.
  7. Responsive table wrappers with overflow-x: auto have a height so the header can stick inside them.
  8. The sticky element has a background and z-index so content does not show through.
  9. Tall sticky sidebars have a max-height and their own overflow-y: auto.
  10. You are not chasing transform or contain; those only affect position: fixed.

Stuck on something like this?

I build and fix full-stack products for startups and indie founders. Tell me what's broken.

Get in touch