早期直接在 <table>、<tr>、<td>、<th> 標籤上設定:
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr align="center" valign="middle">
<td align="left" valign="top">內容</td>
</tr>
</table>
| 屬性 | 作用對象 | 說明 | 現代 CSS 替代 |
|---|---|---|---|
align | table, tr, td, th | 水平對齊:left, center, right | table { margin: 0 auto; } (表格置中), td { text-align: center; } (儲存格文字) |
valign | tr, td, th | 垂直對齊:top, middle, bottom, baseline | td { vertical-align: middle; } |
width | table, td, th | 寬度 (像素/百分比) | width, max-width, min-width |
height | table, td, th | 高度 | height, min-height |
border | table | 邊框粗細 (像素) | border: 1px solid #ccc; + border-collapse: collapse; |
cellspacing | table | 儲存格間距 | border-spacing: 0; (需 border-collapse: separate;) |
cellpadding | table | 儲存格內邊距 (內容到邊框) | td { padding: 8px; } |
bgcolor | table, tr, td, th | 背景顏色 | background-color: #f5f5f5; |
table {
border-collapse: collapse; /* 合併邊框,消除雙線、cellspacing 效果 */
width: 100%; /* 響應式佔滿容器 */
max-width: 100%; /* 防止撐破容器 */
table-layout: fixed; /* 固定佈局:依 col 定寬,不被內容撐開,渲染更快 */
}
th, td {
border: 1px solid #e0e0e0;
padding: 12px 16px;
text-align: left; /* 文字預設靠左 */
vertical-align: middle; /* 垂直置中 */
white-space: nowrap; /* 預設不換行,配合 overflow 處理長文 */
overflow: hidden;
text-overflow: ellipsis; /* 超出顯示省略號 */
}
<table>
<colgroup>
<col style="width: 50px;"> <!-- 固定寬度:序號 -->
<col style="width: 30%;"> <!-- 百分比:名稱 -->
<col style="width: 120px;"> <!-- 固定:價格 -->
<col> <!-- 剩餘空間:描述 -->
</colgroup>
<thead>...</thead>
<tbody>...</tbody>
</table>
使用 <colgroup><col> 定義欄寬,語意清晰、樣式集中、瀏覽器渲染最快。
.align-center { text-align: center; }
.align-right { text-align: right; }
.align-top { vertical-align: top; }
.align-bottom { vertical-align: bottom; }
.nowrap { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wrap { white-space: normal; word-break: break-word; }
HTML 中套用:<td class="align-right nowrap">$1,234,567</td>
tbody tr:nth-child(odd) { background-color: #fafafa; } /* 斑馬紋 */
tbody tr:hover { background-color: #eef7ff; } /* 滑鼠懸停高亮 */
tbody tr.selected { background-color: #d0e9ff; } /* 選取狀態 */
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>現代表格對齊範例</title>
<style>
.data-table {
border-collapse: collapse;
width: 100%;
max-width: 900px;
margin: 2rem auto;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
font-size: 0.9rem;
table-layout: fixed;
box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.data-table col:first-child { width: 40px; }
.data-table col:nth-child(2) { width: 30%; }
.data-table col:nth-child(3) { width: 120px; }
.data-table th,
.data-table td {
border: 1px solid #e8e8e8;
padding: 12px 16px;
vertical-align: middle;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.data-table thead th {
background: #f5f7fa;
font-weight: 600;
text-align: left;
color: #333;
position: sticky;
top: 0;
z-index: 1;
}
.data-table tbody tr:nth-child(odd) td { background: #fcfcfc; }
.data-table tbody tr:hover td { background: #eef6ff; }
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table .center { text-align: center; }
.data-table .wrap { white-space: normal; word-break: break-word; }
</style>
</head>
<body>
<table class="data-table">
<colgroup>
<col><col><col><col><col>
</colgroup>
<thead>
<tr>
<th>#</th>
<th>商品名稱</th>
<th class="num">單價</th>
<th class="center">庫存</th>
<th class="wrap">備註</th>
</tr>
</thead>
<tbody>
<tr><td>1</td><td>高效能筆電</td><td class="num">$45,900</td><td class="center">12</td><td class="wrap">新款上市,歡迎洽詢</td></tr>
<tr><td>2</td><td>人體工學滑鼠</td><td class="num">$1,290</td><td class="center">56</td><td class="wrap">支援無線/藍牙雙模</td></tr>
</tbody>
</table>
</body>
</html>
vertical-align 進階說明baseline (預設,基線對齊)、top、middle、bottom、text-top、text-bottom、sub、super、<length>、<percentage>。middle 讓文字圖片垂直置中。<img> 在文字中) 常用 vertical-align: middle 修正基線對齊導致的底部間隙。<div style="overflow-x:auto"><table>...</table></div> (最簡單、資料完整)。position: sticky; left: 0; 固定第一欄 (需 thead th 同樣設定)。<dl> 卡片式顯示。@media (max-width: 600px) { td:nth-child(n+4) { display: none; } } 隱藏次要欄。拋棄舊屬性,擁抱 border-collapse, table-layout: fixed, <colgroup>, padding, text-align, vertical-align 等 CSS 標準屬性。搭配響應式容器與現代偽類選擇器,即能打造美觀、高效能、無障礙、易維護的專業級資料表格。