mmmoustache mmmoustache - 3 months ago 36
Javascript Question

Position div to center of visible area

I'm in the midst of making a lightbox style pop-up for a mailing list sign up, but I want the pop-up to position to the center of the visible page, not just the center of the whole document; if the user scrolls to the bottom of the page and clicks to sign up, I want it to appear in the center of the screen.

I'm assuming jQuery/JS will be the best way to go for this; here's my current CSS code which works fairly well but the div needs to be pushed down into the visible space dynamically for smaller screens.

.my-div{
width:960px;
height:540px;
position:absolute;
top:50%;
left:50%;
margin-left:-480px;
margin-top:-270px;
z-index:60;
display:none;
}

Answer

You were close! It can be done with CSS alone:

Use position: fixed instead of position: absolute.

Fixed refers to the viewport, while absolute refers to the document. Read all about it!

Comments