Prevent scrollbar jump

With modal components, it’s common/expected to prevent the background from scrolling.
/** * Prevent background scrolling with a modal dialog is open */ html:has(dialog[open]:modal) { overflow: hidden; } However, if your page was long enough that there were visible scrollbars, they disappear when you set overflow to hidden, which causes the layout to shift horizontally.
Sometimes it’s hard to see behind the modal backdrop, but it’s often visible and a bit jarring.


This content originally appeared on Go Make Things and was authored by Go Make Things

With modal components, it’s common/expected to prevent the background from scrolling.

/**
 * Prevent background scrolling with a modal dialog is open
 */
html:has(dialog[open]:modal) {
	overflow: hidden;
}

However, if your page was long enough that there were visible scrollbars, they disappear when you set overflow to hidden, which causes the layout to shift horizontally.

Sometimes it’s hard to see behind the modal backdrop, but it’s often visible and a bit jarring.

Here’s an example on CodePen.

The scrollbar-gutter CSS property tells the browser to reserve space for a scrollbar, even when one isn’t visible. It can be used to prevent that visual shift.

A value of stable reserves space in the default scrollbar position. A value of stable both-edges holds space on both sides of the viewport. And a value of auto is like not having the property at all.

/**
 * Hold space for scrollbar to prevent jumping on pages.
 */
html {
	scrollbar-gutter: stable;
}

Here’s an updated demo with scrollbar-gutter implemented. You can also see it in action with Kelp’s modal dialogs and off-canvas drawers.

The scrollbar-gutter property is now a baseline feature, and time of writing.

People still using older browsers will see the jump, but I think this is a nice progressive enhancement, with an acceptable default behavior for unsupported browsers.

Like this? A Lean Web Club membership is the best way to support my work and help me create more free content.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2025-09-17T14:30:00+00:00) Prevent scrollbar jump. Retrieved from https://www.scien.cx/2025/09/17/prevent-scrollbar-jump/

MLA
" » Prevent scrollbar jump." Go Make Things | Sciencx - Wednesday September 17, 2025, https://www.scien.cx/2025/09/17/prevent-scrollbar-jump/
HARVARD
Go Make Things | Sciencx Wednesday September 17, 2025 » Prevent scrollbar jump., viewed ,<https://www.scien.cx/2025/09/17/prevent-scrollbar-jump/>
VANCOUVER
Go Make Things | Sciencx - » Prevent scrollbar jump. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/17/prevent-scrollbar-jump/
CHICAGO
" » Prevent scrollbar jump." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2025/09/17/prevent-scrollbar-jump/
IEEE
" » Prevent scrollbar jump." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2025/09/17/prevent-scrollbar-jump/. [Accessed: ]
rf:citation
» Prevent scrollbar jump | Go Make Things | Sciencx | https://www.scien.cx/2025/09/17/prevent-scrollbar-jump/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.