← Back to HTML & CSS, page 3

html-css-0045 · Hard senior

A sticky element does not stick. How do you debug it?

position: sticky fails when there is no offset such as top, when an ancestor with overflow hidden, auto or scroll becomes the scroll container, when the parent leaves no room to move, or when a flex item is stretched to the full row height. Walk up the ancestors and check each one's overflow in DevTools.

Full answer

position: sticky behaves like relative until the scroll position reaches a threshold, then it sticks, but only inside its containing block and only relative to its nearest scroll container. Most failures come from one of these causes.

  1. No threshold. Sticky needs at least one of top, bottom, inset-inline-start and so on. Without it, it never sticks.
  2. An ancestor has overflow set to hidden, auto or scroll. That ancestor becomes the scroll container for the sticky element, and if it does not scroll, nothing ever sticks. This is the most common cause.
  3. No room to move. The sticky box can only move inside its parent. If the parent is exactly as tall as the sticky element, there is nothing to travel through.
  4. Flex stretch. In a flex row, items stretch to the row height by default, so a sidebar becomes as tall as its row and has no room to move. Use align-self: flex-start.
  5. A threshold that is hidden behind another fixed header, so it sticks but sits under it. Set top to the header's height.

The test page puts each case in its own scroll box with a height of 150px, scrolls 100px, and compares positions.

After scrolling each box by 100px, the bar in the first box is still at the top of its box. In the second box it scrolled away, because the overflow: hidden wrapper took over as the scroll container. The third (stretched) sidebar also scrolled away, because it filled the row. The fourth, with align-self: flex-start, sticks.

In DevTools, walk up the ancestors of the sticky element and look at each computed overflow value. That finds cause two in a few seconds.

html
<style>
  body { margin: 0; }
  .scroller { height: 150px; overflow: auto; margin-bottom: 8px; border: 1px solid #999; }
  .bar { position: sticky; top: 0; background: gold; height: 24px; }
  .long { height: 400px; background: linear-gradient(#eee, #ccc); }
  .wrap { overflow: hidden; }
  .row { display: flex; }
  .aside { position: sticky; top: 0; background: gold; height: 24px; width: 60px; }
  .row.fixed .aside { align-self: flex-start; }
  .aside.stretch { height: auto; }
</style>
<div class="scroller" id="s1"><div class="bar" id="b1">Works</div><div class="long"></div></div>
<div class="scroller" id="s2"><div class="wrap"><div class="bar" id="b2">Overflow ancestor</div><div class="long"></div></div></div>
<div class="scroller" id="s3"><div class="row"><div class="aside stretch" id="b3">Stretched</div><div class="long" style="flex:1"></div></div></div>
<div class="scroller" id="s4"><div class="row fixed"><div class="aside" id="b4">Fixed</div><div class="long" style="flex:1"></div></div></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Sticky needs a threshold such as top: 0.
  • An ancestor with overflow other than visible becomes the scroll container.
  • The element only moves inside its parent's box, so the parent must be taller.
  • In a flex row, set align-self: flex-start so the item is not stretched.
  • overflow: clip does not create a scroll container.

Common mistakes

  • setting position: sticky on a table row. Sticky on table parts depends on the browser and the table layout, so test it. Applying it to th cells is the usual approach.
  • giving sticky elements a top of 0 when a fixed header sits above. They stick under the header. Use the header height or a custom property for the offset.

Follow-ups

  • relative to what does sticky stick?
  • Relative to what does sticky stick?
  • How would you make a table header sticky?

Tips

  • Check for overflow on ancestors first. A single overflow-x: hidden added to stop a horizontal scrollbar is the usual culprit. overflow: clip does not create a scroll container and can replace it.
  • Present the five causes as a checklist in a fixed order, with overflow on ancestors first.
  • Mention the fixed header offset case at the end.