CSS table-layout 屬性決定瀏覽器如何計算欄寬,是表格寬度控制的核心開關。
| 模式 | 值 | 計算方式 | 效能 | 可控性 | 適用場景 |
|---|---|---|---|---|---|
| 自動佈局 | auto (預設) | 分析所有單元格內容,計算最適寬度 | 慢(需完整渲染) | 低(內容決定寬度) | 內容不固定、欄數少、需自適應 |
| 固定佈局 | fixed | 依 <col>、<th>、第一列寬度、平均分配 | 快(首列即定) | 高(開發者控制) | 大量資料、固定欄寬、響應式、效能敏感 |
.table-fixed {
table-layout: fixed;
width: 100%; /* 或具體像素 */
}
.table-fixed col:nth-child(1) { width: 40px; } /* 固定 40px:勾選框/序號 */
.table-fixed col:nth-child(2) { width: 20%; } /* 百分比:名稱 */
.table-fixed col:nth-child(3) { width: 150px; } /* 固定 px:日期 */
.table-fixed col:nth-child(4) { width: auto; } /* 剩餘空間:描述 */
/* 對應 HTML */
<table class="table-fixed">
<colgroup>
<col><col><col><col>
</colgroup>
<thead>...</thead>
<tbody>...</tbody>
</table>
<table width="800">
<colgroup>
<col width="50">
<col width="200">
<col width="*> <!-- 剩餘空間 -->
</colgroup>
<tr>
<td width="50">...</td>
<td width="200">...</td>
<td>...</td>
</tr>
</table>
| 策略 | CSS | 視覺效果 | 適用場景 |
|---|---|---|---|
| 截斷省略號 | overflow: hidden; text-overflow: ellipsis; white-space: nowrap; | 單行超出顯示 ... | 固定寬欄位、ID、代碼、狀態 |
| 多行截斷 | display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; | 多行超出顯示 ... | 描述、摘要、備註欄 |
| 換行顯示 | word-break: break-word; overflow-wrap: break-word; white-space: normal; | 長字串自動換行 | 網址、長英文、JSON 片段 |
| 水平捲動 | td { white-space: nowrap; } + 容器 overflow-x: auto | 保持原樣,容器可捲動 | 完整保留格式、程式碼、表格整體 |
.table-mixed {
table-layout: fixed;
width: 100%;
}
.table-mixed .col-id { width: 60px; }
.table-mixed .col-name { width: 25%; }
.table-mixed .col-desc { width: auto; } /* 彈性剩餘 */
.table-mixed .col-id td {
text-align: center;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.table-mixed .col-name td {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.table-mixed .col-desc td {
word-break: break-word;
overflow-wrap: break-word;
white-space: normal;
}
/* 桌面:固定佈局 */
@media (min-width: 1024px) {
.table-responsive { table-layout: fixed; }
}
/* 平板:部分欄位合併/隱藏 */
@media (max-width: 1023px) {
.table-responsive .col-desc { display: none; }
.table-responsive .col-name { width: 50%; }
}
/* 手機:卡片模式 */
@media (max-width: 640px) {
.table-responsive { display: block; overflow-x: auto; }
/* 或啟用卡片模式 .table-card-view */
}
function autoFitColumns(table, minWidth = 80) {
// 僅適用 table-layout: auto
if (getComputedStyle(table).tableLayout === 'fixed') return;
const cols = table.querySelectorAll('col');
const headerCells = table.querySelectorAll('thead th');
headerCells.forEach((th, i) => {
const contentWidth = th.scrollWidth;
const style = getComputedStyle(th);
const padding = parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
const targetWidth = Math.max(minWidth, contentWidth + padding + 10); // 緩衝
if (cols[i]) cols[i].style.width = `${targetWidth}px`;
else th.style.width = `${targetWidth}px`;
});
}
// 使用:資料載入後呼叫
fetchData().then(data => {
renderTable(data);
autoFitColumns(document.querySelector('#data-table'));
});
title 屬性或 Tooltip 顯示完整內容text-overflow: ellipsis,改用 white-space: normal<col> 寬度總和超過表格寬度會被忽略,瀏覽器按比例縮放掌握 table-layout: fixed 搭配 <colgroup> 定寬、三種溢出策略靈活組合、響應式斷點切換,能讓表格在任何裝置、任何資料量下都呈現穩定、可預期的寬度表現。