How do I center a DIV in the exact center of a page using jQuery?

Posted by sadmicrowave on Stack Overflow See other posts from Stack Overflow or by sadmicrowave
Published on 2010-04-08T13:05:42Z Indexed on 2010/04/11 21:43 UTC
Read the original article Hit count: 285

Filed under:
|
|

I need to center a DIV in the exact center of a page using jquery. My CSS style for the DIV is as follows:

#page-content #center-box{
 position:absolute;
 width:400px;
 height:500px;
 background:#C0C0C0;
 border:1px solid #000;
 }

and my jQuery for centering is as follows:

windowheight = $(window).height();
windowwidth = $(window).width();
pagecenterW = windowwidth/2;
pagecenterH = windowheight/2;
$("div#page-content div#center-box")
    .css({top: pagecenterH-250 + 'px', left: pagecenterW-200 + 'px'});

This code does not invoke any action on my page when refreshed. What am I doing wrong?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about center