Prevent Body Scrolling

By  on  

One of my pet peeves with fixed or absolute positioned elements is the <body> scrolling while you scroll the the positioned element.  Have you ever tried scrolling a dialog and seeing the page scroll in the background?  Awful user experience, bordering on embarrassing.  Yikes.

So what's the best way to prevent the <body> scrolling in the background?  Pass on scroll events and preventDefault or stopPropagation, that wont work.  The easiest way is a simple CSS snippet:

body.noScroll { /* ...or body.dialogShowing */
  overflow: hidden;
}

Preventing overflow on the entire <body> assures scrolling on elements other than the desired fixed or absolute element wont happen.  It's an easy way to freeze the page for a hovered focus element.

This trick has been used forever -- make sure you keep it in your toolbox!

Recent Features

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

  • By
    Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

Incredible Demos

  • By
    Animated AJAX Record Deletion Using Dojo

    I'm a huge fan of WordPress' method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here's how to achieve that functionality with Dojo JavaScript. The PHP - Content & Header The following snippet goes at the...

  • By
    MooTools ASCII Art

    I didn't realize that I truly was a nerd until I could admit to myself that ASCII art was better than the pieces Picasso, Monet, or Van Gogh could create.  ASCII art is unmatched in its beauty, simplicity, and ... OK, well, I'm being ridiculous;  ASCII...

Discussion

  1. MaxArt

    Keep in mind that overflow: hidden also hides the scrollbar. Usually not a problem on mobile, but on desktop it creates an annoying effect because it resizes the viewport.

    • David

      Except when you are on Mac, there the scrollbar lays above the page content and simple fades in and out without actually affecting the available width. Long live consistency…

    • Shawn D

      True, unless you have ‘Always Show Scrollbars’ turned on in general prefs or are using an external mouse

  2. Daniel

    Bootstrap uses this technique in theirs modals, but this is not very well supported on mobile devices:
    http://v4-alpha.getbootstrap.com/getting-started/browsers-devices/#modals-navbars-and-virtual-keyboards

  3. Onairda

    How would you make the body fixed and the modal window scrollable in case its content exceeds the window size?

    Applying the overflow: hidden on body will prevent the whole page to scroll.

  4. Add overflow: hidden; property on body tag and set position fixed of inner element.

  5. Guest

    In 80% of cases, modal or whatever, will be called when window is somewhat scrolled from top of the page.

    overflow: hidden;

    on body will instantly fix window on top of the page, and again, you’ll have nasty effect.

    • Anna

      This is the problem I’m encountering, and have had no luck in finding a solution! :(

  6. Adam

    In some cases (eg. React), you don’t want to change ‘external world’ state.

    Is there any way to prevent body from scrolling from the level of some element that is supposed to stop body scrolling without touching body element??

  7. Luca

    Awesome

  8. leny

    also in safari(mobile) the body keeps scrolling.

  9. @Adam, you can use something like https://github.com/iest/react-body-classname with React (it’s fine to change ‘external world’ state when needed).

  10. Oliver

    This works for desktop but not for mobile. Is there a solution?

  11. willmcpo

    Check out https://www.npmjs.com/package/body-scroll-lock.

    Locks body scroll without locking scroll of a target element (eg. Modal) for iOS and other devices, as well as desktop browsers.

    Yes works for iOS.

    Here’s a demo: http://wp-os.s3-website-ap-southeast-2.amazonaws.com/body-scroll-lock-demo/index.html.

    You can read more about the rationale of the package at https://medium.com/jsdownunder/locking-body-scroll-for-all-devices-22def9615177.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!