I have a custom sized (80% height width) bootstrap modal body and it scrolls (body content will overflow on some sized screens)
There are 2 problems:
This code
//adjust modal body sizes
var fit_modal_body;
fit_modal_body = function(modal) {
var body, bodypaddings, header, headerheight, height, modalheight;
header = $(".modal-header", modal);
footer = $(".modal-footer", 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"));
footerheight = parseInt(footer.css("height")) + parseInt(footer.css("padding-top")) + parseInt(footer.css("padding-bottom"));
bodypaddings = parseInt(body.css("padding-top")) + parseInt(body.css("padding-bottom"));
height = $(window).height() - headerheight - footerheight - bodypaddings - 150;
return body.css({"max-height": "" + height + "px", 'height':'auto'});
};
fit_modal_body($(".modal"));
$(window).resize(function() {
return fit_modal_body($(".modal"));
});
Written by tsdexter, it worked for me!
you should try to position it absolutly within an element whose position is set to relative. There you then can work with something like
{
bottom:0;
top:0;
left:0;
right:0;
You just need to set the height of modal popup on its show, get the height of screen / window and set it to modal's height. (you can multiply it by 0.8 to get 80% height of screen, which fits nicely).
$('#YourModalId').on('show.bs.modal', function () {
$('.modal .modal-body').css('overflow-y', 'auto');
var _height = $(window).height()*0.8;
$('.modal .modal-body').css('max-height', _height);
});
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.
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"));
});
Here is a fully working Sass solution, but it requires flexbox.
// don't clamp modal height when screen is shorter than 400px
// .modal-body is short in that case, and the default behavior
// (entire modal will scroll) is easier to use
@media(min-height:400px)
.modal
// use top/bottom margin here instead of .modal-dialog
// so that .modal-dialog can use height: 100%
margin: 30px
// without overflow: visible the shadow will be clipped
// at the bottom
overflow: visible
> .modal-dialog
margin: 0 auto
// height must be explicitly set for .modal-content to
// use percentage max-height
height: 100%
> .modal-content
display: flex
flex-direction: column
max-height: 100%
> .modal-header,
> .modal-footer,
// don't allow header/footer to grow or shrink
flex: 0 0 auto
> .modal-body
// allow body to shrink but not grow
flex: 0 1 auto
// make body scrollable instead of entire modal
overflow-y: auto