bootstrap modal removes scroll bar

JavascriptTwitter BootstrapModal Dialog

Javascript Problem Overview


When I trigger a modal view in my page it triggers the scroll bar to disappear. It's an annoying effect because the background page starts moving when the modal moves in / disappears. Is there a cure for that effect?

Javascript Solutions


Solution 1 - Javascript

This is a feature, class modal-open gets added to the HTML body when you show the modal, and removed when you hide it.

This makes the scrollbar disappear since the bootstrap css says

.modal-open {
    overflow: hidden;
}

You can override this by specifying

.modal-open {
    overflow: scroll;
}

in your own css.

Solution 2 - Javascript

I think that inherit is better than scroll because when you open modal, it will always open with scroll, but when you don't have any scrolls you will get the same problem. So I just do this:

.modal-open {
  overflow: inherit;
}

Solution 3 - Javascript

Its better to use overflow-y:scroll and remove padding from body, bootstrap modal added padding to page body.

.modal-open {
  overflow:hidden;
  overflow-y:scroll;
  padding-right:0 !important;
}

IE browser Compatible: IE browser doing same thing by default.

Solution 4 - Javascript

I used

.modal-open {
  overflow-y: scroll;
}

In this case you avoids to display the horizontal scroll bar

Solution 5 - Javascript

Thank God I came accross this post! I was pulling my hair out trying to figure how to get my scrollbars back when closing the window using my own close link. I tried the suggestions for CSS but it just wasnt working properly. After reading

> class modal-open gets added to the HTML body when you show the modal, > and removed when you hide it. -- @flup

I came up with a solution using jquery, so incase anyone else has the same issue and the above suggestions dont work --

<script>
            jQuery(document).ready(function () {
jQuery('.closeit').click(function () {
 jQuery('body').removeClass('modal-open');
                });
            });
        </script>

Solution 6 - Javascript

I was just playing with this 'feature' of Bootstrap modals. Seems the .modal class has overflow-y:auto; So the modal wrapper gets his own scrollbar when the modal itself becomes to high.

If you always want a scrollbar (designers often do) First set the body

body {
    overflow-y:scroll;
}

Then handle .modal-open

.modal-open .modal {
    overflow-y:scroll; /* Force a navbar on .modal */
}

.modal-open .topnavbar-wrapper {
    padding-right:17px; /* Noticed that right aligned navbar content got overlapped by the .modal scrollbar */
}

Leave scrollbar disabling on body untouched in this case

All other answers on this page kept making my content jump.

Heads up!

Allthough this solution worked for me all the time, yesterday I had a problem using this fix when the modal is draggable and to large to fit the screen (vertically). It might have something to do with position:sticky elements I added?

Solution 7 - Javascript

The only answer that worked for me is the following:

.modal-open {
  padding-right: 0 !important;
}
html {
  overflow-y: scroll !important;
}

Solution 8 - Javascript

Better if you will create your own css file to customize a certain part of your bootsrap.

Do not Alter the css file of bootstrap because it will change everything in your bootstrap once you use it in other parts of your page.

If your page is somewhat long, it will automatically provide a scroll bar. And when the modal opened, it will hide the scroll bar because that's what bootstrap do as a default.

To avoid hiding it when modal is opened, just override it by putting the css code (below) on your own css file.

.modal-open {
    overflow: scroll;
}

just a vice versa...

.modal-open {
    overflow: hidden;
}

Solution 9 - Javascript

Additionally, if modal popup needs to scroll with content inside and the parent needs to remain still, add the following to your Custom.css (overriding css)

.modal-body {
    max-height: calc(100vh - 210px);
    overflow-y: auto;
}
.modal-open {
    overflow: hidden;
    overflow-y: scroll;
    padding-right: 0 !important;
}

Solution 10 - Javascript

the bootstrap modal adds a padding once it opens so you can try this code in your own css

.modal-open {
    padding: 0 !important;
}

Solution 11 - Javascript

This is probably caused that 1st modal (when closed) remove modal-open class from body element and second one wont add it back when trigger modal-open.

In this case I would use event to check if modal has been closed/hidden fully and open another

let modal1 = $('.modal1);
let modal2 = $('.modal2);
$modal1.on('hidden.bs.modal', function (e) {
   $modal2.modal('show');
});

This will wait till 1st modal is closed to make sure modal-open is removed from body and re-added it when open.

Solution 12 - Javascript

I just had this problem myself, and finding this question quickly helped me understand what was causing the behaviour. So thanks.

However, I find these CSS workarounds a bit inelegant. Unless, that is, you specifically want to keep the scrollbars (although I can't think of a reason for that).

Essentially Bootstrap is applying padding-right to certain elements to compensate for removing the scrollbar.

Therefore, all you need to do is to put an extra wrapper with zero padding-right around your problematic element (and move the class that's being targeted up to it).

i.e. change from this...

<div class="fixed-top p-1 bg-dark">
    ...this content will move as padding will be modified...
</div>

... to this...

<div class="fixed-top p-0 bg-dark">
    <div class="p-1">
        ...this content won't move...
    </div>
</div>

Solution 13 - Javascript

In my case, bootstrap adds margin-left: -17px to my sidebar element by opening the modal. My solution on this, is to add m-0 class to sidebar.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEl DudeView Question on Stackoverflow
Solution 1 - JavascriptflupView Answer on Stackoverflow
Solution 2 - JavascriptAlisson AlvarengaView Answer on Stackoverflow
Solution 3 - JavascriptdevView Answer on Stackoverflow
Solution 4 - JavascriptMauroView Answer on Stackoverflow
Solution 5 - Javascript730wavyView Answer on Stackoverflow
Solution 6 - JavascriptBrainfeederView Answer on Stackoverflow
Solution 7 - JavascriptMartijn HiemstraView Answer on Stackoverflow
Solution 8 - JavascriptNormanCabilatazanView Answer on Stackoverflow
Solution 9 - JavascriptOracular ManView Answer on Stackoverflow
Solution 10 - JavascriptmehdigricheView Answer on Stackoverflow
Solution 11 - Javascriptuser1508136View Answer on Stackoverflow
Solution 12 - JavascriptMattView Answer on Stackoverflow
Solution 13 - JavascriptAsef HossiniView Answer on Stackoverflow