Set modal layer to the center fo the viewed screen

Stefanie picture Stefanie · Oct 17, 2012 · Viewed 29.3k times · Source

I'm using Twitter Bootstrap's modal, but I had to change their position to absolute, be cause I need them to be able to scroll cause of the small screens. Sadly, with this, the modals open to a fixed position of the site and not on the viewed area. Is there any way to open them (and be able to be scrolled) to the screen what I'm currently viewing?

Picture of my problem

CSS:

.modal {
    width: 845px;
    margin: -250px 0 0 -430px;
    background-color: #f6f5ed;
    position: absolute;
    border: 1px solid #cdc4b2;
    top: 50%;
    left: 50%;
}

Answer

steve picture steve · Feb 6, 2013

This thread: Modal plugin of Bootstrap 2 is not displayed by the center has the correct answer, posted here as well:

   $('#YourModal').modal().css(
            {
                'margin-top': function () {
                    return window.pageYOffset-($(this).height() / 2 );
                }
            });