固定 HTML 表格寬高、控制內容溢出行為(截斷、省略號、捲動、換行),是資料密集型介面的核心需求。本文深入解析 `table-layout: fixed` 固定佈局算法、儲存格 `overflow` 處理策略、固定表頭/首欄捲動實作、響應式斷點調整,提供生產級可復用 CSS 模組。
啟用固定佈局後,瀏覽器**不讀取內容**即可根據 `
table.fixed {
table-layout: fixed; /* 關鍵 */
width: 100%; /* 或固定 px */
border-collapse: collapse;
}
table.fixed col:nth-child(1) { width: 30%; }
table.fixed col:nth-child(2) { width: 20%; }
table.fixed col:nth-child(3) { width: 50%; }
| 模式 | CSS 組合 | 視覺效果 | 適用場景 |
|---|---|---|---|
| 截斷省略號 | overflow: hidden; text-overflow: ellipsis; white-space: nowrap; |
單行超寬顯示「...」 | 代號、名稱、金額等單行關鍵欄位 |
| 自動換行 | white-space: normal; word-break: break-word; overflow-wrap: break-word; |
多行顯示,長英文/URL 自動斷行 | 備註、描述、地址等長文本欄位 |
| 儲存格內捲動 | overflow: auto; max-height: 200px; display: block; (需包裝層) |
儲存格內出現捲動軸 | 極長內容(如日誌、程式碼),需保持表格結構 |
<style>
.table-fixed {
width: 100%;
table-layout: fixed;
border-collapse: collapse;
font-size: 0.9rem;
}
/* 欄寬定義 */
.table-fixed col:nth-child(1) { width: 40px; } /* 序號 */
.table-fixed col:nth-child(2) { width: 30%; } /* 名稱 */
.table-fixed col:nth-child(3) { width: 20%; } /* 分類 */
.table-fixed col:nth-child(4) { width: 50%; } /* 描述 */
/* 基礎樣式 */
.table-fixed th,
.table-fixed td {
border: 1px solid #e0e0e0;
padding: 10px 12px;
vertical-align: middle;
}
.table-fixed thead th {
background: #2c3e50;
color: #fff;
font-weight: 600;
position: sticky;
top: 0;
z-index: 10;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.table-fixed tbody tr:nth-child(even) td { background: #f8f9fa; }
.table-fixed tbody tr:hover td { background: #e8f4fd; }
/* 截斷省略:名稱、分類 */
.table-fixed td:nth-child(2),
.table-fixed td:nth-child(3) {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* 自動換行:描述 */
.table-fixed td:nth-child(4) {
white-space: normal;
word-break: break-word;
overflow-wrap: break-word;
max-height: 4.5em; /* 限制最大 3 行 */
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
}
/* 容器限制高度,啟用垂直捲動 */
.table-wrapper {
max-height: 400px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 8px;
}
</style>
<div class="table-wrapper">
<table class="table-fixed">
<colgroup><col><col><col><col></colgroup>
<thead>
<tr><th>#</th><th>產品名稱</th><th>分類</th><th>詳細描述</th></tr>
</thead>
<tbody>
<tr><td>1</td><td>超長產品名稱超長產品名稱超長產品名稱</td><td>電子產品</td><td>這是一段很長的描述文字,會自動換行並在超過三行時以省略號結尾,保持表格整齊不被撐高。</td></tr>
<!-- 更多列 -->
</tbody>
</table>
</div>
<style>
.table-sticky-col {
table-layout: fixed;
width: 100%;
border-collapse: collapse;
}
.table-sticky-col th:first-child,
.table-sticky-col td:first-child {
position: sticky;
left: 0;
background: #fff; /* 必須有實體背景,避免捲動時穿透 */
z-index: 5;
box-shadow: 2px 0 4px rgba(0,0,0,0.05);
border-right: 2px solid #e0e0e0;
}
.table-sticky-col thead th:first-child { z-index: 15; } /* 表頭首欄最高層級 */
/* 容器需設定 overflow-x: auto */
.table-h-scroll { overflow-x: auto; max-width: 100%; }
</style>
<div class="table-h-scroll">
<table class="table-sticky-col table-fixed">...</table>
</div>
/* 桌面:固定佈局 + 省略號 */
@media (min-width: 1024px) {
.table-responsive { table-layout: fixed; }
.table-responsive td { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
}
/* 平板:允許部分欄位換行 */
@media (max-width: 1023px) and (min-width: 640px) {
.table-responsive td:nth-last-child(-n+2) { white-space: normal; word-break: break-word; }
}
/* 手機:重組為卡片或啟用橫向捲動 */
@media (max-width: 639px) {
.table-responsive { display: block; overflow-x: auto; white-space: nowrap; }
.table-responsive thead { display: table-header-group; }
.table-responsive tbody { display: table-row-group; }
.table-responsive td { white-space: normal; padding: 8px 12px; }
}
// 1. 自動調整欄寬適應內容(僅適用 table-layout: auto)
function autoFitColumns(table) {
table.style.tableLayout = 'auto';
// 強制重排
table.offsetWidth;
table.style.tableLayout = 'fixed';
}
// 2. 點擊展開完整內容
document.querySelectorAll('.table-fixed td[data-full]').forEach(td => {
td.style.cursor = 'pointer';
td.addEventListener('click', () => {
const full = td.dataset.full;
if (td.dataset.expanded) {
td.textContent = td.dataset.short;
delete td.dataset.expanded;
} else {
td.dataset.short = td.textContent;
td.textContent = full;
td.dataset.expanded = '1';
td.style.whiteSpace = 'normal';
td.style.overflow = 'visible';
td.style.textOverflow = 'clip';
}
});
});
表格固定寬高與溢出處理的黃金組合:
table-layout: fixed + `overflow: hidden; text-overflow: ellipsis; white-space: nowrap 截斷單行display: -webkit-box; -webkit-line-clamp: N; -webkit-box-orient: vertical 多行截斷max-height + overflow-y: auto 垂直捲動 + thead th { position: sticky; top: 0 } 固定表頭td:first-child { position: sticky; left: 0; background: #fff }將上述模組化為設計系統元件(`DataTable`),即可在專案中統一、高效地處理所有固定寬高表格需求。