View Full Version : Centering a page when Absolute positioning is used

10-26-2007, 06:56 AM
OK I am going to try and explain this as best as possible. My page is completed and done in CSS. I am trying to center my page using CSS also but since I used all div's and absolute positioning throughout the whole page I can't seem to get it to center correctly. Is this possible since I already used div's and absolute positioning to create my page. There is example online at: http://www.ultimatevanessa.com that can be used but how would I go about centering a page like that? Help!

10-26-2007, 07:28 AM
Wow, I’m really wondering that your page works at all because you’ve currently got 173 errors (http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fwww.ultimatevanessa.com%2F) in your HTML…
And your page is a good example of the use of absolutley positioning in a slightly wrong manner. A classical case where the programmer hasn’t undrstood the concept of absolute positioning.

But anyway… I can’t promise it will work but usually to center a page one would put the entire content in a wrapping div (called “wrapper” or “container”) which will be given a specific width and centered with margin: auto;. You could also do this with the body element itself, though.
In your case it’s probably also necessary to position that wrapper relatively, otherwise the absolutely positioned elements would stay at the side anyway.

So in a nutshell:

body {
width: ???px;
margin: auto;
position: relative;

And by the way: Your code is far from being semantic (http://www.boagworld.com/archives/2005/11/semantic_code_what_why_how.html). Again, that page is a classical case of the misconception that with absolute positioning you can put any element anywhere on the page. Just because you can doesn’t mean you have to. And also you are encouraged to separate the CSS and the HTML (and the JavaScript), i.e. to put it in separate files to clean up the code.

10-26-2007, 05:46 PM
Thanks for the reply. I added a bit more information to the code you left and it worked perfectly. The new page that I created has an external stylesheet, a new design and a lot less code...lol. The page that is currently up was my first try at css so I am sure it had a lot of mistakes or easier ways of doing things. But I did take into consideration everything you said so I will go back over the new page that is not up yet and make sure that things are a bit more organized and put together. Again, thanks.

10-26-2007, 05:49 PM
You could also use this, works in IE6, whereas margin: auto; doesn't.

<style type="text/css">
.box {
width: 80%; /* width of the container box */
position: absolute;
left: 50%; /* the box is now starts at the center of the window */
top: 5%; /* not required: just for good measure */
margin-left: -40%; /* half the width to correct the center */

10-27-2007, 05:15 AM
You could also use this, works in IE6, whereas margin: auto; doesn't.

I'm pretty sure it does exist. Maybe IE handles it strangely, but I know this works:
margin: (top margin) auto (bottom margin) auto;

I know it works because I've used it, and it works in FF, IE6, IE7, Opera, and Safari.

You could try applying that style to the body tag, or just put a container div around everything and apply the style to that.

10-27-2007, 06:21 PM
You could also use this, works in IE6, whereas margin: auto; doesn't.

That’s wrong. IE does understand and correctly apply margin: auto; but only in standards mode, i.e. with a correct and complete doctype at the top of the document (and nothing before that). It’s only IE’s quirks mode where it doesn’t work.