萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML 表格固定寬高完整解法:CSS 寬高、table-layout、溢位處理與響應式權衡

核心 CSS 組合

table.fixed {
  table-layout: fixed;      /* 關鍵:固定佈局,只看第一行或 colgroup 決定寬度 */
  width: 100%;              /* 或固定 px/rem */
  border-collapse: collapse;
}

table.fixed td,
table.fixed th {
  /* 固定高度(可選) */
  height: 48px;             /* 或 min-height 配合 vertical-align */
  
  /* 溢位處理三選一 */
  overflow: hidden;         /* 直接切掉 */
  text-overflow: ellipsis;  /* 單行省略號,需 white-space: nowrap */
  /* white-space: nowrap; */ /* 單行不換行 */
  
  /* 或多行省略(現代瀏覽器) */
  /* display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; */
  
  padding: 0.5rem;
  border: 1px solid #ddd;
}

為什麼需要 `table-layout: fixed`?

` 定義
模式演算法效能寬度可控性適用場景
`auto` (預設)掃描**所有儲存格內容**計算最寬慢(大表格明顯)低(內容長會撐大)小型、內容不固定表格
`fixed`只看**第一行**或 `
**極快**(大表格首選)**高**(完全由 CSS 控制)大量資料、固定版面、需截斷

三種定義欄寬方式(配合 `fixed` 最佳)

1. `` + ``(語意最清晰、維護最易)

<table class="fixed">
  <colgroup>
    <col style="width: 60px;">   <!-- ID -->
    <col style="width: 30%;">   <!-- 名稱 -->
    <col style="width: 50%;">   <!-- 描述 -->
    <col style="width: 120px;"> <!-- 操作 -->
  </colgroup>
  <thead>...</thead>
  <tbody>...</tbody>
</table>

2. CSS `nth-child` 選擇器(無需改 HTML)

.fixed td:nth-child(1), .fixed th:nth-child(1) { width: 60px; }
.fixed td:nth-child(2), .fixed th:nth_child(2) { width: 30%; }
.fixed td:nth-child(3), .fixed th:nth_child(3) { width: 50%; }
.fixed td:nth-child(4), .fixed th:nth_child(4) { width: 120px; }

3. 第一列儲存格直接寫 `width`/`style`(快速測試)

<tr>
  <th style="width: 60px;">ID</th>
  <th style="width: 30%;">名稱</th>
  <th style="width: 50%;">描述</th>
  <th style="width: 120px;">操作</th>
</tr>

固定高度與垂直對齊

/* 固定高度 + 垂直置中 + 單行省略 */
.fixed td, .fixed th {
  height: 48px;              /* 固定高 */
  vertical-align: middle;    /* 垂直置中 */
  white-space: nowrap;       /* 強制單行 */
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 多行固定高度 + 多行省略(WebKit/Blink) */
.fixed .multi-line {
  height: 72px;              /* 約 2 行 */
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  /* 注意:不支援 Firefox 舊版,可用 JS 方案補足 */
}

響應式權衡:固定 vs 流動

方案 A:固定寬度 + 水平捲動(資料完整性優先)

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.fixed { min-width: 800px; } /* 強制最小寬觸發捲動 */

方案 B:行動裝置轉卡片模式(體驗優先)

@media (max-width: 640px) {
  .table-wrap { overflow-x: visible; }
  .fixed { table-layout: auto; width: 100%; min-width: 0; }
  .fixed thead { display: none; }
  .fixed tbody, .fixed tr, .fixed td { display: block; width: 100%; }
  .fixed td { 
    display: flex; justify-content: space-between; padding: 0.5rem; border-bottom: 1px solid #eee;
  }
  .fixed td::before { content: attr(data-label); font-weight: 600; }
  /* 固定高度在卡片模式通常移除 */
  .fixed td { height: auto; white-space: normal; overflow: visible; }
}

方案 C:關鍵欄固定(Sticky Column)

.fixed th:first-child, .fixed td:first-child {
  position: sticky; left: 0; background: #fff; z-index: 1;
  box-shadow: 2px 0 4px rgba(0,0,0,0.1);
}

常見坑點排查

` 明確定義、避免合併關鍵欄
現象原因解決
設定 `width` 但欄位仍被撐大未加 `table-layout: fixed`加上 `table-layout: fixed`
`colspan` 合併後寬度錯亂合併儲存格不參與固定計算用 `
固定高度但內容溢出不隱藏`overflow: hidden` 需搭配 `display: block/table-cell` 且有高度確認 `display` 未被覆蓋、高度生效、無 `min-height` 干擾
行動版卡片模式欄位順序錯亂CSS 選擇器權重、HTML 結構順序用 `data-label` 明確對應、檢查 DOM 順序

最佳實踐清單

  1. **預設開啟 `table-layout: fixed`**,大表格效能提升顯著。
  2. **用 `` 定義欄寬**,語意清晰、不受內容干擾、易維護。
  3. **固定高度僅用於單行/預知行數**,多行文字改用 `max-height` + `overflow: auto` 或卡片模式。
  4. **溢位必處理**:`overflow: hidden` + `text-overflow: ellipsis`(單行)或 `word-break: break-all`(強制斷行)。
  5. **行動裝置提供替代呈現**(捲動/卡片/固定欄),勿強迫縮小字體。

掌握 `table-layout: fixed` 與 ``,表格寬高完全受控,效能與體驗雙贏。

copyright © 萬盛學電腦網 all rights reserved