萬盛學電腦網

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

HTML 表格寬度控制實戰:CSS 固定佈局、溢出處理與響應式策略

核心問題:為什麼設定寬度還是會被撐開?

瀏覽器預設 table-layout: auto (自動佈局演算法):**根據內容計算欄寬**,設定的 width 僅為「建議值」,若內容 (長單字、大圖片、不換行字串) 超過建議寬度,瀏覽器會**無視設定撐開欄位**,導致版面崩壞。

解決方案核心:table-layout: fixed

table {
  width: 100%;           /* 或固定像素 */
  table-layout: fixed;   /* 關鍵:強制依據  或第一列寬度渲染,忽略內容寬度 */
  border-collapse: collapse;
}

**效果**:欄寬**完全由開發者控制** (透過 <col>、th/td width、CSS width),內容超出將**溢出**而非撐開。渲染速度更快 (一次遍歷即定寬)。

三種寬度定義方式 (搭配 fixed 佈局)

方式語法優先級適用場景
<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;
}

策略四:Tooltip / 展開顯示完整內容 (進階 UX)

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>

郵件模板 (HTML Email) 特殊處理

郵件客戶端 (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 不支援)。

總結檢查清單

  1. ✅ 表格設 table-layout: fixed。
  2. ✅ 用 <colgroup><col style="width:..."> 定義欄寬。
  3. ✅ 儲存格設 overflow: hidden; text-overflow: ellipsis; white-space: nowrap; (單行) 或 word-break: break-word; (多行)。
  4. ✅ 外層包 <div style="overflow-x:auto"> 處理小螢幕。
  5. ✅ 郵件模板雙重寫法 (屬性 + 行內 style),避開 fixed 佈局。

掌握這套組合拳,表格寬度再也不會失控!

copyright © 萬盛學電腦網 all rights reserved