讓表格內容在儲存格中垂直置中、水平置中,是常見排版需求。現代 CSS 提供 Flexbox、Grid 等強大佈局模組,但 `
| 方案 | 核心 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 不支援、同樣需包裝層 | 現代專案、簡潔程式碼偏好 |
<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。
<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>
<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` 效力。
總結
選擇置中方案決策樹:
- 內容純文字/單行/圖片、需 IE8+ 相容 → **`vertical-align: middle` + `text-align: center`**(零成本)。
- 內容含多行文字、圖文混排、需響應式彈性 → **Flexbox 包裝層**(功能最全)。
- 現代專案、追求程式碼極簡、無 IE 需求 → **Grid `place-items: center`**(最優雅)。
建立專案通用 `.cell-flex`、`.cell-grid` 工具類,配合 `
` 基礎 `vertical-align: middle`,即可靈活應對所有表格置中需求。
HTML5 教學排行
推薦文章
- HTML Frameset 導航頁面實作教學:target 屬性與框架通訊完整指…
- HTML5 基礎教學(三):深度解析 Geolocation 與 Web Wor…
- 正則表達式精準刪除指定 HTML 標籤:保留白名單、避免部分匹配陷阱的 C# 實…
- HTML 下拉選單 `<select>` 完整指南:原生功能、樣式限制、無障礙與…
- HTML 表格垂直置中對齊:傳統嵌套法與現代 Flexbox 方案
- HTML input 標籤詳解:類型屬性、表單收集與 HTML/XHTML 差異
- HTML 常用標籤完整速查表:語義化結構、文本格式化、表單、媒體、表格與 HTM…
- HTML 顯示原始碼標籤:`<pre>`、`<code>`、實體編碼與安全輸出完…
- HTML 邏輯標籤與物理標籤差異解析:語意化標記最佳實踐
- HTML5 視頻教程精選推薦:從入門到實戰的優質學習資源清單
copyright © 萬盛學電腦網 all rights reserved