Ronalds Vilciņš

Why overflow: hidden Can Break Sticky Positioning

A sticky heading can stop following the viewport after an ancestor gets overflow: hidden. The heading’s top value is still there. The sticky heading now follows a different scroll container.

Before adding another positioning rule, inspect the ancestors.

Change the ancestor that sticky follows
View code
HTML
<label>Wrapper overflow <select id="overflow"><option value="hidden">hidden</option><option value="clip">clip</option></select></label>
<button id="scroll" type="button">Scroll outer panel</button>
<div class="scroller" tabindex="0" role="region" aria-label="Outer scrolling panel"><p class="lead">The wrapper starts below this text.</p><div class="wrapper"><h2 class="sticky">Reading notes</h2><div class="content"><p>Keep scrolling the outer panel.</p></div></div></div>
<p class="note">With hidden, the wrapper captures the sticky scrolling context. With clip, the heading can stick to the outer panel.</p>
CSS
button, select { padding: .35rem .6rem; border: 1px solid var(--clg); background: var(--cw); color: var(--cb); }
input[type="range"] { width: min(100%, 18rem); vertical-align: middle; }
h2, h3 { font-size: 1rem; }
.note { color: var(--clg); }

button { margin-top: .5rem; }
.scroller { height: 12rem; overflow: auto; margin-top: 1rem; border: 1px solid var(--clg); }
.lead { height: 4rem; padding-inline: 1rem; }
.wrapper { overflow: hidden; }
.sticky { position: sticky; top: 0; margin: 0; padding: .75rem; background: var(--cg); }
.content { min-height: 32rem; padding: 1rem; }
JavaScript
const scroller = document.querySelector('.scroller');
document.querySelector('#overflow').addEventListener('change', event => {
  document.querySelector('.wrapper').style.overflow = event.target.value;
  scroller.scrollTop = 0;
});
document.querySelector('#scroll').addEventListener('click', () => {
  scroller.scrollTop = scroller.scrollTop > 0 ? 0 : 140;
});

The wrapper changes what sticky follows

Try this on a page tall enough to scroll:

<div class="wrapper">
  <h2 class="heading">Reading notes</h2>
  <div class="long-content">Scroll the page</div>
</div>
.wrapper {
  overflow: hidden;
}

.heading {
  position: sticky;
  top: 0;
  background: white;
}

.long-content {
  min-height: 150vh;
}

overflow: hidden creates a scroll container, even though it does not show scrollbars. A sticky element uses its nearest ancestor with a scrolling mechanism. If that ancestor never actually scrolls, scrolling the document may not produce the effect you expected.

Also check the wrapper’s height. Sticky positioning remains constrained by its containing block; it cannot follow you indefinitely beyond that block.

Clip without creating a scroll container

If the wrapper only needs to clip decoration, try:

.wrapper {
  overflow: clip;
}

Unlike hidden, clip does not allow scrolling, including programmatic scrolling. It also does not create a new formatting context by itself. Add display: flow-root separately if you need that layout behaviour. MDN’s overflow reference describes the differences.

Check both axes: overflow-x: hidden combined with the other axis’s default can produce a computed auto value there. The authored rule alone does not tell the whole story.

Moving the clipping to a decorative child may be a cleaner fix than changing the wrapper. Keep the sticky element outside the clipping box when the two behaviours have unrelated jobs.