In an HTML table, how can I center the contents of the cells (not the table itself)?

2.4k Views Asked by At

Given that I'm allowed to use static JS and HTML tags (only those supported by IE6), how can I center the contents of the cells in an HTML table (not the table itself)?

4

There are 4 best solutions below

0
Richard Ev On BEST ANSWER

Using the css style text-align: center;, either as an inline style (below) or as a css style applied to all <td> elements (as suggested in Joel's answer).

<td style="text-align:center;">
    Table contents here
</td>
1
lhf On

Use the align attribute for <td>.

1
Joel Etherton On

IE 6 supports CSS. In the head of the document:

<style type="text/css">

td {
    text-align: center;
}

</style>

If you have more than one table and only want to do this to a specific table, you can use the selectors to get even more granular.

0
Thijs Wouters On

This is a solution with only html tags.

<table>
  <tr>
    <td><center>Data</center></td>
  </tr>
</table>

But the other solutions are better in view of best practices