Does HTML5 ban th cells from tbody?

asp.net MvcHtmlWeb Standards

asp.net Mvc Problem Overview


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?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

> 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.

Solution 2 - asp.net Mvc

The HTML5 spec only requires that it be inside a tr, and the spec actually includes an example with a th nested inside a tbody.

Solution 3 - asp.net Mvc

Generally a TH in a THEAD will have a scope value of "col" while a TH in a TBODY will have a scope value of "row".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDoug McCleanView Question on Stackoverflow
Solution 1 - asp.net MvcBoltClockView Answer on Stackoverflow
Solution 2 - asp.net MvcChuckView Answer on Stackoverflow
Solution 3 - asp.net MvcChristian ZiebarthView Answer on Stackoverflow