表格由 <table> 包裹,內部由列(<tr>)、標題儲存格(<th>)與資料儲存格(<td>)組成。
<table border="1">
<tr><th>姓名</th><th>年齡</th></tr>
<tr><td>小明</td><td>25</td></tr>
</table>
<th colspan="2"> 或 <td colspan="2">,數值為合併的格數。<th rowspan="2"> 或 <td rowspan="2">。注意:使用 rowspan 時,被合併的那一行需省略對應的 <td>,並以 </tr> 明確結束每一列。| 屬性 | 說明 | 單位 |
|---|---|---|
| width / height | 表格寬度 / 高度 | 像素或百分比 |
| border | 外框粗細 | 像素 |
| cellspacing | 儲存格間距(框線間距) | 像素 |
| cellpadding | 內容與儲存格邊框距離 | 像素 |
現代建議:使用 CSS border-collapse: collapse; 替代 cellspacing,以 padding 替代 cellpadding。
align="left|center|right" 可用於 <tr>、<th>、<td>。valign="top|middle|bottom|baseline" 同樣適用於上述三標籤。baseline 以文字基準線對齊,視覺上文字會偏向儲存格上方。設定 <table align="left|right"> 可讓表格像圖片一樣浮動,周圍文字會環繞。若右側空間不足,文字會掉至表格下方。
使用 bgcolor="#顏色代碼" 於 <table>、<tr> 或 <td> 設定背景色(建議改用 CSS background-color)。
<thead>、<tbody>、<tfoot> 增強語意與無障礙。scope="col|row" 於 <th> 輔助螢幕閱讀器。