Need to prevent page scrolling when a menu or popup is open? Here’s a quick and easy way to lock and unlock scrolling in your Webflow project using just a few lines of custom code. This technique is especially helpful when creating custom menus, overlays, or modals where you want to restrict user interaction with the background content.
This scroll-lock trick disables page scrolling by toggling the overflow: hidden
style on the <html>
element. With one click, you can lock or unlock scrolling dynamically using a trigger element—like a menu toggle or modal close button.
Want to test it out before adding it to your project?
Paste the following code into your Project Settings → Custom Code → Before </body>
tag, or into the Page Settings if you only need it on specific pages.Add the code into your Before </body> tag in custom code settings.
<style>
/* Lock Webflow Scrolling CSS */
html.no-scroll {
overflow: hidden;
}
</style>
<!-- Lock Webflow Scrolling Script -->
<script type = "text/javascript" > $('.lock-scroll').on('click', toggleScrolling);
function toggleScrolling() {
$('html').toggleClass('no-scroll');
}
</script>
💡 This code listens for clicks on any element with the .lock-scroll
class. When clicked, it toggles the no-scroll
class on the <html>
element, which prevents the page from scrolling.
.lock-scroll
Class to Your Trigger ElementNow, add the class lock-scroll
to any element you want to use as a toggle—for example, your mobile menu button or overlay close button.
Once published, test the feature on the live site to confirm it’s working as expected.
Want more control? You can extend the code to:
Let us know if you'd like a more advanced version!
We love supporting the Webflow community, and your support means the world to us. If this helped you:
Thanks for being awesome. Keep building! 💪