Solution 1: You can declare .modal{ overflow-y:auto}
or .modal-open .modal{ overflow-y:auto}
if you are using below 3v of bootstrap (for upper versions it is already declared).
Bootstrap adds modal-open
class to body
in order to remove scrollbars in case modal is shown, but does not add any class to html
which also can have scrollbars, as a result the scrollbar of html
sometimes can be visible too, to remove it you have to set modal show/hide events and add/remove overflow:hidden
on html
. Here how to do this.
$('.modal').on('hidden.bs.modal', function () {
$('html').css('overflow','auto');
}).on('shown.bs.modal', function () {
$('html').css('overflow','hidden');
});
Solution 2: As modal has functionality keys, the best way to handle this is to fix height of or even better connect the height of modal with height of the viewport like this -
.modal-body {
overflow:auto;
max-height: 65vh;
}
With this method you also do not have to handle body
and html
scrollbars.
Note 1: Browser support for vh
units.
Note 2: As it is proposed above. If you change .modal{position:fixed}
to .modal{position:absolute}
, but in case page has more height than modal user can scroll too much up and modal will disappear from viewport, this is not good for user experience.