Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

center the modal-dialog Horizontally and vertically

How can I center the modal-dialog vertically and Horizontally in the page?

like image 738
John Macl Avatar asked Apr 20 '14 18:04

John Macl


Video Answer


2 Answers

A jQuery solution, considering the modal-dialog is position absolute/relative/fixed:

var windowHeight = $(window).height();
var windowWidth = $(window).width();
var boxHeight = $('.modal-dialog').height();
var boxWidth = $('.modal-dialog').width();
$('.modal-dialog').css({'left' : ((windowWidth - boxWidth)/2), 'top' : ((windowHeight - boxHeight)/2)});

A jQuery solution, considering the modal-dialog it's not position absolute/relative/fixed:

css:

margin-left: auto;
margin-right: auto;

jquery:

var windowHeight = $(window).height();
var boxHeight = $('.modal-dialog').height();
$('.modal-dialog').css({'margin-top' : ((windowHeight - boxHeight )/2)});
like image 74
entiendoNull Avatar answered Oct 01 '22 18:10

entiendoNull


.modal-dialog {
    position: absolute;
    left: 50%;
    top: 50%;
    -moz-transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    -o-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}

/* for IE 8 */
.modal-dialog.ie8 {
    /* you need to specify width and height */
    width: 500px; 
    height: 300px;
    margin-left: -150px; /* 300/2=150 */
    margin-top: -250px; /* 500/2=250 */
}
like image 36
aimadnet Avatar answered Oct 01 '22 18:10

aimadnet