Css – Text Centering Using CSS not working in IE

centercssinternet explorertext-align

I am having problems getting text within a table to appear centered in IE.

In Firefox 2, 3 and Safari everything work fine, but for some reason, the text doesn't appear centered in IE 6 or 7.

I'm using:

h2 {
  font: 300 12px "Helvetica", serif; 
  text-align: center; 
  text-transform: uppercase;
}

I've also tried adding margin-left:auto;, margin-right:auto and position:relative;

to no avail.

Best Answer

CSS text-align property should be declared on the parent element and not the element you are trying to center. IE uses text-align: center property to center text. Firefox uses margin: 0 auto and it has to be declared on the element you are trying to center.

<div style="text-align: center">
    <h2 style="margin: 0 auto">Some text</h2>
</div>