dreamwest Posted April 4, 2011 Share Posted April 4, 2011 Im trying to position a box in the center of the screen like lightbox does <div style="position:absolute;top:100px;left:140px;width:450px;height:400px;z-index:999;position:fixed;"> This is in the center of the screen</div> Quote Link to comment Share on other sites More sharing options...
cssfreakie Posted April 4, 2011 Share Posted April 4, 2011 Hi Dreamwest, First of all, I really recommend not to use inline style, its mostly redundant and slower than style inherited from an external style-sheet, besides it doesn't separate concerns. Now, the trick in positioning an absolute div depends on the width of the element. If you know the width you are already half way. If you run the code below you will see how it can be done. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link type="text/css" rel="stylesheet" href="css/style.css" /> <title>center an absolute div</title> </head> <style type="text/css"> /* use an external stylesheet!! */ body{margin:0;padding:0;} /* use a reset.css in case you have no idea */ #wrapper{ width:960px; overflow:hidden; /* check my blog why */ margin:0 auto; position:relative; /* see comment for #pop on position */ z-index: 0; } #pop{ position:absolute; /*it's z-index depends on any parent element other than static useful to know for IE7 and lower*/ width:450px; height:400px; z-index:999; background:#ECA03A; color:#fff; top: 50px; left: 50%; /* here starts the magic */ margin-left: -225px; /* the margin-left= width/2 = 450/2=225 */ } #header,#footer{ width:960px; height:120px; background:#333; clear:both; } #left, #right{ min-height: 300px; /* watch out for ie6 */ float:left; } #left{ background:green; width:260px; } #right{ background:greenyellow; width:700px; } </style> <body> <div id="wrapper"> <div id="pop"> <span>This is in the center of the screen</span> </div> <div id="header"></div> <div id="left"></div> <div id="right"></div> <div id="footer"></div> </div> </body> </html> The most important part is this: #pop{ width:450px; left: 50%; margin-left: -225px; /* the margin-left= width/2 = 450/2=225 */ } Hope this helps! cssfreakie Quote Link to comment Share on other sites More sharing options...
dreamwest Posted April 4, 2011 Author Share Posted April 4, 2011 Thanks. Quote Link to comment Share on other sites More sharing options...
cssfreakie Posted April 4, 2011 Share Posted April 4, 2011 Your welcome, You could do the same with a fluid width which can be nice Width:50% left:50%; margin-left: -25%; /* width/2= 50%/2=25% */ Quote Link to comment Share on other sites More sharing options...
stevengreen22 Posted April 12, 2011 Share Posted April 12, 2011 ello again, More questions. with.... #left, #right{ min-height: 300px; /* watch out for ie6 */ float:left; } #left{ background:green; width:260px; } #right{ background:greenyellow; width:700px; } Can you explain why both the left and right div's are set to float left? is it because the pop div is set to absolute so the right one is 'sitting' against it instead of the right side of the screen? If I had tried this I would have floated the right to the right and the left to the left. I get the same result. Is this one of those things where both ways work but your example is the correct way or does it not matter? Having it set to % looks pretty cool! Quote Link to comment Share on other sites More sharing options...
cssfreakie Posted April 12, 2011 Share Posted April 12, 2011 it saves 1 extra declaration of float, and besides that it makes more sense to me to have all divs floated in the same direction, what if a 3th div suddenly comes along? Also, Float right would change the order from right to left (since we read from left to right that's a little odd) Try it out, take 3 divs, Give them a number and foat them all left or right. div.box{ width:200px; height:200px; float:left; /* change this and see what i mean*/ } Best thing is to read the float property at w3.org Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.