萬盛學電腦網

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

HTML 表格垂直置中與水平置中完整解法:Flexbox、Grid 與傳統 table-cell 方法對比

讓表格內容在儲存格中垂直置中、水平置中,是常見排版需求。現代 CSS 提供 Flexbox、Grid 等強大佈局模組,但 `` 預設 `display: table-cell` 仍有其獨特優勢。本文系統比較三種主流垂直置中方案、水平置中技巧、響應式處理與瀏覽器相容性,提供可直接套用的程式碼模組。

三種垂直置中方案對比

方案核心 CSS優點缺點/限制適用場景
table-cell (傳統) vertical-align: middle 原生支援、無需額外容器、IE8+ 完美相容 無法對 `` 內部元素再用 Flex/Grid、高度由內容撐開 簡單表格、郵件模板、舊專案維護
Flexbox display: flex; align-items: center; justify-content: center 功能強大、支援多軸對齊、可輕鬆處理多行文字、圖文混排 需在 `` 內加包裝 `
`、改變 `display` 可能影響表格佈局算法
現代專案、複雜儲存格內容、需響應式
CSS Grid display: grid; place-items: center 語意清晰、單行程式碼完成雙軸置中、支援隱式軌道 IE 不支援、同樣需包裝層 現代專案、簡潔程式碼偏好

方案一:傳統 table-cell(零包裝層)

<style>
.table-vcenter td,
.table-vcenter th {
  vertical-align: middle;   /* 關鍵:垂直置中 */
  text-align: center;       /* 水平置中 */
  padding: 16px;
  border: 1px solid #ddd;
}
</style>

<table class="table-vcenter">
  <tr>
    <td style="height: 100px;">垂直置中文字</td>
    <td><img src="icon.png" alt=""> 圖片與文字</td>
  </tr>
</table>

⚠️ 若 `` 內有區塊元素(`

`、`

`),`vertical-align` 可能失效,需改用 Flex/Grid。

方案二:Flexbox 包裝層(推薦現代專案)

<style>
.table-flex td,
.table-flex th {
  padding: 0; /* 移除預設 padding,由內層控制 */
}
.cell-flex {
  display: flex;
  align-items: center;      /* 垂直置中 */
  justify-content: center;  /* 水平置中 */
  height: 100%;             /* 撐滿儲存格高度 */
  padding: 12px 16px;       /* 內距移到這裡 */
  box-sizing: border-box;
}
/* 多行文字自動換行、置中 */
.cell-flex.multi {
  flex-direction: column;
  text-align: center;
}
</style>

<table class="table-flex">
  <tr>
    <td><div class="cell-flex">單行垂直置中</div></td>
    <td><div class="cell-flex multi">多行
文字
垂直置中</div></td> <td><div class="cell-flex"><img src="icon.svg" alt=""> 圖文混排</div></td> </tr> </table>

方案三:CSS Grid 包裝層(最簡潔)

<style>
.cell-grid {
  display: grid;
  place-items: center;      /* 等同 align-items: center; justify-items: center */
  height: 100%;
  padding: 12px 16px;
  box-sizing: border-box;
}
</style>

<td><div class="cell-grid">Grid 雙軸置中</div></td>

表格容器層級置中:整個表格在頁面置中

<style>
.page-center {
  display: flex;
  min-height: 100vh;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}
.table-outer {
  max-width: 90%;
  width: 100%;
}
</style>

<div class="page-center">
  <table class="table-outer table-vcenter">...</table>
</div>

響應式處理:小螢幕調整置中策略

@media (max-width: 600px) {
  .table-flex td { padding: 8px; }
  .cell-flex { padding: 8px 12px; }
  .cell-flex.multi { flex-direction: column; }
  /* 極窄螢幕改為左對齊,避免文字過度擠壓 */
  .table-flex td:first-child .cell-flex { justify-content: flex-start; text-align: left; }
}

完整專案級置中模組

/* table-align.css */
.table-align {
  border-collapse: collapse;
  width: 100%;
}
.table-align th,
.table-align td {
  border: 1px solid #e0e0e0;
  vertical-align: middle; /* 基礎垂直置中 */
}

/* 水平對齊工具類 */
.ta-left   { text-align: left; }
.ta-center { text-align: center; }
.ta-right  { text-align: right; }

/* 進階:Flex 包裝層(需在 HTML 加入 
) */ .cell-flex { display: flex; align-items: center; justify-content: center; height: 100%; padding: 12px 16px; box-sizing: border-box; } .cell-flex.start { justify-content: flex-start; } .cell-flex.end { justify-content: flex-end; } .cell-flex.between { justify-content: space-between; } .cell-flex.column { flex-direction: column; text-align: center; } /* 進階:Grid 包裝層 */ .cell-grid { display: grid; place-items: center; height: 100%; padding: 12px 16px; box-sizing: border-box; } /* 響應式調整 */ @media (max-width: 640px) { .cell-flex, .cell-grid { padding: 8px 12px; } .table-align td:first-child .cell-flex { justify-content: flex-start; } }

無障礙與語意提醒

  • 置中純屬視覺表現,**不改變語意順序**,螢幕閱讀器仍按 DOM 順序讀取。
  • 若置中導致內容順序與視覺順序不符,考慮調整 HTML 結構而非單靠 CSS。
  • 表頭 `` 必須保持,置中樣式不影響 `scope` 效力。

總結

選擇置中方案決策樹:

  1. 內容純文字/單行/圖片、需 IE8+ 相容 → **`vertical-align: middle` + `text-align: center`**(零成本)。
  2. 內容含多行文字、圖文混排、需響應式彈性 → **Flexbox 包裝層**(功能最全)。
  3. 現代專案、追求程式碼極簡、無 IE 需求 → **Grid `place-items: center`**(最優雅)。

建立專案通用 `.cell-flex`、`.cell-grid` 工具類,配合 `` 基礎 `vertical-align: middle`,即可靈活應對所有表格置中需求。

copyright © 萬盛學電腦網 all rights reserved