tags:

views:

100

answers:

6

Hi there,

I cannot get my site to be centered for the life of me with CSS. I have tried all the usual methods suggested around the web including:

body {
    text-align: center;
}
#container {
    width: 770px;
    margin: 0 auto;
    text-align: left;
}

Then using

<div id="container>
<!-- Centered Content Goes here-->
</div>

But it just wont go to the center. It stays at the left side of the page.

An example of the CSS for the element that I want to be centered is this:

#topHeader
{
    background:url(images/top_header.jpg);
    position:absolute;
    width: 695px;
    height: 242px;
    top: 0px;
    left: 0px;
}

So, my HTML would look like this:

<div id="container>
<div id="topHeader></div>
<!-- All other elements go here as well-->
</div>

But as I mentioned before, the element stays put. Thanks! Eric

A: 

As far as I know it simply doesn't work. text-align centers text or inline content, not block elements.

Edit: On the other hand The Disintegrator's link makes sense. Unfortunately, only for fixed-sized blocks.

Michael Krelin - hacker
-1 what!
Crimson
Crimson, you can not center block element without knowing its width ahead of time.
Michael Krelin - hacker
A: 

You're placing the header absolutely so it's being offset from the containing block (i.e. body), not the parent element. What you want is Relative positioning.

absolute

The box's position (and possibly size) is specified with the 'top', 'right', 'bottom', and 'left' properties. These properties specify offsets with respect to the box's containing block. Absolutely positioned boxes are taken out of the normal flow. This means they have no impact on the layout of later siblings. Also, though absolutely positioned boxes have margins, they do not collapse with any other margins.

- 9.3.1 Choosing a positioning scheme: 'position' property

Absolute:

<html>
    <head>
     <style type="text/css">
      body {
       text-align: center;
      }
      #container {
       color:blue;
       border:1px solid blue;

       width: 770px;
       margin: 0 auto;
       text-align: left;
      }
      #topHeader
      {
       color:red;
       border:1px solid red;

       position:absolute;
       width: 695px;
       height: 242px;
       top: 0px;
       left: 0px;
      }  
     </style>
    </head>
    <body>
     outside
     <div id="container">
      inside
      <div id="topHeader">deep inside</div>
      <!-- All other elements go here as well-->
     </div>
    </body>
</html>

Relative:

<html>
    <head>
     <style type="text/css">
      body {
       text-align: center;
      }
      #container {
       color:blue;
       border:1px solid blue;

       width: 770px;
       margin: 0 auto;
       text-align: left;
      }
      #topHeader
      {
       color:red;
       border:1px solid red;

       position:relative;
       width: 695px;
       height: 242px;
       top: 0px;
       left: 0px;
      }  
     </style>
    </head>
    <body>
     outside
     <div id="container">
      inside
      <div id="topHeader">deep inside</div>
      <!-- All other elements go here as well-->
     </div>
    </body>
</html>
CptSkippy
+5  A: 

Try with this dead centre

The Disintegrator
+1, I liked the idea.
Michael Krelin - hacker
Awesome example. Added it to my CSS techniques cheat list.
Programming Hero
+1 Nice resource.
rick schott
A: 

Try adding this to the top of your css file:

// Wipes out any preexisting padding and margin.
html, body {
    padding: 0;
    margin: 0;
}

Then add a position: relative; directive to the class you want centered. Actually, try adding it to the html, body one so that all your classes use relative position. It might be that you have position: absolute; set which then combines with the left: 0px; to force your header contain to ignore the margin: 0 auto; and stay on the left of the page.

Daniel Bingham
A: 

The primary issue is the absolute positioning of your #topHeader element. Because you have it absolutely positioned with top: 0px; left: 0px;, that's exactly where it's going to be positioned - at the top left of the page.

Start off by removing the absolute positioning from the #topHeader element.

bcwood
A: 

One thing to check when trying out all of these methods of centering is to make sure that your doctype is correct for the method that is being used.

Hope this helps for you.

Chris