萬盛學電腦網

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

HTML 表格寬度控制:傳統 width 屬性與 CSS 現代響應式布局對比

控制 HTML 表格寬度,早期使用 `

`、``、``、`th`/`td` 的 `width` **立即計算欄寬**,不等待內容
  • 渲染速度快,適合大數據表格
  • 欄寬可預測、可控,**內容超寬不會撐大欄位**(需配合 `overflow` 處理)
  • 寬度總和不足 100% 時,剩餘空間按比例分配給無寬度欄位
  • 固定佈局最佳實踐範例

    <style>
    .table-fixed {
      width: 100%;
      table-layout: fixed;        /* 關鍵:啟用固定佈局 */
      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: 20%; }   /* 數量 */
    .table-fixed col:nth-child(4) { width: 30%; }   /* 備註(自適應剩餘) */
    .table-fixed th,
    .table-fixed td {
      border: 1px solid #ddd;
      padding: 10px 12px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;        /* 超寬截斷省略號 */
    }
    .table-fixed td:nth-child(4) {
      white-space: normal;        /* 備註欄允許換行 */
      word-break: break-word;
    }
    </style>
    
    <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>超長產品名稱超長產品名稱超長產品名稱</td><td>$999</td><td>10</td><td>這是一段很長的備註文字會自動換行顯示</td></tr>
      </tbody>
    </table>

    響應式表格寬度策略

    策略一:容器限制 + 橫向捲動(資料密集型)

    .table-wrapper {
      overflow-x: auto;
      -webkit-overflow-scrolling: touch; /* iOS 惯性滑動 */
    }
    .table-responsive {
      min-width: 600px; /* 確保最小可讀寬度 */
      width: 100%;
    }

    策略二:斷點重組為卡片/列表(關鍵資訊優先)

    @media (max-width: 600px) {
      .table-card thead { display: none; } /* 隱藏表頭 */
      .table-card tr { display: block; margin-bottom: 1rem; border: 1px solid #ddd; border-radius: 8px; padding: 1rem; background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,0.05); }
      .table-card td { display: flex; justify-content: space-between; padding: 0.5rem 0; border-bottom: 1px solid #eee; }
      .table-card td:last-child { border-bottom: none; }
      .table-card td::before { content: attr(data-label); font-weight: 600; color: #666; } /* 用 data-label 顯示欄位名 */
    }
    
    /* HTML 需加 data-label */
    <td data-label="產品">筆電</td>

    策略三:關鍵欄固定,其餘捲動(進階)

    .table-sticky-col th:first-child,
    .table-sticky-col td:first-child {
      position: sticky;
      left: 0;
      background: #fff; /* 需實體背景避免穿透 */
      z-index: 1;
      box-shadow: 2px 0 4px rgba(0,0,0,0.05);
    }

    完整專案級表格寬度模組

    /* table-width.css */
    .table {
      width: 100%;
      max-width: 100%;
      border-collapse: collapse;
      table-layout: fixed; /* 預設固定佈局,效能佳 */
    }
    .table-auto { table-layout: auto; } /* 需自動佈局時覆蓋 */
    
    .table th, .table td {
      padding: 0.75rem 1rem;
      border: 1px solid #e0e0e0;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .table td[data-wrap], .table th[data-wrap] { white-space: normal; word-break: break-word; }
    
    /* 響應式:小螢幕切換卡片 */
    @media (max-width: 640px) {
      .table-responsive thead { display: none; }
      .table-responsive tr { display: block; margin-bottom: 1rem; border: 1px solid #ddd; border-radius: 6px; background: #fff; }
      .table-responsive td { display: flex; justify-content: space-between; padding: 0.5rem; border-bottom: 1px solid #f0f0f0; }
      .table-responsive td:last-child { border-bottom: none; }
      .table-responsive td::before { content: attr(data-label); font-weight: 500; color: #555; }
    }

    總結

    表格寬度控制現代最佳實踐:

    1. 預設使用 `table-layout: fixed`,配合 `
    ` 預定義欄寬。
  • 容器加 `overflow-x: auto`** 實現響應式橫向捲動,或用 Media Query 重組為卡片。
  • 內容截斷用 `overflow: hidden; text-overflow: ellipsis; white-space: nowrap`**,需換行欄位加 `white-space: normal`。
  • 完全棄用 HTML `width`、`cellpadding`、`cellspacing` 屬性**,全權交給 CSS。
  • 將上述 CSS 封裝為專案設計系統元件,即可在任何專案一致、高效地處理表格寬度與響應式需求。

    copyright © 萬盛學電腦網 all rights reserved
    ` 等屬性,現代開發完全改用 CSS `width`、`max-width`、`table-layout` 等屬性,並結合響應式設計原則。本文對比新舊寫法,深入解析 `table-layout: fixed` 與 `auto` 的演算差異、欄寬分配策略、響應式斷點處理,提供可直接套用的最佳實踐模組。

    傳統 HTML width 屬性(已廢棄,僅供維舊參考)

    <table width="400">
      <tr>
        <td width="100"></td>
        <td width="100"></td>
        <td width="100"></td>
        <td width="100"></td>
      </tr>
    </table>

    缺點:

    • 混合結構與表現,違反關注點分離
    • 無法響應式調整,固定像素在行動裝置易溢出
    • 各瀏覽器對百分比與像素混用的計算邏輯不一致
    • 無法控制最小/最大寬度、內容溢出處理

    CSS 現代寬度控制核心屬性

    ``
    屬性作用對象說明典型值
    width`table`, `th`, `td`, `col`, `colgroup`設定寬度,百分比相對父容器,像素為固定值`100%`, `600px`, `30%`
    max-width/min-width`table`, `th`, `td`限制最大/最小寬度,防止過寬或過窄`max-width: 1200px; min-width: 320px;`
    table-layout`table`關鍵:`auto`(預設,依內容計算)vs `fixed`(依寬度屬性計算,效能佳、可控)`fixed` / `auto`
    width on `
    `col`, `colgroup`預先定義欄寬,配合 `table-layout: fixed` 即時生效,無需等待內容載入`
    overflow, `text-overflow`, `white-space``td`, `th`內容超寬時處理:截斷省略、換行、捲動`overflow: hidden; text-overflow: ellipsis; white-space: nowrap;`

    table-layout: fixed vs auto 深度解析

    auto(預設,自動佈局)

    • 瀏覽器讀取**所有儲存格內容**後計算最佳欄寬
    • 大表格渲染較慢(需完整解析內容)
    • 欄寬不可預測,長內容會撐大欄位

    fixed(固定佈局,推薦)

    • 依 `