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%;
}

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).

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.