| 標籤 | 語義 | 說明 |
|---|---|---|
| table | table(表格) | 表格 |
| tr | table row(表格列) | 列 |
| td | table data cell(表格單元格) | 單元格 |
| 標籤 | 語義 | 說明 |
|---|---|---|
| thead | table head | 表頭 |
| tbody | table body | 表身 |
| tfoot | table foot | 表腳 |
| th | table header | 表頭單元格 |
<table>、<tr>、<td> 是表格的三個基本標籤。caption 與 th 可以省略,但這三者一定要有。
<table> <tr> <td>單元格1</td> <td>單元格2</td> </tr> <tr> <td>單元格1</td> <td>單元格2</td> </tr> </table>
說明:<table> 與 </table> 標記表格的起訖,<tr> 與 </tr> 標記列的起訖——有幾組 tr 就是幾列;<td> 與 </td> 則標記單元格。
完整的表格應包含四個部分:表格標題(caption)、表頭(thead)、表身(tbody)、表腳(tfoot)。語義化之後,代碼更清晰,後期維護也更省事。
<table> <caption>表格標題</caption> <!--表頭--> <thead> <tr> <th>表頭單元格1</th> <th>表頭單元格2</th> </tr> </thead> <!--表身--> <tbody> <tr> <td>標準單元格1</td> <td>標準單元格2</td> </tr> <tr> <td>標準單元格1</td> <td>標準單元格2</td> </tr> </tbody> <!--表腳--> <tfoot> <tr> <td>標準單元格1</td> <td>標準單元格2</td> </tr> </tfoot> </table>
說明:thead、tbody、tfoot 分別代表表頭、表身、表腳;th 是表頭單元格,td 是標準單元格;每一組 <tr></tr> 代表一列。
合併行用 td 的 rowspan 屬性,合併列則用 colspan 屬性。
(1)合併行:
<td rowspan="跨度的列數">
(2)合併列:
<td colspan="跨度的行數">