center bootbox vertically
I use the below script to automatically center any bootstrap modal I use in my web site, and it works fine, but it does not work with bootbox even though bootbox uses the 'modal' class. Is bootbox somehow overwriting the show event? If so, is there a work-around to vertically center bootbox? Thanks.
$(".modal").on("show", function () { $(this).css({ 'top': '50%', 'margin-top': function () { return -($(this).height() / 2); } }); });
Hey,
No, the show event isn't being overridden, but I suspect it's due to the way you're listening for the event directly on .modal - you need to use event delegation instead. This is a jQuery / JavaScript issue rather than anything to do with Bootbox. Check out the docs for $.on and search for the section starting 'event handlers are bound only...' and you'll find a far better explanation than I could give you :).
Thank you very much for pointing me in the right direction. I changed the way I was listening to the 'show' event and now bootbox is centered vertically in the screen. This is how it looks now.
$("body").on("show", ".modal", function () { $(this).css({ 'top': '50%', 'margin-top': function () { return -($(this).height() / 2); } }); });
No problem, glad that sorted it.
Didn't have luck using the above method with 4.3.0. Any tip?
@cvsguimaraes check those events from bootstrap documentation: http://getbootstrap.com/javascript/#modals
$("body").on("shown.bs.modal", ".modal", function() {
$(this).css({
'top': '50%',
'margin-top': function () {
return -($(this).height() / 2);
}
});
});
This didn't work for me. For some reason it shifts the whole page down:

I then tried a different approach:
$("body").on("shown.bs.modal", ".modal", function() {
$(this).find('div.modal-dialog').css({
'top': '50%',
'margin-top': function () {
return -($(this).height() / 2);
}
});
});
...which kinda works - but not in Chrome for some reason:

Any suggestions?
Use the method suggested by @tarlepp but pay attention to the parent (probably the document body) positioning. Check if you used some absolute or fixed positioning.
Mmm nope, didn't have that. Its weird, as the modal-wrapper looks like it has scrollbars (but starting part way down the page)
Anyway, I managed to get it going with this "hacky" solution :)
$("body").on("shown.bs.modal", ".modal", function() {
$(this).find('div.modal-dialog').css({
'margin-top': function () {
var modal_height = $('.modal-dialog').first().height();
var window_height = $(window).height();
return ((window_height/2) - (modal_height/2));
}
});
});
Hello, how could I get this on angular??
@diegomesata use directive for that.
Just had this issue after I made a top fixed header. The fix I did was to add a css class in the bootbox config with a high z-index as in:
bootbox.dialog({ className: "dialogFixedtop"......
and the class is
.dialogFixedtop {
z-index: 9999 !important;
}
Also tried to move the top margin, but the shadow became an issue.
Hi, A little late to the party but the way I do it is var box = bootbox.dialog({ ,,, });
box.css({
'top': '50%',
'margin-top': function () {
return -(box.height() / 2);
}
});
Hope this helps :)
thank you makeusabrew its worked for me.
so what's the final solution?
IanIT's comment worked for me.
Huy guys this would fix all guys :)
var box = bootbox.dialog({
....
});
dialog = box.find('.modal-dialog');
box.css('display', 'block');
dialog.css("margin-top", Math.max(0, ($(window).height() - dialog.height()) / 2));