By the end of this lesson you’ll be able to lock page scrolling while the Webflow nav menu is open — a small but important UX fix — using a single line of CSS and no JavaScript at all. It’s the kind of tidy solution that makes a mobile menu feel finished instead of janky.
The problem is familiar: you open the nav menu on mobile, but the page behind it still scrolls, which feels sloppy and lets people lose their place. The usual reflex is to reach for a script. You don’t need one — Webflow already gives you everything you need.
The key is a class Webflow adds for you automatically.
How it works
When you open a Webflow nav menu, Webflow adds a w--open combo class to the menu button (its base class is w-nav-button), and removes it when the menu closes. That toggling class is a perfect state signal — and modern CSS can read it.
The :has() selector lets a parent style itself based on what it contains. So we tell the body: “whenever you contain a nav button that’s currently open, hide your overflow.” That’s the entire trick — a state that already exists, read by a selector that already exists:
body:has(.w-nav-button.w--open) {
overflow: hidden;
}
While the menu is open, the body can’t scroll; the moment it closes, w--open disappears and scrolling comes right back. No interactions, no library, no code of your own to maintain.
One thing to plan is scope, which depends on where you paste the rule. Drop it in your Global Styles component or a single page’s head (Page Settings) and it affects only that page. Paste it in the head code of the Custom Code tab under Site Settings and it applies everywhere. Then publish and test on the live link — the w--open class toggles on the real site.
How to use it
- Copy the rule —
body:has(.w-nav-button.w--open) { overflow: hidden }. - Pick your scope — for one page, use your Global Styles component or the page’s head; for the whole site, use the head code in Site Settings → Custom Code.
- Paste and publish — add the CSS in your chosen spot and publish the site.
- Test on the live page — open the nav menu and confirm the page can’t scroll, then close it and confirm scrolling returns.
That’s it — one clean CSS rule instead of a script, and a mobile menu that behaves exactly the way people expect.