Sticky Sidebar
A sidebar that sticks to the viewport while scrolling using position: sticky.
Published June 2, 2026
Demo
HTML
<div class="layout">
<nav class="sticky-nav">
<!-- nav links -->
</nav>
<main class="content">
<!-- scrollable content -->
</main>
</div>
CSS
.layout {
display: grid;
grid-template-columns: 180px 1fr;
height: 260px;
overflow-y: auto;
}
.sticky-nav {
position: sticky;
top: 0;
align-self: start;
height: fit-content;
}
How it works
position: sticky places an element in normal flow but makes it stick to a specified position when it would otherwise scroll off. The sidebar uses position: sticky; top: 0 inside a scrollable container. The key requirement is that the scrolling ancestor must have a finite height — here the layout wrapper uses overflow-y: auto and a fixed height. align-self: start is needed on the sidebar so it doesn't stretch to the grid row height.