Validation (HTML5): Element 'th' cannot be nested in element 'table'

13,916

You cannot have the <th> element outside of a <tr>, the following snippet is valid

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>text header</th>
        </tr>
    </thead>
    <tbody>
    <tr>
        <td>7</td>
        <td>text</td>
    </tr>
    <tbody>
</table>

<th> Usage context https://developer.mozilla.org/en/docs/Web/HTML/Element/th

Permitted parent elements

A <tr> element.

Share:
13,916
Coops
Author by

Coops

Love to code, not sure why as head, brick, wall... you know those moments! =) Anyway I think for me it's making life easier for people and problem solving really fires me up. Plus I have a habit of trying to find the best or most efficient way of doing things. Paul SOreadytohelp

Updated on June 14, 2022

Comments

  • Coops
    Coops almost 2 years

    Given the following HTML, why do you get the error:

    Validation (HTML5): Element 'th' cannot be nested in element 'table'

    <table>
        <th>ID</th>
        <th>text header</th>
        <tr>
            <td>7</td>
            <td>text</td>
        </tr>
    </table>
    
  • Coops
    Coops over 7 years
    Should have been obvious really, given the structure of HTML tables and that everything has to be in a row
  • Jeffrey Drake
    Jeffrey Drake over 4 years
    I ran into this, and because the message didn't really say what it should have had in the parent elements (at least in the tool I was using), it wasn't immediately obvious. It should have been obvious sure, but everyone has their days.
  • Coops
    Coops over 2 years
    I think in your case, you just needed to surround all your <th> elements in a row. I have updated my answer to illustrate this