Mohamad
Mohamad

Reputation: 35349

How do I vertically center a modal using jQuery, independent of scrolling height?

I want to vertically center a modal. If the window height is sufficient to require scrolling, the popup always appears near the top. This means I will need to scroll up to see it.

var winH = $(window).height(),
    winW = $(window).width();
$(".popup").css('top', winH / 2 - $(".popup").height() / 2);
$(".popup").css('left', winW / 2 - $(".popup").width() / 2);

CSS:

.popup {
    overflow:hidden;
    position:absolute;
    width:600px;
}

Upvotes: 0

Views: 4402

Answers (2)

RandomWebGuy
RandomWebGuy

Reputation: 1439

Sounds like you already found your answer, but since I have it. Here's a working JS fiddle version: Live Version

Upvotes: 1

ddinchev
ddinchev

Reputation: 34673

Just go with "position: fixed".

Upvotes: 3

Related Questions