如果您想提供有关表格的附加信息并组织其中的内容,则可以使用它们。它们还可以在某些方面影响表格的视觉呈现(尽管这可能因浏览器而异,例如,对<colgroup>
/<col>
is 极其零散)。
For example if you have header rows on the top and bottom you would group them in a <thead>
and a <tfoot>
respectively, and the data rows in a <tbody>
. Browsers will ensure that the <tfoot>
is always rendered at the bottom no matter whether you place it before or after any <tbody>
or <tr>
elements1 or how much data you have in your table, which is useful if your table potentially has lots of rows:
<table>
<caption>High Scores</caption>
<thead>
<tr><th>#</th><th>Name</th><th>Score</th></tr>
</thead>
<tfoot>
<tr><th>#</th><th>Name</th><th>Score</th></tr>
</tfoot>
<tbody>
<tr><td>1</td><td>Alice</td><td>10000</td></tr>
<tr><td>2</td><td>Bob</td><td>9000</td></tr>
<tr><td>3</td><td>Carol</td><td>8500</td></tr>
<tr><td>4</td><td>Dave</td><td>8000</td></tr>
<!-- Up to 100 data rows -->
</tbody>
</table>
否则,默认情况下所有行都会分组为一个<tbody>
(即使你没有明确使用<tbody></tbody>
表中的标签)。因此,如果标题行位于表格底部,则必须将它们放在表格的最底部,以便它们显示在最后:
<table>
<caption>High Scores</caption>
<tr><th>#</th><th>Name</th><th>Score</th></tr>
<tr><td>1</td><td>Alice</td><td>10000</td></tr>
<tr><td>2</td><td>Bob</td><td>9000</td></tr>
<tr><td>3</td><td>Carol</td><td>8500</td></tr>
<tr><td>4</td><td>Dave</td><td>8000</td></tr>
<!-- Up to 100 data rows -->
<tr><th>#</th><th>Name</th><th>Score</th></tr>
</table>
当然,如果你关心这类事情,这也可以说减少了它的语义。
1 Note that it is required that a <tfoot>
, if you use one, be placed before any <tbody>
or <tr>
elements in previous specifications up to and including HTML 4 http://www.w3.org/TR/html401/struct/tables.html#h-11.2.3 and XHTML 1 for it to validate against those doctypes. This is no longer true as of HTML5 http://www.w3.org/TR/html-markup/tfoot.html.