english • français

11/2002

While building this website I had a strange problem : I was using XHTML Strict (as recommended by the W3C ;), but I didn't succeed in centering a <table>, I tried to find help on Internet but no hint...
so...

How to center a <table> in XHTML Strict ?

In XHTML Strict, something like <center><table>...</table></center> or <table align="center">...</table> is forbidden (because of the deprecated <center> tag and the forbidden align="center" in <table>).

With Mozilla you can use the CSS style table { margin-left:auto; margin-right:auto; } but it doesn't work within Microsoft Internet Explorer (MSIE).

The only thing which works both in Mozilla and in MSIE is wrapping the <table> in another <table> (align="center" being allowed within <td> tag) :

<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="center"><table>...</table></td></tr></table>

It's not so nice but it works !

Added 2003-06-17 : Another idea is to combine both previous tricks and use a CSS style - table { margin-left:auto; margin-right:auto; text-align: center; } - and it works both in MSIE and Mozilla !

home :: blog :: photo blog :: my photo gallery