Skip to content
Tutorials Center
My learning

Topics

Lesson 67 of 139 ยท Layout Fundamentals

Sticky Headers, Scroll Containers and Scrollbar Styling

Fix position: sticky that does not stick, control scroll containers with scrollbar-gutter and overscroll-behavior, and style scrollbars with modern CSS.

position: sticky looks like the easiest positioning value: write top: 0 and a header stays visible while the page scrolls. Then you try it on a real page and nothing happens. Sticky has more preconditions than any other value, and nearly all of them are about scroll containers, the boxes that scroll their own content.

This lesson explains how sticky decides where to stick, the short checklist that fixes it when it does not, and three related tools for scrollable areas: scrollbar-gutter to stop layout jumps, overscroll-behavior to stop scroll chaining, and scrollbar-width plus scrollbar-color to style the scrollbar itself. If you have not met the basics, read Positioning and Overflow first.

How sticky works

A sticky element is laid out in normal flow, like position: relative. The browser then watches the nearest scrolling ancestor (the nearest ancestor whose overflow is not visible, or the viewport if there is none). When scrolling would push the element past the offset you gave (top, bottom, left or right), the browser holds it at that offset instead.

Two details matter:

  • The offset is measured against the scrollport of that ancestor, not against the page.
  • The element can never leave its parent box. It sticks while the parent is in view, and it scrolls away together with the end of the parent.
flowchart TD
  A["Scroll container (overflow: auto)"] --> B["Parent section (the sticking range)"]
  B --> C["Sticky heading (top: 0)"]
  B --> D["Long content"]

The heading sticks to the scroll container's top edge, but only within its parent section.

Try it. Scroll the box and watch each heading stay put until the next section pushes it out.

Section headings that stick

Every h2 has its own parent section, so each heading sticks only while its section is visible. That is the pattern behind alphabetical contact lists and table-of-contents panels.

Why sticky does not stick: the checklist

When a sticky element behaves like a normal one, go through these causes in order. Together they explain almost every report.

  1. No offset. position: sticky alone does nothing. You need at least one of top, right, bottom, left.
  2. A clipping ancestor. If any ancestor between the element and the scroller has overflow: hidden, auto or scroll, that ancestor becomes the scroll container, and it probably does not scroll. The sticky element sticks to a box that never moves. The usual culprit is overflow-x: hidden added to a wrapper to hide a horizontal scrollbar. Setting one axis to hidden forces the other to auto.
  3. A parent that is too small. The element cannot leave its parent. If the parent is only as tall as the sticky element, there is no distance to travel.
  4. Flex or grid stretching. In a flex row, items stretch to the row height by default, so a sticky sidebar fills its whole column and has nowhere to move. Add align-self: start.
  5. Stacking. A sticky header with no z-index may slide under positioned content below it. Give it a z-index (see z-index).
  6. Table quirks. In tables, sticky works on th and td cells, and on thead in modern browsers, but not reliably on tr. Put it on the cells.

The fourth cause is worth seeing on its own. The sidebar below sticks only because of align-self: start. Delete that line in the editor and the sidebar stops moving.

A sticky sidebar in a flex layout

When debugging, walk up from the sticky element and look at the computed overflow of every ancestor in your browser's dev tools. The first one that is not visible is the box your element sticks to.

A sticky header covers the top of the viewport, so a link to #section scrolls the target under the header. Fix it on the targets with scroll-margin-top, or for the whole page with scroll-padding-top on the scroll container (usually html). Use the header height, or a custom property that holds it:

CSS
:root { --header-h: 4rem; }
html { scroll-padding-top: var(--header-h); }
.site-header { position: sticky; top: 0; height: var(--header-h); }

This is also the idiom that Scroll Snap uses to keep snapped items clear of fixed UI.

scrollbar-gutter: no more layout jumps

Classic scrollbars take space from the content box. When content grows and a vertical scrollbar appears, everything shifts sideways by 15 pixels or so. Opening a modal and hiding the page scrollbar causes the same jump in reverse.

scrollbar-gutter reserves the space in advance:

  • auto: the default. Space is used only while a scrollbar is shown.
  • stable: space for the scrollbar is always reserved on the scrolling box, even when it does not scroll.
  • stable both-edges: reserves the space on both sides, so centered content stays symmetric.

It only has an effect with classic scrollbars that take up space. Overlay scrollbars (the default on most phones and macOS) take no room, so the property changes nothing there.

Reserving scrollbar space

If your system uses overlay scrollbars, both boxes look the same. On Windows and most Linux desktops, the second box has a visible empty strip on the right: that is the reserved gutter.

overscroll-behavior: stopping scroll chaining

When you scroll to the end of an inner box and keep scrolling, the leftover scroll chains to the parent, and the whole page moves. That is annoying inside a chat panel, a dropdown or a modal. It also triggers pull-to-refresh on phones.

overscroll-behavior controls it on the scroll container:

Value Effect
auto Default. Scroll chains to the parent and shows bounce effects.
contain No chaining to the parent. Local effects such as bounce may still show.
none No chaining and no bounce or glow effects.

Set overscroll-behavior: contain on the scrollable panel and the page behind it stays still. There are also overscroll-behavior-x and overscroll-behavior-y for one axis. Note that it only matters on boxes that really scroll, so the box needs a fixed height (or max-height) and overflow: auto.

Containing scroll chaining

Scroll to the bottom of the panel and keep spinning the wheel. With contain, the stripes behind it do not move. With auto, they do.

Styling the scrollbar

Two standard properties style the scrollbar without hacks:

  • scrollbar-width: auto | thin | none sets the thickness. none hides the bar but keeps scrolling working. Use none sparingly, because users lose the visual hint that content continues.
  • scrollbar-color: <thumb> <track> sets the two colors.

Both are inherited, so you can set them on html to restyle the page scrollbar, or on one component.

CSS
.log-view {
  overflow: auto;
  scrollbar-width: thin;
  scrollbar-color: #6366f1 #e0e7ff;
}

Older WebKit-only ::-webkit-scrollbar pseudo-elements allow rounded thumbs and arrows, but they are non-standard, and once you use them the standard properties are ignored in Chromium. Prefer the standard pair, and keep enough contrast between thumb and track that the control stays visible. Never rely on a styled scrollbar as the only sign that content scrolls.

A thin colored scrollbar

The tabindex="0" lets keyboard users focus the box and scroll it with the arrow keys, an accessibility detail worth adding to every scrollable region without focusable content. See Accessibility for more.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A header has position: sticky but scrolls away like a normal element. It has no top value. What is the problem?

    Sticky needs at least one of top, right, bottom or left to know where to hold the element. Without an offset it behaves like position: relative.
  2. 2. Sticky stops working after you add overflow-x: hidden to a wrapper around the element. Why?

    A non-visible overflow value turns the wrapper into a scroll container. The element sticks to that wrapper, which usually does not scroll, instead of the page.
  3. 3. What does scrollbar-gutter: stable do?

    stable keeps room for a classic scrollbar even when none is shown, so content does not jump when one appears.
  4. 4. Which declaration stops the page behind a scrollable panel from moving when the panel reaches its end?

    overscroll-behavior: contain prevents scroll chaining to the parent. The panel itself must still be a real scroll container.

Summary

  • Sticky needs an offset, a scroll container that really scrolls, and a parent tall enough to travel in.
  • An overflow value other than visible on any ancestor changes which box the element sticks to.
  • In flex and grid layouts, add align-self: start so sticky items do not stretch.
  • Use scroll-padding-top or scroll-margin-top so anchor links clear a sticky header.
  • scrollbar-gutter: stable prevents layout shift, overscroll-behavior: contain stops scroll chaining, and scrollbar-width with scrollbar-color style the bar.

Next: overscroll-behavior: Stopping Scroll Chaining.