<center> tag displays differently vs text-align

1.6k views Asked by At

I've just discovered <center> is obsolete in HTML5, but it works fine in IE9 with <!DOCTYPE html>:

http://www.w3.org/TR/html5/obsolete.html#obsolete

The alternative is to use CSS text-align, but I get conflicting results:

<body>
  <table style="text-align:center">
    <tr>
      <th>Column</th>
    </tr>
    <tr>
      <td>Data</td>
    </tr>
  </table>
  <p style="text-align:center">Text</p>
</body>

Any suggestions on how to center both the table content and the p content within the page? (I can't see what's so bad with <center> anyway).

1

There are 1 answers

2
SW4 On BEST ANSWER

I've just discovered is obsolete in HTML5 (...) I can't see what's so bad with (it) anyway

If it isnt in the spec, or is marked obsolete then how it is interpreted and whether it is supported becomes increasingly up to the vendor.

As of Oct 28th 2014 center appears under

Elements in the following list are entirely obsolete, and must not be used by authors

You may being using center now and it 'works', but in the next version of your browser, all support may be droppped, leaving you with a page that doesnt render as intended.

text-align:center will center your text, in exactly the same way. However any additional styling applied to the center element by browsers (each has a default set of CSS styles often derived from the w3c recommendation) will not be present.

The conflicting results you note arent due to the use of center vs text-align they are because the text is being aligned within elements of varying widths. If you set the width of your table to 100%, the alignments will be similar.