asp.net-mvchtmlweb-standards

Does HTML5 ban th cells from tbody?


I have the following markup as a part of a Razor view:

<table>
  <caption>Presidents</caption>
  <thead>
    <tr>
      <th scope="col">Name</th>
      <th scope="col">Born</th>
      <th scope="col">Died</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Washington</th>
      <td>1732</td>
      <td>1799</td>
    </tr>
    <!-- etc -->
  </tbody>
</table>

With the "target schema for validation" set to HTML5, Visual Studio complains thusly:

Warning 1 Validation (HTML5): Element 'th' must not be nested within element 'tbody tfoot'.

Is this really true? If so, could someone link to the spec?

My understanding was that using <th> for row headers was not just legal but encouraged. It certainly seems fairly common, I could link dozens of tutorials explaining (seemingly sensibly) that it helps with accessibility.

Is this a VS bug? A real change coming with HTML5 (a good one? a bad one?)? What's the story?


Solution

  • My understanding was that using <th> for row headers was not just legal but encouraged

    As far as I know, this was always legal in HTML 4 (and possibly its predecessors), and hasn't changed in HTML5.

    W3C's HTML5 validator, while still experimental, reports no warnings or errors. Then again, I'm sure the HTML5 validation Visual Studio is using is experimental as well since HTML5 itself hasn't yet been finalized.

    The HTML5 spec on marking up tabular data, specifically section 4.9.13, shows the use of <th> within <tbody> and <tfoot> to scope row data:

    <table>
     <thead>
      <tr>
       <th>
       <th>2008
       <th>2007
       <th>2006
     <tbody>
      <tr>
       <th>Net sales
       <td>$ 32,479
       <td>$ 24,006
       <td>$ 19,315
      <tr>
       <th>Cost of sales
       <td>  21,334
       <td>  15,852
       <td>  13,717
     <tbody>
      <tr>
       <th>Gross margin
       <td>$ 11,145
       <td>$  8,154
       <td>$  5,598
     <tfoot>
      <tr>
       <th>Gross margin percentage
       <td>34.3%
       <td>34.0%
       <td>29.0%
    </table>
    

    So it's perfectly legitimate to have <th> elements inside <tr> elements inside either a <tbody> or <tfoot>. As it should be anyway, since table headings aren't just found on table headers.