Jump to content

position center of screen


dreamwest

Recommended Posts

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! ;D

 

cssfreakie

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.