This worked for me
.modal-dialog,
.modal-content {
/* 80% of window height */
height: 80%;
}
.modal-body {
/* 100% = dialog height, 120px = header + footer */
max-height: calc(100% - 120px);
overflow-y: scroll;
}
Fiddle: http://jsfiddle.net/mehmetatas/18dpgqpb/2/
Answer from Mehmet Ataş on Stack OverflowThis worked for me
.modal-dialog,
.modal-content {
/* 80% of window height */
height: 80%;
}
.modal-body {
/* 100% = dialog height, 120px = header + footer */
max-height: calc(100% - 120px);
overflow-y: scroll;
}
Fiddle: http://jsfiddle.net/mehmetatas/18dpgqpb/2/
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.
/*When the modal fills the screen it has an even 2.5% on top and bottom*/
/*Centers the modal*/
.modal-dialog {
margin: 2.5vh auto;
}
/*Sets the maximum height of the entire modal to 95% of the screen height*/
.modal-content {
max-height: 95vh;
overflow: scroll;
}
/*Sets the maximum height of the modal body to 90% of the screen height*/
.modal-body {
max-height: 90vh;
}
/*Sets the maximum height of the modal image to 69% of the screen height*/
.modal-body img {
max-height: 69vh;
}
css - how can i increase bootstrap modal height - Stack Overflow
Bootstrap modal body max height 100% - javascript
jquery - Bootstrap 3 - set height of modal window according to screen size - Stack Overflow
html - How to increase Bootstrap Modal Width? - Stack Overflow
The .modal-content block is inside the .modal-dialog block. Hence, tell the child to be of the same height as the parent. Try:
.modal-dialog {
height: 90%; /* = 90% of the .modal-backdrop block = %90 of the screen */
}
.modal-content {
height: 100%; /* = 100% of the .modal-dialog block */
}
Check:
@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
.modal-tall .modal-dialog {
height: 90%;
}
.modal-tall .modal-content {
height: 100%;
}
/* fix SO stick navigation ;) */
@media (min-width: 768px) { body { padding-top: 75px; } }
<div class="container">
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#modalDefault">
Default modal
</button>
<button type="button" class="btn btn-warning btn-lg" data-toggle="modal" data-target="#modalTall">
Tall modal
</button>
</div>
<div id="modalDefault" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Default modal</h4>
</div>
</div>
</div>
</div>
<div id="modalTall" class="modal modal-tall fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal has 90% height</h4>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
It does not work on Bootstrap 5.
.modal-dialog {
height: 90%; /* = 90% of the .modal-backdrop block = %90 of the screen */
}
.modal-content {
height: 100%; /* = 100% of the .modal-dialog block */
}
The following works well.
.modal-dialog, .modal-content {
height: 70%;
}
.modal-body {
max-height: calc(100% - 120px);
overflow-y: scroll;
}
I caved in and wrote coffeescript to fix this. If anyone's interested, here's the coffeescript:
fit_modal_body = (modal) ->
header = $(".modal-header", modal)
body = $(".modal-body", modal)
modalheight = parseInt(modal.css("height"))
headerheight = parseInt(header.css("height")) + parseInt(header.css("padding-top")) + parseInt(header.css("padding-bottom"))
bodypaddings = parseInt(body.css("padding-top")) + parseInt(body.css("padding-bottom"))
height = modalheight - headerheight - bodypaddings - 5 # fudge factor
body.css("max-height", "#{height}px")
# Here you need to bind your event with the appropriate modal, as an example:
$(window).resize(() -> fit_modal_body($(".modal")))
Or the equivalent javascript as generated per above.
var fit_modal_body;
fit_modal_body = function(modal) {
var body, bodypaddings, header, headerheight, height, modalheight;
header = $(".modal-header", modal);
body = $(".modal-body", modal);
modalheight = parseInt(modal.css("height"));
headerheight = parseInt(header.css("height")) + parseInt(header.css("padding-top")) + parseInt(header.css("padding-bottom"));
bodypaddings = parseInt(body.css("padding-top")) + parseInt(body.css("padding-bottom"));
height = modalheight - headerheight - bodypaddings - 5;
return body.css("max-height", "" + height + "px");
};
$(window).resize(function() {
return fit_modal_body($(".modal"));
});
I faced the same problem with a long form. Javascript was not an option for me, so I ended up with a fully HTML-CSS solution.
The main goal was to code it just working with percentages, in order to have an easy way to build the media queries.
I've tested it with Firefox 22.0, Google Chrome 28.0.1500.71, Safari 6.0.5 and IE8. Here's the demo.
Modal HTML Structure:
The key is to have the structural divs clean from padding/margin/border. All these styles should be applied to the items inside them.
<div id="dialog" class="modal hide dialog1" aria-hidden="false">
<div class="modal-header">
</div>
<div class="modal-body">
<div>
</div>
</div>
</div>
Styles:
The styles applied to these structure divs are the ones which determines its size.
.modal.dialog1 { /* customized styles. this way you can have N dialogs, each one with its own size styles */
width: 60%;
height: 50%;
left: 20%; /* ( window's width [100%] - dialog's width [60%] ) / 2 */
}
/* media query for mobile devices */
@media ( max-width: 480px ) {
.modal.dialog1 {
height: 90%;
left: 5%; /* ( window's width [100%] - dialog's width [90%] ) / 2 */
top: 5%;
width: 90%;
}
}
/* split the modal in two divs (header and body) with defined heights */
.modal .modal-header {
height: 10%;
}
.modal .modal-body {
height: 90%;
}
/* The div inside modal-body is the key; there's where we put the content (which may need the vertical scrollbar) */
.modal .modal-body div {
height: 100%;
overflow-y: auto;
width: 100%;
}
For more detailed code, refer to the demo, where you'll see whole styling classes and those bootstrap styles which needs to be disabled/overriden.
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
Similar to Bass, I had to also set the overflow-y. That could actually be done in the CSS
$('#myModal').on('show.bs.modal', function () {
$('.modal .modal-body').css('overflow-y', 'auto');
$('.modal .modal-body').css('max-height', $(window).height() * 0.7);
});
In your code, for the modal-dialog div, add another class, modal-lg:
<div class="modal-dialog modal-lg">
Or if you wanna centre your modal dialog, use:
.modal-ku {
width: 750px;
margin: auto;
}
The easiest way can be inline style on modal-dialog div :
<div class="modal" id="myModal">
<div class="modal-dialog" style="width:1250px;">
<div class="modal-content">
...
</div>
</div>
</div>
If you want the modal to consume 90% of the viewport height, you don't need any JS. CSS has a special unit for that called vh. 100vh is 100% of the viewport.
In your case, you'll just need:
.modal {
height: 90vh;
}
You can also change it to max-height if necessary.
Your callback function changes the handler for the shown.bs.modal event. If your modal is already open this handler has already been executed and will not trigger again in window resize.
You need a different handler for the window.resize event.
// This will detect and set the height properly when the modal opens
$('.modal').on('shown.bs.modal', function (e) {
var modalObj = $(this).find('.modal-content');
$(modalObj).height('auto');
if ($(modalObj).height() > ($(window).height() * 0.9)) {
$(modalObj).height($(window).height() * 0.9);
}
});
// This will detect and set the height properly when the window resizes
var callback = function () {
jQuery('.modal').each(function (idx, item) {
var modalObj = $(item).find('.modal-content');
$(modalObj).height('auto');
if ($(modalObj).height() > ($(window).height() * 0.9)) {
$(modalObj).height($(window).height() * 0.9);
}
});
};
// Binding the callback in document.ready is not required, just on window.resize
$(window).resize(callback);
Here is a demo in Bootply.
Try with this js to calculate the modal height:
$(document).ready(function() {
$(".showcomments").click(function() {
$('#myModal').modal('show');
rescale();
});
$("[rel=tooltip]").tooltip();
});
function rescale(){
var size = {width: $(window).width() , height: $(window).height() }
/*CALCULATE SIZE*/
var offset = 20;
var offsetBody = 150;
$('#myModal').css('height', size.height - offset );
$('.modal-body').css('height', size.height - (offset + offsetBody));
$('#myModal').css('top', 0);
}
$(window).bind("resize", rescale);
Test: http://jsfiddle.net/fUZxA/23/
It worked for me just fine by using this CSS:
.my-modal {
width:600px;
height:300px;
.modal-header {
background-color:#ccc;
button {
font-size:30px;
}
}
.modal-body {
overflow-y: hidden;
height: 100%;
max-height:190px;
}
}
Update: I took a look at your fiddle. Try to add "height" to your modal.