Comment by simonw

17 days ago

This is a neat piece of modern CSS:

  body:has(dialog[open]) {
    overflow: hidden;
  }

https://caniuse.com/css-has confirms the has() selector has had widespread browser support since December 2023.

YMMV / be careful with this, body:has() and html:has() can be extremely expensive (and introduce severe lag visible to the user) if you have dynamic components on the page that are constantly altering the DOM (ex. react/vue apps)

Inert should be used instead of overflow. Achieves the same thing but is also compliant with accessibility in a way overflow isn’t

I used this same approach in a recent web app and it worked great. You can also use scrollbar-gutter: stable, which disables scrolling but maintains the preserved space to avoid content reflows.