53

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?

Doug McClean
  • 14,265
  • 6
  • 48
  • 70
  • Does your action code, instead of ``, contain any invalidly nested code? A `` without a `` for example? – Gareth Apr 01 '11 at 05:20
  • Gareth, good question, but no it doesn't. I just meant to suggest that there is more than one similar row. I tried it with only the one and it only reduced the duplicate copies of the same warning (one for each such cell). – Doug McClean Apr 01 '11 at 11:22

3 Answers3

66

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 22
    I agree. I created [issue 656626](http://connect.microsoft.com/VisualStudio/feedback/details/656626/html5-validation-reports-spurious-error-when-th-tags-appear-within-tbody-tags) to report this. – Doug McClean Apr 01 '11 at 11:35
  • @Doug McClean: Any word on VS11's HTML5 validator? I'm not sure if it's any different from the VS2010 one. – BoltClock Mar 19 '12 at 17:22
  • @BoltClock I haven't had a chance to play with VS11 yet. – Doug McClean Mar 19 '12 at 18:36
  • 3
    I could fix the warnings on VS2010 by changing the `html_5.xsd` and `xhtml_5.xsd`, based on Chris's [answer](http://stackoverflow.com/questions/9603241/html5-element-legend-occurs-too-few-times#answer-12912876). Open the file, find the line `` and change it to ``. – tie Apr 29 '13 at 00:35
13

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.

Chuck
  • 234,037
  • 30
  • 302
  • 389
5

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

Christian Ziebarth
  • 753
  • 1
  • 7
  • 20