CSS

CSS scrollbar-gutter: Stop Layout Shift When the Scrollbar Appears

A page is short, so there’s no scrollbar. You open a longer page, the scrollbar appears, and the whole layout jumps sideways by about 15 pixels. The same happens when a dropdown expands, a filter adds results, or a modal locks scrolling. When the jump happens after the page has loaded, it also counts toward your Cumulative Layout Shift (CLS) score.

One line of CSS fixes it: scrollbar-gutter: stable tells the browser to reserve the space for the scrollbar ahead of time, so the content width never changes.

The fix

Put it on the html element. That reserves the gutter for the page scrollbar on every page, whether it scrolls or not.

css
html {
  scrollbar-gutter: stable;
}

Demo:

Without scrollbar-gutter

Short list 3
Long list 24

With scrollbar-gutter: stable

Short list 3
Long list 24

The badge on the left jumps when the box gets a scrollbar. On the right, the space is already reserved, so both badges line up. If you are on a Mac with the default settings or on a phone, both sides look the same: those scrollbars float over the content and take no space, so there is no jump to fix.

Keep centered layouts centered: stable both-edges

stable reserves space on one side only, so a centered layout sits about 7 pixels left of the true center. both-edges reserves the same space on the opposite side too, keeping the content perfectly centered.

css
html {
  scrollbar-gutter: stable both-edges;
}

No jump when a modal locks scrolling

Locking the page with overflow: hidden while a modal is open removes the scrollbar, and the page behind the modal shifts right. With scrollbar-gutter: stable, the gutter stays reserved while scrolling is locked, so nothing moves. No more measuring the scrollbar in JavaScript and adding padding-right.

css
html {
  scrollbar-gutter: stable;
}

html.modal-open {
  overflow: hidden;
}

Scrollable boxes, too

It works on any element that scrolls: sidebars, chat windows, dropdowns, code panels. The element needs overflow set to auto, scroll or hidden. With the default overflow: visible, nothing is reserved.

css
.sidebar {
  height: 100vh;
  overflow-y: auto;
  scrollbar-gutter: stable;
}

Things to know

  • Use html, not body. The page scrollbar belongs to the root element, and scrollbar-gutter on body does nothing for it.
  • Overlay scrollbars are left alone. On macOS (default settings), iOS and Android, scrollbars float over the content, so the browser reserves nothing and your layout is unchanged.
  • Browser support: Chrome and Edge 94+, Firefox 97+, Safari 18.2+. Older browsers ignore the property and behave as before, so it’s safe to add today.

Want the opposite and no scrollbar at all? Here’s how to hide the scrollbar in CSS while keeping the content scrollable. For more small fixes like this one, see these CSS tips and tricks.

FAQ

How do I stop the layout from shifting when the scrollbar appears?
Add html { scrollbar-gutter: stable; }. The browser reserves the scrollbar’s space on every page, so the content width stays the same whether the page scrolls or not.

What’s the difference between stable and stable both-edges?
stable reserves space on the scrollbar’s side only. both-edges also reserves the same space on the opposite side, which keeps centered content centered.

Why doesn’t scrollbar-gutter work?
Usually because it’s on body instead of html, the element has overflow: visible, or your system uses overlay scrollbars (macOS default, phones), which never take up space.

Did you like this CSS code snippet? Share it with your friends:
Want to learn more? Ask your AI: