Jump to content

Absolute centering (horizontal and vertical)


seany123

Recommended Posts

im wanting to center vertically and horizontally.. this is the code i have so far! its not far away...

 

CSS:


body {
    background-color: #E1DDD9;
    color: #564B47;
    font-family: Verdana,Arial,Helvetica,SunSans-Regular,Sans-Serif;
    font-size: 12px;
    margin: 0;
    padding: 0;
    width: 100%;
}

#centeredcontent {
    left: 50%;
    margin: -100px 0 0 -200px;
    position: absolute;
    top: 50%;
}

Link to comment
Share on other sites

That's no problem. As long as the left margin is minus half the width.

 

The trick is that with position: absolute plus left:50% the element is put at the right-hand side of the middle of the screen. To get it centered, move it back to the left with margin-left:-(half the element's width).

Link to comment
Share on other sites

I changed it to 256.5px and its seemed to have centered it perfectly.

 

Risky business. Half pixels are not valid values. You're running the risk that some browsers don't render it at all, despite the fact that there are also browsers that forgive the error by ignoring the .5.

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.