萬盛學電腦網

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

HTML 表格寬度控制完全指南:table-layout、CSS 寬度、響應式與溢出處理

表格寬度計算的兩種模式

CSS table-layout 屬性決定瀏覽器如何計算欄寬,是表格寬度控制的核心開關。

模式值計算方式效能可控性適用場景
自動佈局auto (預設)分析所有單元格內容,計算最適寬度慢(需完整渲染)低(內容決定寬度)內容不固定、欄數少、需自適應
固定佈局fixed依 <col>、<th>、第一列寬度、平均分配快(首列即定)高(開發者控制)大量資料、固定欄寬、響應式、效能敏感

固定佈局最佳實踐(推薦)

.table-fixed {
  table-layout: fixed;
  width: 100%; /* 或具體像素 */
}

.table-fixed col:nth-child(1) { width: 40px; }   /* 固定 40px:勾選框/序號 */
.table-fixed col:nth-child(2) { width: 20%; }   /* 百分比:名稱 */
.table-fixed col:nth-child(3) { width: 150px; } /* 固定 px:日期 */
.table-fixed col:nth-child(4) { width: auto; }  /* 剩餘空間:描述 */

/* 對應 HTML */
<table class="table-fixed">
  <colgroup>
    <col><col><col><col>
  </colgroup>
  <thead>...</thead>
  <tbody>...</tbody>
</table>

傳統屬性寫法(相容舊專案)

<table width="800">
  <colgroup>
    <col width="50">
    <col width="200">
    <col width="*> <!-- 剩餘空間 -->
  </colgroup>
  <tr>
    <td width="50">...</td>
    <td width="200">...</td>
    <td>...</td>
  </tr>
</table>

內容溢出處理三大策略

策略CSS視覺效果適用場景
截斷省略號overflow: hidden; text-overflow: ellipsis; white-space: nowrap;單行超出顯示 ...固定寬欄位、ID、代碼、狀態
多行截斷display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;多行超出顯示 ...描述、摘要、備註欄
換行顯示word-break: break-word; overflow-wrap: break-word; white-space: normal;長字串自動換行網址、長英文、JSON 片段
水平捲動td { white-space: nowrap; } + 容器 overflow-x: auto保持原樣,容器可捲動完整保留格式、程式碼、表格整體

實戰:混合策略表格

.table-mixed {
  table-layout: fixed;
  width: 100%;
}
.table-mixed .col-id { width: 60px; }
.table-mixed .col-name { width: 25%; }
.table-mixed .col-desc { width: auto; } /* 彈性剩餘 */

.table-mixed .col-id td { 
  text-align: center; 
  white-space: nowrap; 
  overflow: hidden; 
  text-overflow: ellipsis; 
}
.table-mixed .col-name td { 
  white-space: nowrap; 
  overflow: hidden; 
  text-overflow: ellipsis; 
}
.table-mixed .col-desc td { 
  word-break: break-word; 
  overflow-wrap: break-word; 
  white-space: normal; 
}

響應式斷點調整

/* 桌面:固定佈局 */
@media (min-width: 1024px) {
  .table-responsive { table-layout: fixed; }
}

/* 平板:部分欄位合併/隱藏 */
@media (max-width: 1023px) {
  .table-responsive .col-desc { display: none; }
  .table-responsive .col-name { width: 50%; }
}

/* 手機:卡片模式 */
@media (max-width: 640px) {
  .table-responsive { display: block; overflow-x: auto; }
  /* 或啟用卡片模式 .table-card-view */
}

JavaScript 動態調整欄寬

function autoFitColumns(table, minWidth = 80) {
  // 僅適用 table-layout: auto
  if (getComputedStyle(table).tableLayout === 'fixed') return;
  
  const cols = table.querySelectorAll('col');
  const headerCells = table.querySelectorAll('thead th');
  
  headerCells.forEach((th, i) => {
    const contentWidth = th.scrollWidth;
    const style = getComputedStyle(th);
    const padding = parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
    const targetWidth = Math.max(minWidth, contentWidth + padding + 10); // 緩衝
    
    if (cols[i]) cols[i].style.width = `${targetWidth}px`;
    else th.style.width = `${targetWidth}px`;
  });
}

// 使用:資料載入後呼叫
fetchData().then(data => {
  renderTable(data);
  autoFitColumns(document.querySelector('#data-table'));
});

無障礙與列印考量

  • 截斷內容提供 title 屬性或 Tooltip 顯示完整內容
  • 列印樣式移除 text-overflow: ellipsis,改用 white-space: normal
  • 固定佈局下 <col> 寬度總和超過表格寬度會被忽略,瀏覽器按比例縮放

掌握 table-layout: fixed 搭配 <colgroup> 定寬、三種溢出策略靈活組合、響應式斷點切換,能讓表格在任何裝置、任何資料量下都呈現穩定、可預期的寬度表現。

copyright © 萬盛學電腦網 all rights reserved