CSS position sticky explained with practical examples

CSS position: sticky, with practical examples

A header that stays at the top while scrolling, a sidebar that follows the reader, a table header that remains visible over a long table: position: sticky does all of these without JavaScript. An element with it behaves normally until it reaches a given offset from the edge of the viewport, then stays there until its parent ends. When it does not work, the reason is almost always one of three things, all explained below.

Problem: The header, the sidebar and a table header should stay visible while the page scrolls, without scroll listeners in JavaScript.

Solution: Give the element position: sticky and an offset (top: 0), make sure no ancestor has overflow: hidden or overflow: auto, and in flex or grid layouts keep the element from stretching to the full height of its parent with align-self: start.

A sticky header

.site-header {
    position: sticky;
    top: 0;
    z-index: 100;
    background: #fff;
}

/* WordPress admin bar: logged-in users see it above the header */
.admin-bar .site-header {
    top: 32px;
}

@media (max-width: 782px) {
    .admin-bar .site-header {
        top: 46px;
    }
}

A background is needed, otherwise the content scrolls visibly behind the header. z-index keeps it above elements that create their own stacking context.

A sticky sidebar

.content-wrapper {
    display: grid;
    grid-template-columns: 1fr 300px;
    gap: 2.5rem;
}

.sidebar__inner {
    position: sticky;
    top: 1.5rem;
    align-self: start;               /* do not stretch to the full row height */
    max-height: calc(100vh - 3rem);
    overflow-y: auto;                /* scroll inside if the sidebar is taller than the screen */
}

In a grid or flex row, items are stretched to the height of the row by default. A sidebar that is as tall as the content has no room to move and never sticks; align-self: start fixes it. The max-height with its own scroll prevents the bottom of a long sidebar from being unreachable.

A sticky table header

.table-wrapper {
    max-height: 70vh;
    overflow: auto;                  /* the table scrolls inside this box */
}

.table-wrapper thead th {
    position: sticky;
    top: 0;
    background: #f8fafc;
}

Here the scrolling container is the wrapper, not the page, and the header sticks to the top of the wrapper. Put position: sticky on the th cells: support on thead and tr elements was inconsistent in older browsers.

Why it does not stick

  1. An ancestor has overflow: hidden, auto or scroll. The element then sticks inside that ancestor, which often does not scroll at all. Themes set overflow-x: hidden on a wrapper to hide horizontal scrollbars, and that silently breaks every sticky element inside. overflow: clip (Chrome 90+ and Firefox 81+; Safari support is announced for version 16) hides the overflow without creating a scroll container.
  2. No offset is set. Without top (or bottom) the element never sticks.
  3. The parent ends. A sticky element stays inside its parent; when the parent scrolls out of view, the element goes with it. That is by design: a sticky element in a short section only sticks for the length of that section.

NOTE: A sticky header takes vertical space on small screens, where space is scarce. Keep it compact on phones, or make only a slim bar sticky, so the content is not reduced to a strip between a fixed header and the browser’s own toolbar.