Search code examples
xhtmlhtml-tablealignment

Centering a Table in xHTML


I need to center an entire table in xHTML. The tags that work in HTML to do so do not necessarily work in xHTML, but the way to do it is not obvious (to me).

Works in HTML:

<div>
<table border="0" cellspacing="0" cellpadding="0" align="center" style="border-collapse:collapse; width:90%;">

Not working in xHTML:

<div>
<table border="0" cellspacing="0" cellpadding="0" style="align:center;border-collapse:collapse; width:90%;">

Using text-align:center instead changes the alignment of text within cells, rather than the alignment of the entire table on the page.

What is the correct method to center align a table in xHTML.


Solution

  • One way to accomplish this is to use margins in the style attribute:

    <table style="margin-left:auto; margin-right:auto">