萬盛學電腦網

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

HTML 表格固定寬高與內容溢出處理:CSS table-layout、overflow 與響應式最佳實踐

固定 HTML 表格寬高、控制內容溢出行為(截斷、省略號、捲動、換行),是資料密集型介面的核心需求。本文深入解析 `table-layout: fixed` 固定佈局算法、儲存格 `overflow` 處理策略、固定表頭/首欄捲動實作、響應式斷點調整,提供生產級可復用 CSS 模組。

核心概念:table-layout: fixed

啟用固定佈局後,瀏覽器**不讀取內容**即可根據 ``、``/`` 的 `width` 瞬間計算欄寬,效能大幅提升,且欄寬**不會被長內容撐大**。

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; }
}

JavaScript 輔助:動態計算欄寬、展開/收合長文本

// 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';
    }
  });
});

常見問題 FAQ

Q: `table-layout: fixed` 設定後,寬度仍被撐大?
A: 確認 `` 或首列 ``/`` 有明確 `width`,**且總和 ≤ 表格寬度**;檢查是否有 `white-space: nowrap` 導致內容無法換行(固定佈局下不會撐大,但會溢出)。
Q: `position: sticky` 表頭不生效?
A> 父容器(或祖先)不可有 `overflow: hidden/auto/scroll`(除非是捲動容器本身);`th` 需指定實體 `background-color`;`border-collapse: collapse` 環境下 sticky 相容性最佳。
Q: 固定首欄捲動時背景穿透?
A: 固定欄**必須設定實體 `background-color`**(不能透明),並給予 `z-index` 高於其他儲存格。
Q: 列印時固定表頭/首欄錯位?
A: `@media print` 中重置 `position: static; overflow: visible;`。

總結

表格固定寬高與溢出處理的黃金組合:

  1. table-layout: fixed + `` 定義欄寬
  2. 儲存格 overflow: hidden; text-overflow: ellipsis; white-space: nowrap 截斷單行
  3. 長文本欄位 display: -webkit-box; -webkit-line-clamp: N; -webkit-box-orient: vertical 多行截斷
  4. 容器 max-height + overflow-y: auto 垂直捲動 + thead th { position: sticky; top: 0 } 固定表頭
  5. 首欄固定 td:first-child { position: sticky; left: 0; background: #fff }
  6. 響應式斷點切換:桌面固定佈局 → 平板部分鬆綁 → 手機卡片/橫向捲動

將上述模組化為設計系統元件(`DataTable`),即可在專案中統一、高效地處理所有固定寬高表格需求。

copyright © 萬盛學電腦網 all rights reserved