Bill Zimmerman Bill Zimmerman - 7 days ago 6
HTML Question

Are table headers only for the top row in html?

I always see the th tag only used in the first row of the table. Is there some specific reason why it can't be used to create 'left' headers along the leftmost column. Is this bad form, or is this ok.

Basically, a table with headings on the top row and the leftmost column, with the very top left square being empty.

e.g.

<table>
<tr>
<th><!--empty--></th>
<th>Top 1</th>
<th>Top 2</th></tr>
<tr>
<th>LeftHeader?</th>
<td>data1</td>
<td>data2</td></tr>
</table>

Answer

That's valid, however, when using a <thead> it has to be the first row. This is valid:

<table>
    <thead>
        <tr>
            <td>0,0</td><td>1,0</td><td>2,0</td>
        </tr>
    </thead>
    <tr>
        <th>0,1</th><th>1,1</th><th>2,1</th>
    </tr>
</table>

But this is not:

<table>
    <tr>
        <td>0,0</td><td>1,0</td><td>2,0</td>
    </tr>
    <thead>
        <tr>
            <th>0,1</th><th>1,1</th><th>2,1</th>
        </tr>
    </thead>
</table>

It's invalid HTML and you can double check that with the w3C markup validation service though before you do you'll have to add a <!DOCTYPE> declaration and the rest of a valid HTML doc.

Comments