I am trying to make a modal with a body that will scroll when the content becomes too large. However, I want the modal to be responsive to the screen size. When I set the max-height to 40% it has no effect. However, if I set the max-height to 400px it works as expected, but is not responsive. I am sure I am just missing something simple, but I can't seem to get it to work.
Here is an example
Doesn't work:
.modal-body {
max-height:40%;
overflow-y: auto;
}
Works:
.modal-body {
max-height:400px;
overflow-y: auto;
}
This worked for me
Fiddle: http://jsfiddle.net/mehmetatas/18dpgqpb/2/
You've no doubt solved this by now or decided to do something different, but as it has not been answered & I stumbled across this when looking for something similar I thought I'd share my method.
I've taken to using two div sets. One has hidden-xs and is for sm, md & lg device viewing. The other has hidden-sm, -md, -lg and is only for mobile. Now I have a lot more control over the display in my CSS.
You can see a rough idea in this js fiddle where I set the footer and buttons to be smaller when the resolution is of the -xs size.
This should work for everyone, any screen resolutions:
First, count your modal header and footer height, in my case I have
H4
heading so I have them on141px
, already counted default modal margin in20px(top+bottom)
.So that subtract
141px
is themax-height
for my modal height, for the better result there are both border top and bottom by1px
, for this,143px
will work perfectly.In some case of styling you may like to use
overflow-y: auto;
instead ofoverflow-y: scroll;
, try it.Try it, and you get the best result in both computer or mobile devices. If you have a heading larger than
H4
, recount it see how muchpx
you would like to subtract.If you don't know what I am telling, just change the number of
143px
, see what is the best result for your case.Last, I'd suggest have it an inline CSS.
The
scss
solution forBootstrap 4.0
.modal { max-height: 100vh; .modal-dialog { .modal-content { .modal-body { max-height: calc(80vh - 140px); overflow-y: auto; } } } }
Make sure the
.modal
max-height
is100vh
. Then for.modal-body
usecalc()
function to calculate desired height. In above case we want to occupy80vh
of the viewport, reduced by the size of header + footer in pixels. This is around 140px together but you can measure it easily and apply your own custom values. For smaller/taller modal modify80vh
accordingly.Instead of using a %, the units vh set it to a percent of the viewport (browser window) size.
I was able to set a modal with an image and text beneath to be responsive to the browser window size using vh.
If you just want the content to scroll, you could leave out the part that limits the size of the modal body.