<table width="400">
<tr>
<td width="100">A</td>
<td width="100">B</td>
<td width="100">C</td>
<td width="100">D</td>
</tr>
</table>
<style>
.fixed-width-table {
table-layout: fixed; /* 關鍵:固定版面演算法 */
width: 100%;
}
.fixed-width-table col:nth-child(1) { width: 15%; }
.fixed-width-table col:nth-child(2) { width: 35%; }
.fixed-width-table col:nth-child(3) { width: 50%; }
/* 儲存格溢位處理 */
.fixed-width-table td {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
padding: 8px 12px;
}
</style>
<table class="fixed-width-table">
<colgroup>
<col><col><col>
</colgroup>
<thead><tr><th>欄位 A</th><th>欄位 B</th><th>欄位 C</th></tr></thead>
<tbody>
<tr><td>內容不會撐大欄寬</td><td>超長文字會自動截斷加省略號</td><td>固定 50%</td></tr>
</tbody>
</table>
| 模式 | 演算法 | 速度 | 欄寬受內容影響 | 適用場景 |
|---|---|---|---|---|
auto (預設) | 讀取所有內容計算 | 慢 (大表格明顯) | 是 | 內容決定寬度、簡單表格 |
fixed | 依 col/首列 td 寬度一次定案 | 快 | 否 | 資料密集、固定版面、大型表格 |
table-layout: fixed; width: 100%;。<colgroup><col style="width:..."> 宣告欄寬 (支援 px, %, fr-like 比例)。overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 防撐破。overflow-wrap: anywhere; 並移除 white-space: nowrap。固定版面模式是大型資料表格效能與視覺穩定的關鍵。