When position: sticky does nothing, the CSS on the element itself is usually fine. The problem sits somewhere else: a parent that leaves the element no room to move, an ancestor that quietly became the scroll container, or an offset that points the wrong way. This guide covers the causes the basic checklist does not, starting with a DevTools script that finds the culprit in seconds.

Short answer: sticky needs an offset (top, bottom, left or right) and room to move inside its parent, measured from the nearest scroll container. It fails when a flex or grid parent stretches the element to full height, when an ancestor has overflow set to anything except visible, or when the parent is exactly as tall as the element.

For the four basic causes (missing offset, overflow ancestor, short parent, overriding rule), see the sticky section of the CSS position guide. This article starts where that checklist ends.

Find the culprit first: a DevTools script

Guessing wastes time, because the broken ancestor is often several levels up and its overflow rule lives in a different stylesheet. Select the sticky element in the Elements panel, open the Console and run this. It compares the element's height with its parent's, shows how a flex or grid parent aligns it, and lists every ancestor that is a scroll container.

const el = $0; // the element selected in the Elements panel
const parent = el.parentElement;
const html = document.documentElement;
const p = getComputedStyle(parent);
console.log('heights:', el.offsetHeight, 'element,', parent.offsetHeight, 'parent');
console.log('parent display:', p.display, '| align-items:', p.alignItems, '| element align-self:', getComputedStyle(el).alignSelf);

for (let a = parent; a && a !== html; a = a.parentElement) {
  const s = getComputedStyle(a);
  const propagated = a === document.body && getComputedStyle(html).overflow === 'visible';
  if (!propagated && (s.overflowX !== 'visible' || s.overflowY !== 'visible')) {
    console.log('scroll container:', a, s.overflowX, s.overflowY);
  }
}

How to read the output:

  • Heights match: the parent leaves no room. Look for flex or grid stretch (next section) or a wrapper element that is only as tall as the sticky element.
  • Parent is flex or grid, align-items is normal or stretch, and align-self is auto: the element is being stretched.
  • A "scroll container" line: sticky now measures against that ancestor, which usually never scrolls. See the overflow section.

Position sticky not working with flex or grid

This is the most common sticky bug in two-column layouts. Flex and grid items stretch to fill the container on the cross axis by default, so a sidebar in a flex row becomes exactly as tall as the row. Sticky only moves an element inside its parent, and a sidebar that already fills the parent has nowhere to go. It looks like sticky is ignored, but there is simply no space to travel.

.layout { display: flex; gap: 24px; }

.sidebar {
  position: sticky;
  top: 24px;
  align-self: flex-start; /* stop the default stretch */
}

align-self: flex-start on the item, or align-items: flex-start on the container, lets the sidebar keep its own height. height: fit-content works too.

Grid has the same default

Grid items stretch as well, so the fix has the same shape. Use start instead of flex-start:

.layout { display: grid; grid-template-columns: 240px 1fr; gap: 24px; }

.sidebar {
  position: sticky;
  top: 24px;
  align-self: start;
}

Alternative: leave the column stretched and put the sticky rule on an element inside it. The stretched column is as tall as the row, so the inner element has room to move. This is useful when you cannot change the container's alignment. You can test the alignment values in the Flexbox Generator or the CSS Grid Generator.

overflow-x: hidden and the wrapper trap

The position guide notes that an ancestor with overflow: hidden breaks sticky. Two details make it easy to miss. First, setting one axis changes the other: when overflow-x is hidden, scroll or auto and overflow-y is visible, the browser computes overflow-y as auto. A rule meant to stop horizontal scrolling also makes the element a vertical scroll container. Second, that container never scrolls, because its height is auto, so sticky measures against something that never moves and never sticks.

/* Breaks sticky for everything inside .page */
.page { overflow-x: hidden; }

/* Keeps sticky working: clip hides overflow without creating a scroll container */
.page { overflow-x: clip; }

overflow: clip is supported in all current browsers (Safari from version 16). Unlike hidden, it can be combined with overflow-y: visible.

html and body

When only body sets overflow, the browser passes the value up to the viewport, so sticky keeps working. The trap appears when html sets it too, a common reset:

html, body { overflow-x: hidden; }

html now takes the viewport, so body keeps its own overflow and becomes a scroll container. Remove the rule from body or switch both to clip.

Fix the cause, not the symptom: overflow-x: hidden on a wrapper is usually added to hide a horizontal scrollbar. The scrollbar means one element is wider than the viewport. Find that element and fix its width, and the wrapper rule is no longer needed.

position: sticky with bottom: 0

top: 0 and bottom: 0 do opposite jobs. With top: 0, the element is held at the top edge once it would scroll past it. With bottom: 0, it is held at the bottom edge while its normal position is below the visible area, and it settles into its own place when you scroll down to it. A sticky bottom bar therefore only works if it comes after the content it belongs to and the parent is taller than the viewport.

Three setups make bottom: 0 look broken:

  • The element is first in its parent. Its normal position is at the top, never below the visible area, so there is nothing for bottom to hold. Move it to the end of the parent in the HTML.
  • The parent is shorter than the viewport. On a short page everything is already visible, so the bar just sits at the end of the content.
  • An ancestor is a scroll container that never scrolls. bottom is measured against that container's bottom edge, not the window's, so give the container a fixed height or remove its overflow.
<form class="order-form">
  <!-- long list of fields -->
  <div class="action-bar">
    <button type="submit">Place order</button>
  </div>
</form>
.action-bar {
  position: sticky;
  bottom: 0;
  padding: 12px 16px;
  background: #fff; /* opaque, so fields do not show through */
  border-top: 1px solid #d8d4ec;
}

If the bar must stay visible on every page whatever its parent is, fixed is the better fit. The comparison table in the position guide covers when to pick which.

Sticky table headers not sticking

Tables add three problems of their own. Apply position: sticky and top: 0 to the th cells, which is the most reliable target, and give them an opaque background so rows do not show through.

Borders disappear

With border-collapse: collapse, borders belong to the table grid rather than the cell, so they stay behind when the header sticks. Use separate with zero spacing and put the border on the cell:

.data-table { border-collapse: separate; border-spacing: 0; }

.data-table th {
  position: sticky;
  top: 0;
  background: #fff;
  border-bottom: 1px solid #d8d4ec;
}

The scrolling wrapper moves the reference

Wide tables are often wrapped in a div with overflow-x: auto. That div becomes the scroll container, and since its height is auto it never scrolls vertically, so the header never sticks to the page. Limit the wrapper's height and let it scroll in both directions, and the header sticks to the top of the wrapper:

.table-wrap { max-height: 70vh; overflow: auto; }

/* Sticky first column inside the same wrapper */
.data-table th:first-child,
.data-table td:first-child { position: sticky; left: 0; background: #fff; }
.data-table th:first-child { z-index: 2; } /* corner cell stays above both */

Two sticky headers on top of each other

Every sticky element uses its own offset, so a second header with top: 0 slides underneath the first. Give it an offset equal to the first header's height. A custom property keeps the two in sync:

:root { --nav-h: 64px; }

.nav { position: sticky; top: 0; height: var(--nav-h); z-index: 30; }
.subnav { position: sticky; top: var(--nav-h); z-index: 20; }

Each element also stays stuck only while its own parent is on screen. If .subnav sits inside a short section, it leaves with that section. Put both headers in a parent that spans the whole scroll area when they need to stay together. Sticky always creates a stacking context, which the z-index guide explains.

Position sticky not working with JavaScript, React and Vue

Sticky is pure CSS, so no script is needed to make it work. Scripts and frameworks break it in three ways.

A component wrapper becomes the parent

In React, Vue and similar frameworks a component often renders one extra root element. If the sticky element sits inside a component that returns a wrapper div, that wrapper is its parent. It is exactly as tall as the element, so there is no room to stick.

// Broken: the wrapper div is the sticky element's parent
function Sidebar() {
  return (
    <div>
      <nav className="sidebar">...</nav>
    </div>
  );
}

// Fixed: the sticky element is the root, so its parent is the layout container
function Sidebar() {
  return <nav className="sidebar">...</nav>;
}

A fragment (<>...</>) works the same way when the component needs to return several elements.

A scroll lock adds overflow to an ancestor

Modal and drawer libraries lock page scroll by setting overflow: hidden on html, body or a wrapper. If the rule is left behind after the modal closes, or lands on a wrapper that contains the sticky element, sticky stops working until a reload. Run the script from the first section while the modal is closed and look for a scroll container line.

Detecting when an element is stuck

Styling an element differently once it sticks, such as adding a shadow under a header, needs a small observer. Place a 1px sentinel directly above the sticky element and watch it with IntersectionObserver. When the sentinel leaves the viewport, the element is stuck.

const header = document.querySelector('.site-header');
const sentinel = document.querySelector('.sentinel');

new IntersectionObserver(([entry]) => {
  header.classList.toggle('is-stuck', !entry.isIntersecting);
}).observe(sentinel);
.sentinel { height: 1px; }
.site-header { position: sticky; top: 0; }
.site-header.is-stuck { box-shadow: 0 2px 8px rgb(0 0 0 / 0.15); }

This assumes top: 0. For another offset, pass a rootMargin with a negative top value equal to that offset.

Quick fix by symptom

SymptomLikely causeFix
Sidebar never moves in a two-column layoutFlex or grid stretches it to full heightalign-self: flex-start or align-self: start
Works on one page, fails on anotherA wrapper or reset adds overflow to html, body or a divRun the DevTools script, then use overflow-x: clip
Bottom bar does not stickIt comes first in its parent, or the parent is shorter than the viewportMove it to the end of the parent and check the height
Table header does not stickA scrolling wrapper or collapsed bordersSet max-height and overflow: auto on the wrapper, use border-collapse: separate
Second header slides under the firstBoth use top: 0Offset the second with a --nav-h custom property
Sticky stops after a modal closesA scroll lock left overflow on an ancestorRemove the leftover rule and re-run the script
Fails inside a React or Vue componentThe component wrapper is the only parentPut the sticky styles on the root or return a fragment

FAQ

Why is position sticky not working in a flex container?

Flex items stretch to the full height of the container by default, so a sticky sidebar is as tall as its parent and has no room to move. Add align-self: flex-start to the sticky item, or align-items: flex-start to the container, and the item keeps its own height and starts to stick.

Does overflow-x: hidden break position sticky?

Yes, on any wrapper element, and on body when html also sets overflow. Hiding one axis makes the other axis compute to auto, so the element becomes a scroll container that never scrolls, and sticky measures against it. Use overflow-x: clip instead, which hides the overflow without creating a scroll container.

Why is position sticky with bottom: 0 not working?

bottom: 0 holds the element at the bottom edge only while its normal position is below the visible area. If the element is first in its parent, or the parent is shorter than the viewport, it never sits below the fold and nothing sticks. Put the element after the content it belongs to and make sure the parent is taller than the screen.

Does position sticky work in CSS Grid?

Yes. Grid items stretch by default, so add align-self: start to the sticky item, otherwise it fills its grid area and has no room to move. Everything else works as in a normal block layout, including the need for a top, bottom, left or right offset.

Why does my sticky table header not stick?

Usually the table sits inside a wrapper with overflow-x: auto, which becomes the scroll container and never scrolls vertically. Give the wrapper a max-height and overflow: auto so the header sticks inside it, apply position: sticky to the th cells, and use border-collapse: separate so the borders move with the cells.

Why does position sticky stop working in React or Vue?

A component that renders a wrapper element makes that wrapper the sticky element's parent, and a parent exactly as tall as its child leaves no room to stick. Put the sticky styles on the component's root element, or return a fragment so the element sits directly inside the layout container.

How do I find which ancestor is breaking position sticky?

Select the element in DevTools and run a short script in the console that walks up the parent chain and logs every ancestor whose overflow is not visible. The first section of this guide has the script, plus a check for flex and grid stretch.

Do I still need -webkit-sticky?

No. Safari has supported unprefixed position: sticky since version 13, so position: sticky alone covers every current browser. The -webkit-sticky prefix only matters for Safari 12 and older.

See align-self change a layout

Switch align-items and align-self in the Flexbox Generator and watch a tall sidebar shrink to its own height, then copy the CSS.

Open Flexbox Generator →