Code Reference

Sticky Sidebar

CSS · Example / how-to

CSS · Example / how-to


📋 Overview

Two-column docs layout: scrolling main column, sticky "On this page" sidebar.

🔧 Core concepts

PropertyRole
position: stickyStick within parent
topOffset under header
Grid/flexColumn structure

💡 Examples

.docs {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 14rem;
  gap: 2rem;
}

.toc {
  position: sticky;
  top: 5rem;
  align-self: start;
  max-height: calc(100vh - 6rem);
  overflow: auto;
}

@media (max-width: 800px) {
  .docs { grid-template-columns: 1fr; }
  .toc { position: static; max-height: none; }
}

⚠️ Pitfalls

  • Sticky fails if any ancestor has overflow: hidden.
  • Parent must be taller than the sticky element.

On this page