Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

How does position: sticky work, and why does it sometimes not stick?

Questions in this chapter (6)
  • Draft
  • medium
  • Askedvery often
  • Verified October 2026

Answer

position: sticky is relative positioning with an offset the browser recomputes as you scroll:

  1. The box is laid out in normal flow and keeps its space, like relative.
  2. The browser finds the nearest ancestor scroll container. Its scrollport (the visible area), shrunk by the box's non-auto insets, is the sticky view rectangle. top: 0 means "the top edge of that scrollport".
  3. Whenever the box would cross an edge of that rectangle on a side with a non-auto inset, it's shifted back inside it.
  4. The shift stops when the box reaches the edge of its containing block, usually its parent. That's why a section heading scrolls away with the end of its section.

So debugging a sticky element that doesn't stick is checking those three inputs:

  • No inset. With top, bottom, left and right all auto, there's no edge to stick to; it behaves like relative.
  • The wrong scroll container. Any ancestor with overflow other than visible or clip is a scroll container, even overflow: hidden, which never scrolls. The sticky box sticks to that ancestor, which moves along with the page, so it looks dead. Setting only overflow-x: hidden also counts, because overflow-y: visible then computes to auto.
  • No room to move. If the containing block is barely taller than the sticky box, there's nowhere to slide. A sidebar stretched to the full height of a flex or grid row is the classic case.

Two more behaviours worth naming: multiple sticky boxes in one container are offset independently, so they can overlap; and a sticky box always forms a stacking context, even with z-index: auto.

Example

<style>
  .scroller { height: 200px; overflow: auto; }
  section { height: 300px; }
  h2 { position: sticky; top: 0; height: 40px; margin: 0; }
</style>
<div class="scroller" id="plain">
  <section><h2></h2></section>
  <section></section>
</div>
<div class="scroller" id="wrapped">
  <div style="overflow: hidden">
    <section><h2></h2></section>
    <section></section>
  </div>
</div>
<script>
  for (const scroller of document.querySelectorAll(".scroller")) {
    const heading = scroller.querySelector("h2");
    const offsets = [100, 280].map((y) => {
      scroller.scrollTop = y;
      return heading.getBoundingClientRect().top - scroller.getBoundingClientRect().top;
    });
    console.log(scroller.id, offsets.join(" "));
  }
  // → plain 0 -20
  // → wrapped -100 -280
</script>

Chrome 155 and Firefox 158 agreed. In plain, the heading is pinned at 0 after 100px of scrolling, then pushed out at -20 because only 20px of its section is left. In wrapped, the overflow: hidden div is the nearest scroll container; it never scrolls, so the heading moves exactly with the content.

Pitfalls

  • Hiding horizontal overflow on a page wrapper. With overflow-x: hidden on it, its overflow-y computed to auto and a top: 0 header sat at -500 after scrolling 500px; with overflow-x: clip it stayed at 0.
  • Stretching the sticky element. A top: 0 sidebar in a flex row was as tall as the 600px row and scrolled away (-100 at 100px of scroll); align-self: start kept it at 0.
  • Wrapping the sticky element in a tight parent. A heading whose section was only its own height never stayed put.
  • Expecting top: 0 to stick to the window when an ancestor scrolls. It sticks to the nearest scroll container, whichever that is.

Follow-ups

How do you fix a sticky element under an overflow: hidden ancestor?
Use overflow: clip if the ancestor only needs clipping: it clips without becoming a scroll container. Otherwise move the sticky element outside that ancestor, or make that ancestor the intended scroller with a real height.
What does bottom: 0 do on a sticky element?
It keeps the box from going below the scrollport's bottom edge. A footer laid out 500px down a 200px scroller sat at 170 (200 − 30px height) until its natural position scrolled into view.
Can a sticky box be offset upwards when it only has top set?
No. With top set and bottom: auto, the sticky offset only pushes it down from its in-flow position. When its containing block's end edge arrives, that offset shrinks back toward zero, so the box leaves with its section.

Exercises

What does each scroll position print?

<style>
  .scroller { height: 200px; overflow: auto; }
  section { height: 300px; }
  h2 { position: sticky; top: 10px; height: 40px; margin: 0; }
</style>
<div class="scroller"><section><h2></h2></section><section></section></div>
<script>
  const scroller = document.querySelector(".scroller");
  const heading = document.querySelector("h2");
  for (const y of [0, 50, 270]) {
    scroller.scrollTop = y;
    console.log(y, heading.getBoundingClientRect().top - scroller.getBoundingClientRect().top);
  }
</script>

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.