What I was trying to do was simply center a div using CSS. and I couldn’t get my head around it. It simply didn’t work. I used the following code:
<style type="text/css">
<!--
div.test {
width: 300px;
margin-right: auto;
margin-left: auto;
border: 1px solid black;
}
-->
</style>
<div class='test'>
hello
</div>
This didn’t work on IE, it did on anything else (including my iPhone, which made it very frustrating). The div simply stayed aligned on the right.
Now for the (simple!) solution, which cost me a lot of time, and which I couldn’t locate searching for the problem in a search engine. Simply add the doctype, et voila:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
Why did that fix it?
Yes, if you omit the
<!DOCTYPE>declaration you get the dreaded Quirks Mode, where IE tries to behave as much as possible like IE5, for compatibility reasons.Not supporting
margin-left/right: autois one of the more prominent IE5 CSS bugs, but by no means the only one or even the worst. Quirks Mode CSS rendering is an enormous headache you should avoid at all costs. Always include a<!DOCTYPE>in every HTML document.