0

In HTML we can use <tbody> and <thead>.

Which works fine with a 'normal' table.

<table>
  <thead>
    <tr>
      <th>col1</th>
      <th>col2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>data1-1</td>
      <td>data1-2</td>
    </tr>
  </tbody>
</table>

However sometimes there is a rotated table:

<table>
  <tr>
    <th>col1</th>
    <td>data1-1</td>
  </tr>
  <tr>
    <th>col2</th>
    <td>data1-2</td>
  </tr>
</table>

When I use a rotated table I never use <tbody> or <tbody>.

However if I'm correct the <tbody> is mandatory if a <tfoot> is used.

Does a <tr> have to be inside a <tbody>

So my question is:

Is the above statement correct (that it is indeed mandatory if a <tfoot> is used)?

If so where would you add <thead>s and <tbody>s in the second example table?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
PeeHaa
  • 71,436
  • 58
  • 190
  • 262

1 Answers1

1

According to the W3 specification the tbody tag is always mandatory unless your table has only one table body and there is no header and foot sections.

In your case you can use:

<table>
  <tbody>
   <tr>
    <td>col1</td>
    <td>data1-1</td>
   </tr>
   <tr>
    <td>col2</td>
    <td>data1-2</td>
   </tr>
  </tbody>
 </table>

That is HTML valid. Since you don't have "real" header on top of the table I think no header tag applies here. I'm not sure rotated tables are supported by HTML convention, so you basically have a normal table with only body.

jasalguero
  • 4,142
  • 2
  • 31
  • 52
  • I do need to use ``s or I need to add extra classes for the 'header' cells (which is a last resort). Will it still validate? – PeeHaa Aug 08 '11 at 10:08
  • 1
    Yes, you can use th for the 'header' cells, or add class attribute for css styling / javascript. In both cases it validates. You can try yourself with http://validator.w3.org/check – jasalguero Aug 08 '11 at 11:20