bootstrap modal removes scroll bar
Asked Answered
L

15

117

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?

Luncheon answered 31/7, 2014 at 22:44 Comment(0)
E
142

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.

Elexa answered 31/7, 2014 at 22:45 Comment(7)
can you please be more specific?Luncheon
Added some more details. Better now?Elexa
It's more appropriate to use overflow-y:scroll, which I think better addresses the intent of the OPs question. Using overflow:scroll will add a horizontal scrollbar to the bottom of the screen.Loci
perfect, works with angular-strap modals as well, can simply include the above CSS in a <style></style> inside the templateUrl so it not inflict on other modals in the app.Fivefold
@Scott, Thanks for that extra tip. I was wondering where that horizontal scrollbar was coming from.Prem
Use overflow: inherit; instead. This way the modal don't remove scroll when you have scroll and don't add scroll when you don't haveCence
Experienced same issue and however had to apply CSS to .modal class instead .modal-openPublish
C
42

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;
}
Cence answered 15/11, 2016 at 18:50 Comment(2)
This is the real answer. overflow scroll still effect some element in my case.Uam
Experienced same issue and however had to apply CSS to .modal class instead .modal-openPublish
W
30

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.

Weldonwelfare answered 14/12, 2017 at 7:5 Comment(3)
Thx. Additionally, if modal popup needs to scroll and the parent needs to remain still .modal-body { max-height: calc(100vh - 210px); overflow-y: auto; } .modal-open { overflow: hidden; overflow-y: scroll; padding-right: 0 !important; }Ola
The padding-right: 0 !important; is indeed very important. In my case, even if the scrollbar is enabled it leaves behind an invisible scrollbar in the original page behind the modal and still moves the page to the left which is what I'm trying to remove. Thank you very much for this answer!Fundamental
Worked like a charm. Just as @YongPin said; when the modal popped up, an invisible scrollbar was still being shown behind it. the padding-right: 0 fixed it.Fawcette
B
26

I used

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

In this case you avoids to display the horizontal scroll bar

Brittain answered 10/6, 2015 at 9:41 Comment(1)
we could combine it with @Alisson Alvarenga 's answer and use overflow-y: inherit;Elmoelmore
N
11

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>
Nepheline answered 10/12, 2015 at 15:30 Comment(3)
I had an issue where I was overriding the hide.bs.modal event so I could execute some custom code when the modal was hidden, but what I didn't realize is that stopped the modal-open class from being removed from the body - hence my scroll bar didn't come back when closing this one specific modal. I'm not removing the modal-open class manually in my hide.bs.modal code and it fixes that issue.Dareen
You should just need to add a data-dismiss="modal" attribute to your link instead of doing anything custom like this. Bootstrap will do all the appropriate closing actions when that link is clicked.Whitmore
data-dismiss="modal" is not working in my case because I am calling a function in the angular controller. The jQuery solution suggested works very well for me.Viscountess
S
6

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?

Selfcontained answered 21/11, 2016 at 14:34 Comment(1)
When I've added body{overflow-y:scroll;}, my problem was solved. Thank you, sir.Iolite
M
4

The only answer that worked for me is the following:

.modal-open {
  padding-right: 0 !important;
}
html {
  overflow-y: scroll !important;
}
Mummery answered 7/9, 2020 at 18:12 Comment(1)
This is also the only answer that worked for me. Thank you!Biddick
O
2

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;
}
Ola answered 13/4, 2018 at 0:58 Comment(1)
Genius, this is the only thing that has worked for me on BS5. Thank youPriam
O
1

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;
}
Oeuvre answered 4/3, 2015 at 1:58 Comment(3)
This seems to contain the same information as the other answer on this question. If your answer is different, edit it to clarify what exactly is different. Please only add a new answer if you have something new.Thoroughbred
It contains same answer but have other way of explaining it. Sometimes, understanding depends on how it is explained.Oeuvre
Not all readers could get the same idea. Better to give other way of explaining it for them to understand clearly.Oeuvre
V
1

Just so PrimeNg users can get help here, PrimeNg does the same thing:

.p-overflow-hidden {
    overflow: hidden;
}

In the case of PrimeNg though, it can be disabled by disabling the modal property when opening the DynamicDialog; if that's appropriate for your use-case.

Otherwise, it requires the same fix as Bootstrap.

Vigil answered 16/10, 2022 at 22:46 Comment(0)
S
0

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

.modal-open {
    padding: 0 !important;
}
Sucker answered 21/2, 2018 at 11:6 Comment(0)
B
0

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.

Bootleg answered 13/12, 2019 at 12:20 Comment(0)
B
0

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>
Belcher answered 19/5, 2020 at 18:51 Comment(0)
M
0

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.

Monarda answered 16/11, 2021 at 8:2 Comment(1)
This answer worked for me. In my case the <nav> shifted right by 17px and there was padding added as well. I added me-0 and adjusted the padding to prevent the navbar from shifting.Quay
S
0

I ran into this issue in bootstrap 5 and just added the overflow-y-scroll class to the body.

Strep answered 22/6, 2023 at 0:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.