表格 <thead><tbody>
<thead>、<tbody> 和 <tfoot> 是 HTML 里非必须的表格元素 ( 通常不会特别去使用 ),使用后能让表格看起来更有架构性,这篇教学会介绍 <thead>、<tbody> 和 <tfoot> 的用法。
认识 <thead>、<tbody> 和 <tfoot>
<thead>、<tbody> 和 <tfoot> 是 HTML 里非必须的表格元素,通常不会特别去使用,使用后能让表格看起来更有架构性,<thead> 是表格的表头,<tbody> 是表格的内容,<tfoot>则是表格的结尾。
<thead>、<tbody>和<tfoot>属于“容器元素”,需要有“起始标签”以及“结束标签”。<thead>显示类型为“table-header-group”,只能作为<table>的子元素。<tbody>显示类型为“table-row-group”,只能作为<table>的子元素。<tfoot>显示类型为“table-footer-group”,只能作为<table>的子元素。
下方的 HTML 会在表格加入 <thead>、<tbody> 和 <tfoot> ( 额外加入边界值样式 )。
<style>
td, th{
padding:20px;
}
</style>
<table border=1>
<thead>
<tr>
<th></th>
<th>a</th>
<th>b</th>
</tr>
</thead>
<tbody>
<tr>
<th>第一項</th>
<td>10</td>
<td>20</td>
</tr>
<tr>
<th>第二項</th>
<td>100</td>
<td>200</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>加總</th>
<td>110</td>
<td>220</td>
</tr>
</tfoot>
</table>
<thead>、<tbody> 和 <tfoot> 支援属性
<thead>、<tbody> 和 <tfoot> 支援“全域属性”以及“可见元素的事件属性”( 参考“HTML 元素属性”),例如下方的 HTML,会将 <thead>、<tbody> 和 <tfoot> 加入 style 属性,改变其背景颜色。
<style>
td, th{
padding:20px;
}
</style>
<table border=1>
<thead style="background:#ccc">
<tr>
<th></th>
<th>a</th>
<th>b</th>
</tr>
</thead>
<tbody>
<tr>
<th>第一項</th>
<td>10</td>
<td>20</td>
</tr>
<tr>
<th>第二項</th>
<td>100</td>
<td>200</td>
</tr>
</tbody>
<tfoot style="background:#f99;">
<tr>
<th>加總</th>
<td>110</td>
<td>220</td>
</tr>
</tfoot>
</table>
<thead>、<tbody> 和 <tfoot> 默认样式
下方是 <thead>、<tbody> 和 <tfoot> 的默认样式:
详细样式教学可参考:CSS 教学、认识表格、表格默认样式
thead {
display: table-header-group;
vertical-align: middle;
border-color: inherit;
}
tbody {
display: table-row-group;
vertical-align: middle;
border-color: inherit;
}
tfoot {
display: table-footer-group;
vertical-align: middle;
border-color: inherit;
}
微信扫码关注
抖音扫码关注