Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 3 - set height of modal window according to screen size

I am trying to create a kind of responsive megamenu using Bootstrap 3 modal dialog. I would like to set the width and height of the whole modal window to 80% of the viewport, while setting the modal-body to a max-height in order not to populate the whole screen on large viewports.

My HTML:

    <div class="modal fade">             <div class="modal-dialog modal-megamenu">                 <div class="modal-content">                     <div class="modal-header">                         <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>                         <h4 class="modal-title">Modal Mega Menu Test</h4>                     </div>                     <div class="modal-body">                         <div class="row">                             <div class="col-md-3"></div>                             <div class="col-md-3"></div>                             <div class="col-md-3"></div>                             <div class="col-md-3"></div>                         </div>                     </div>                 </div>                     <div class="modal-footer">                         <button type="button" data-dismiss="modal" class="btn btn-primary">close</button>                     </div>              </div>         </div> 

My CSS:

.modal-megamenu {     width:80%;     height:80%; }  .modal-body {     max-height:500px;     overflow:auto; } 

This works as intended for the width, but the "height" parameter doesn't give any result. Like that, the height of the modal window is always set to the max-height value. Does anybody have an idea of how to set the height dynamically according to the viewport height?

like image 923
Ralf Glaser Avatar asked Oct 02 '13 15:10

Ralf Glaser


People also ask

How do I change the height of a bootstrap modal?

modal max-height is 100vh . Then for . modal-body use calc() function to calculate desired height. In above case we want to occupy 80vh of the viewport, reduced by the size of header + footer in pixels.

How do I change the width and height of a modal in bootstrap?

Change the size of the modal by adding the . modal-sm class for small modals, . modal-lg class for large modals, or . modal-xl for extra large modals.

How can I make my modal height responsive?

Making your Modal Responsive The most obvious way is to write a bunch of media queries like a chump. I instead suggest using max-width and max-height. When combined with calc() this allows you to have a consistent padding between the modal and the edge of the screen on smaller devices.

How do you increase width and height of modals?

Answer: Set width for . modal-dialog element Similarly, you can override the width property of . modal-sm , . modal-lg and . modal-xl class to resize the small, large and extra-large modal dialog box respectively.


1 Answers

Pure CSS solution, using calc

.modal-body {     max-height: calc(100vh - 200px);     overflow-y: auto; } 

200px may be adjusted in accordance to height of header & footer

like image 177
Daniel Garmoshka Avatar answered Oct 11 '22 11:10

Daniel Garmoshka