跳到主要内容

表格基础

表格由哪些元素组成?​

  • table: 表格容器;
  • tr: 一行;
  • th: 标题单元格, 用 scope 声明它管行还是管列;
  • td: 数据单元格;
  • 层级: table > tr > th/td;

如何写出一个带行列标题的表格?​

<table>
<tr>
<th scope="col">Player</th>
<th scope="col">Gloobles</th>
<th scope="col">Za'taak</th>
</tr>
<tr>
<th scope="row">TR-7</th>
<td>7</td>
<td>4,569</td>
</tr>
<tr>
<th scope="row">Khiresh Odo</th>
<td>7</td>
<td>7,223</td>
</tr>
</table>
  • 第一行用 scope="col" 声明列标题, 每行首个 th 用 scope="row" 声明行标题;
  • scope 的完整取值见 单元格合并与关联;

th 与 td 有什么区别?​

元素角色默认渲染
th标题单元格加粗、居中
td数据单元格常规文本、左对齐
  • 共同点: 两者都是单元格, 都可用 colspan/rowspan/headers 属性;
  • 空位: 结构需要的空单元格写成空内容的 td, 不能省略;