瀏覽器預設 table-layout: auto (自動佈局演算法):**根據內容計算欄寬**,設定的 width 僅為「建議值」,若內容 (長單字、大圖片、不換行字串) 超過建議寬度,瀏覽器會**無視設定撐開欄位**,導致版面崩壞。
table-layout: fixedtable {
width: 100%; /* 或固定像素 */
table-layout: fixed; /* 關鍵:強制依據 或第一列寬度渲染,忽略內容寬度 */
border-collapse: collapse;
}
**效果**:欄寬**完全由開發者控制** (透過 <col>、th/td width、CSS width),內容超出將**溢出**而非撐開。渲染速度更快 (一次遍歷即定寬)。
| 方式 | 語法 | 優先級 | 適用場景 |
|---|---|---|---|
| <colgroup> + <col> (推薦) | <col style="width: 120px;"><col style="width: 30%;"> | 最高 | 語意清晰、樣式集中、易維護、支援 span 合併欄 |
| CSS 類別 | th.col-id { width: 50px; } | 次之 | 元件化開發、配合 JS 動態切換欄寬 |
| 行內 style / width 屬性 | <th style="width: 100px;"> | 最低 | 快速原型、動態產生、郵件模板 |
啟用 table-layout: fixed 後,內容超出欄寬會溢出。必須搭配儲存格 CSS 處理:
td, th {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
/* 必須三者同時設定才生效 */
}
td, th {
white-space: normal; /* 允許換行 */
word-break: break-word; /* 單字過長在邊界斷行 (現代標準) */
/* 或舊版相容:word-wrap: break-word; */
/* 極端情況:word-break: break-all; (字母間也斷行) */
}
/* 整張表格橫向滾動 - 推薦響應式 */
.table-wrapper {
overflow-x: auto;
-webkit-overflow-scrolling: touch; /* iOS 惯性滾動 */
}
table { min-width: 800px; } /* 確保有滾動空間 */
/* 單一儲存格滾動 (罕用) */
td.scrollable {
display: block; /* 必須為 block/inline-block */
max-width: 200px;
overflow-x: auto;
white-space: nowrap;
}
td {
position: relative;
}
td[data-full]::after {
content: attr(data-full);
position: absolute;
left: 0; top: 100%;
background: #333; color: #fff;
padding: 4px 8px; border-radius: 4px;
white-space: nowrap; z-index: 10;
opacity: 0; visibility: hidden; transition: 0.2s;
}
td:hover::after { opacity: 1; visibility: visible; }
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>響應式固定寬度表格</title>
<style>
.table-container { overflow-x: auto; margin: 1rem 0; border: 1px solid #eee; border-radius: 4px; }
.fixed-table {
width: 100%; min-width: 800px; /* 觸發容器滾動 */
border-collapse: collapse;
table-layout: fixed;
font-size: 0.9rem;
}
.fixed-table col:nth-child(1) { width: 50px; }
.fixed-table col:nth-child(2) { width: 25%; }
.fixed-table col:nth-child(3) { width: 120px; }
.fixed-table col:nth-child(4) { width: 100px; }
.fixed-table col:last-child { width: auto; } /* 剩餘空間 */
.fixed-table th, .fixed-table td {
border: 1px solid #e0e0e0; padding: 10px 12px;
vertical-align: middle; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.fixed-table thead th { background: #f8f9fa; font-weight: 600; position: sticky; top: 0; z-index: 1; }
.fixed-table tbody tr:nth-child(odd) { background: #fdfdfd; }
.fixed-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.fixed-table .wrap { white-space: normal; word-break: break-word; }
</style>
</head>
<body>
<div class="table-container">
<table class="fixed-table">
<colgroup><col><col><col><col><col></colgroup>
<thead><tr><th>ID</th><th>商品名稱</th><th class="num">價格</th><th class="num">庫存</th><th class="wrap">詳細描述</th></tr></thead>
<tbody>
<tr><td>1</td><td>超長商品名稱會自動截斷顯示省略號...</td><td class="num">$1,234</td><td class="num">99</td><td class="wrap">這是一段很長的描述文字,在寬度足夠時會正常換行顯示。</td></tr>
<tr><td>2</td><td>短名稱</td><td class="num">$50</td><td class="num">200</td><td class="wrap">簡短描述</td></tr>
</tbody>
</table>
</div>
</body>
</html>
郵件客戶端 (Outlook, Gmail App) CSS 支援有限,**必須使用行內 style + HTML 屬性雙重保險**:
<table width="600" border="0" cellpadding="0" cellspacing="0" style="width:600px; max-width:100%; border-collapse:collapse;">
<tr>
<td width="150" style="width:150px; padding:10px;">固定欄</td>
<td style="padding:10px;">自適應欄</td>
</tr>
</table>
table 設 width="600" style="width:600px; max-width:100%" 兼容桌面/手機。td 同時寫 width="150" 和 style="width:150px;"。table-layout: fixed、<colgroup>、border-spacing、text-overflow: ellipsis (Outlook 不支援)。table-layout: fixed。<colgroup><col style="width:..."> 定義欄寬。overflow: hidden; text-overflow: ellipsis; white-space: nowrap; (單行) 或 word-break: break-word; (多行)。<div style="overflow-x:auto"> 處理小螢幕。fixed 佈局。掌握這套組合拳,表格寬度再也不會失控!