Full answer
A common way to lock scrolling behind a modal is overflow: hidden on the body or html. On systems with classic scrollbars, which take space, the scrollbar disappears and the page gets wider by that amount, so all content jumps sideways by the width of the bar. On phones and systems with overlay scrollbars there is no jump.
Fixes:
scrollbar-gutter: stableon the scrolling element keeps room for the scrollbar even when it is not needed, so the width does not change when overflow changes. It is a newer property, so check the support table.scrollbar-gutter: stable both-edgesalso adds space on the other side so content stays centred.- Compensate with padding: measure the bar width (
window.innerWidth - document.documentElement.clientWidth) and add it as right padding while locked. This works everywhere, but needs script.
The other problem is scroll chaining. When a user scrolls the modal's own list to the end and keeps scrolling, the page behind starts to scroll. overscroll-behavior: contain on the inner scroll area stops the scroll from passing to the parent. overscroll-behavior: none also disables effects such as pull-to-refresh and bounce at the edge of that element. Use it with care, because it changes browser habits.
Remember that locking page scroll does not stop keyboard focus from reaching content behind the modal. Use inert or a native dialog for that. Also, a native dialog opened with showModal() lets you style body:has(dialog[open]) for the lock.
The computed values show that the gutter is reserved on the page and that scroll chaining is contained in the panel. In a desktop browser with classic scrollbars, the layout width stays the same after the lock is added.
The test browser here hides scrollbars, so the width jump itself was not measured. Check it on a desktop browser with visible scrollbars.
<style>
html { scrollbar-gutter: stable; }
body.lock { overflow: hidden; }
.panel { height: 80px; overflow-y: auto; overscroll-behavior: contain; width: 150px; border: 1px solid #999; }
</style>
<div class="panel" id="p"><div style="height:300px">Long list</div></div>
<script>document.body.classList.add("lock");</script>The test browser hides scrollbars, so the width jump was not measured. The check read the computed values only. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- overflow: hidden removes the scrollbar and shifts the layout.
- scrollbar-gutter: stable reserves the space.
- overscroll-behavior: contain stops scroll chaining.
- Scroll lock does not trap focus.
- Check on a desktop browser with visible scrollbars.
Common mistakes
- Using overflow: hidden on the body and ignoring the layout jump on desktop.
- Using overscroll-behavior: none everywhere, which removes useful browser behaviour.
- Thinking that a scroll lock also traps keyboard focus.
Follow-ups
- What does scroll chaining mean?
- Why is a scroll lock not a focus trap?
Tips
- Explain why the page jumps: the scrollbar width disappears.
- Name scrollbar-gutter and overscroll-behavior, and say what each solves.