| 屬性 | 作用 | 值 | 預設 |
|---|---|---|---|
colspan | 橫向跨欄合併 | 正整數 | 1 |
rowspan | 縱向跨列合併 | 正整數 | 1 |
<table>
<caption>Q1 營收彙總 (單位: 萬元)</caption>
<thead>
<tr>
<th colspan="3">第一季</th> <!-- 跨 3 欄 -->
</tr>
<tr>
<th>1 月</th><th>2 月</th><th>3 月</th>
</tr>
</thead>
<tbody>
<tr><td>120</td><td>135</td><td>150</td></tr>
</tbody>
</table>
<table>
<thead>
<tr><th>分類</th><th>品項</th><th>價格</th></tr>
</thead>
<tbody>
<tr>
<td rowspan="3">水果</td> <!-- 跨 3 列 -->
<td>蘋果</td><td>$50</td>
</tr>
<tr><td>香蕉</td><td>$30</td></tr> <!-- 此列只有 2 個 td -->
<tr><td>橘子</td><td>$40</td></tr>
<tr>
<td rowspan="2">蔬菜</td>
<td>高麗菜</td><td>$35</td>
</tr>
<tr><td>胡蘿蔔</td><td>$25</td></tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>專案</th>
<th colspan="2">前端</th> <!-- 跨 2 欄 -->
<th rowspan="2">備註</th> <!-- 跨 2 列 -->
</tr>
<tr>
<th>框架</th><th>工具</th> <!-- 第 2 行表頭補齊 -->
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">專案 A</td> <!-- 跨 2 列 -->
<td>Vue 3</td><td>Vite</td>
<td>進行中</td>
</tr>
<tr>
<td>React 18</td><td>Next.js</td>
<td>規劃中</td>
</tr>
<tr>
<td>專案 B</td>
<td colspan="2">待評估</td> <!-- 跨 2 欄 -->
<td>-</td>
</tr>
</tbody>
</table>
<tr> 的有效儲存格數 = 實際 <td>/<th> 數 + 上一列 rowspan 延伸下來的數量rowspan="3",後續 2 列**必須少寫對應數量的 <td>**合併儲存格會破壞預設的標題-資料對應關係,必須明確標記:
<table>
<thead>
<tr>
<th id="cat" rowspan="2">分類</th>
<th id="item">品項</th>
<th id="price">價格</th>
</tr>
</thead>
<tbody>
<tr>
<td headers="item">蘋果</td>
<td headers="price">$50</td>
</tr>
<tr>
<td headers="cat item">香蕉</td> <!-- 關聯兩個 th -->
<td headers="price">$30</td>
</tr>
</tbody>
</table>
或使用 scope="col|row|colgroup|rowgroup" 於 <th>(簡單表格足夠)。
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 10px 12px; text-align: left; vertical-align: middle; }
th { background: #f5f5f5; font-weight: 600; }
th[rowspan], th[colspan] { background: #eef; } /* 視覺區分合併表頭 */
td[rowspan], td[colspan] { background: #fafafa; }
<div style="overflow-x:auto"><table>...</table></div> 保持結構完整@media (max-width: 600px) 將 tr 變為 display:block,但合併邏輯難以對應,**建議複雜合併表格僅桌面顯示,行動版提供 CSV 下載**rowspan="0" / colspan="0" → 必須 ≥ 1<td> 數量未對應減少<th> 缺少 scope 或 id/headerstable-layout: fixed 加速大表格渲染