tags:

views:

48

answers:

4

Hello,

I am trying to center a DIV, with "margin:auto" . It works fine with Chrome and FF but the following code does not center the DIV with IE:

CSS

#container {
 margin:auto;
 width:950px;
 height:50px;
 background:#000;
}

HTML

<div id="container"></div>

What am I doing wrong?

Thanks,

Joel


Edit (full HTML/CSS code):

<html>
<head>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.2.0/build/cssreset/reset-min.css"&gt;
<style>

#container {
 margin: 0 auto; 
 width:950px;
 height:50px;
 background:#000;
}
</style>
</head>
<body>
<div id="container"></div>
</body>
</html>
A: 

This link should help you out: http://www.maxdesign.com.au/articles/center/

Nico
Thanks. Seems like the problem was "text-align:center;" even when using IE9!
Joel
@Joel Please explain further, I'm curious ...
Šime Vidas
I used the HTML I showed above. I run the file in the browser and the DIV is aligned to the left. Only when I use "body { text-align:center; }" then the DIV really appears in the center...
Joel
And i am talking about IE9
Joel
A: 

Try this;

#container {
 margin:0 auto;
 width:950px;
 height:50px;
 background:#000;
}
Rob
Using margin: 0 auto; makes not a shred of difference
Alex
@Alex Be careful, I wouldn't be surprised if this change would make a difference in IE. IE has lots of weird bugs
Šime Vidas
Trust me, it's not. It's because the poster most likely doesn't have a valid doctype. Although I do understand where you are coming from having dealt with plenty of IE bugs in my time!
Alex
Why the -1 vote?? I'm only trying to help out here..
Rob
A: 

This should help you out:

body {
    text-align: center;
}

#container {
    text-align: left;
    margin:auto;
    width:950px;
    height:50px;
    background:#000;
}

You do nothing wrong, IE6 does: it ignores "margin: auto;"

Thomasz
This method hasn't been required since IE5.5 I believe. Auto margins have always working in IE... it's just a case of making sure the browser isn't rendering in Quirks Mode.
Alex
IE9? maybe next time you have to say you're talking about IE9.
Thomasz
IE9 beta to be precise...
Šime Vidas
+3  A: 

Check that you have a valid doctype. This behaviour usually associated with the browser rendering in Quirks Mode:

http://www.ahallicks.co.uk/ie.html

Alex
That was the problem!! Thanks! :)
Joel
This is the correct answer. Make sure that the doctype is on the very first line of the code in order to keep IE from firing up quirks mode. In quirks mode, IE ignores the margin:auto; style.
Riley
Awesome, great!
Joel
@Riley +1 for the helpfull coment! Never realized that.
Rob