I’m trying to get some HTML pages to be more compliant and am starting off with simple stuff like removing align=”center” in tables, etc.
However, I can’t seem to get the effect I want using CSS. Stuff I’ve tried include
text-align:middle
vertical-align:middle
and the most suggested by designer friends
margin-left:auto; margin-right:auto; width: 100px
But under certain layouts or certain browsers (especially IE6) none seem to work. Is there a reliable way to do this using purely CSS and get it to work on most browsers?
EDIT:
I meant text-align: center;. Typed it in a rush. The page already has a DOCTYPE but I’m not sure if it’s appropriate.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
It’s:
for horizontal centering but:
for vertical centering but vertical centering this way will pretty much only work with table cells. To get cross-browser vertical centering without using tables look at Vertical Centering in CSS (yes it’s non-trivial).
Note:
text-align: middleandvertical-align: centerare incorrect values.To horizontally center a block element, the usual trick is:
with:
Bear in mind, that this won’t necessarily work with certain versions of IE in the euphemistic “quirks” mode. It’s a good practice to force IE into (also euphemistic) “standards compliant” mode by using a DOCTYPE at the top of your document.