Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent background scroll when bootstrap modal is open

I'm using Bootstrap v3.0.2. Want to disable background scrolling when a modal is open. I tried:

.modal-open {
  overflow: hidden;
}

But it isn't working. I found a solution to this problem is:

.modal-open {
    overflow: hidden;
    position:fixed;
    width: 100%;
}

But position: fixed; causing an extra white-space at the bottom of the page in chrome(less than 100% view) and also for large displays(in 100% view) while opening and closing the modal. How to get rid of it? (My modal contains scroll-able fields)

like image 815
fatCop Avatar asked Feb 15 '15 09:02

fatCop


People also ask

How Do I Stop background scroll when modal is open?

Approach: A simple solution to this problem is to set the value of the “overflow” property of the body element to “hidden” whenever the modal is opened, which disables the scroll on the selected element.

How do you stop background scrolling in CSS?

Disabling scroll with only CSS There's another way to disable scrolling that is commonly used when opening modals or scrollable floating elements. And it is simply by adding the CSS property overflow: hidden; on the element you want to prevent the scroll.

What prevents the body from scrolling when overlay is on?

Approach: To prevent body scrolling but allow overlay scrolling we have to switch the overflow to be hidden when the overlay is being shown. And while the whole page's overflow is hidden or the scroll is disabled the overflow in the y-axis of the overlay is enabled and the overflow can be scrolled.


2 Answers

Use height: 100% to make the .modal-open fit the whole screen. Eventually use

top: 0;
left: 0;
right: 0;
bottom: 0;
like image 148
QUB3X Avatar answered Oct 09 '22 01:10

QUB3X


incase anyone having this still, just use

.modal-open {
    overflow: hidden !important;
}

EDIT: have no idea why its de-voted but this is what fixed my issue regarding the scrolling.

like image 26
ctf0 Avatar answered Oct 09 '22 00:10

ctf0