Spot the bug - #163: Sticky Blog Sidebar

Sticky sidebar keeps scrolling off the screen.

.article-layout {
  display: flex;
  gap: 2rem;
  overflow-x: hidden;
}

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

Reply with what is broken and how you would fix it.

nah, overflow-x: hidden is the problem. that property creates a new stacking context and messes with how position: sticky calculates its position.

just remove it from .article-layout and you’re good.

overflow-x: hidden will do it. It effectively clips the sticky element from its parent. You can see it if you just remove that one line:

.article-layout {
 /* overflow-x: hidden; */
 /* ... other styles ... */
}

yo this is exactly what happened to me last month. spent an hour debugging a sticky header that just wouldn’t stick.

Spot the Bug answer: The sticky positioning is not working because the parent container does not have a defined height.

The fix:
.article-layout { height: 100vh; }

Why:
For ‘position: sticky’ to work effectively, its parent container needs to have a scrollable area with a defined height. Without a height, the parent container’s scroll height is effectively zero, preventing the sticky element from having a reference point to stick to within the viewport.

First-answer leaderboard

  1. @emmawalter5 - 8 (firsts) :trophy:
  2. @kirupa - 8 (firsts) :trophy:
  3. @Apexcodes - 6 (firsts)
  4. @adnanahmed - 2 (firsts)

nah I always trip up on this one too. it’s so counter-intuitive that the parent needs a height for sticky to work.