border-collapse: collapse 讓相鄰單元格共享邊框,而非雙重邊框疊加。配合精確的邊框寬度分配,實現視覺上 1px 細線網格。
/* 表格容器 */
.table-thin {
border: 1px solid #000; /* 外框 */
border-width: 1px 0 0 1px; /* 關鍵:僅顯示上/左邊框 */
border-collapse: collapse; /* 核心:合併邊框模型 */
margin: 2px 0;
text-align: center;
width: 100%;
font-size: 12px;
}
/* 單元格:補全右/下邊框 */
.table-thin td,
.table-thin th {
border: 1px solid #000;
border-width: 0 1px 1px 0; /* 關鍵:僅顯示右/下邊框 */
margin: 2px 0;
padding: 4px 8px;
text-align: left;
}
/* 表頭強化 */
.table-thin th {
text-align: center;
font-weight: 600;
background-color: #f4f4f4;
white-space: nowrap; /* 防止表頭換行 */
}
/* 響應式處理 */
@media (max-width: 600px) {
.table-thin { display: block; overflow-x: auto; white-space: nowrap; }
}
/* 視覺效果對照 */
┌─────────┬─────────┐ ← 表格上邊框 (table border-top)
│ 表頭 A │ 表頭 B │ ← th 右邊框 + 下邊框
├─────────┼─────────┤ ← td 右邊框 + 下邊框 (共享線)
│ 數據 1 │ 數據 2 │
└─────────┴─────────┘ ← 表格無下邊框 (由最後一行 td border-bottom 構成)
/* 邊框寬度分配邏輯 */
Table: border-width: 1px 0 0 1px; /* 上、左 */
Th/Td: border-width: 0 1px 1px 0; /* 右、下 */
/* 相鄰單元格共享邊框:TD 右框 = 下一個 TD 左框 (但左框為 0) → 視覺單線 */
.table-duotone {
border: 1px solid #333; /* 深色外框 */
border-width: 1px 0 0 1px;
border-collapse: collapse;
}
.table-duotone td,
.table-duotone th {
border: 1px solid #e0e0e0; /* 淺色內框 */
border-width: 0 1px 1px 0;
}
.table-duotone th { background: #fafafa; }
.table-striped tbody tr:nth-child(odd) { background: #f9f9f9; }
.table-striped tbody tr:hover { background: #e8f4fd; }
/* 結合細線 */
.table-striped.table-thin tbody tr:hover td {
background: #e8f4fd;
border-color: #b3d7f2; /* 懸停時邊框變色增強視覺 */
}
.table-scroll {
max-height: 400px;
overflow-y: auto;
border: 1px solid #ddd;
}
.table-scroll thead th {
position: sticky;
top: 0;
background: #f4f4f4;
z-index: 1; /* 確保表頭在內容之上 */
border-bottom: 2px solid #ccc; /* 強調分隔 */
}
<table class="table-thin">
<caption>2024 年 Q1 季度銷售數據</caption>
<thead>
<tr>
<th scope="col">產品</th>
<th scope="col">銷量</th>
<th scope="col">金額</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">產品 A</th>
<td>1,234</td>
<td>¥12.3 萬</td>
</tr>
<tr>
<th scope="row">產品 B</th>
<td>567</td>
<td>¥5.6 萬</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">總計</th>
<td>1,801</td>
<td>¥17.9 萬</td>
</tr>
</tfoot>
</table>
.table-thin td,
.table-thin th {
border-inline-end: 1px solid #000; /* 邏輯右邊框 */
border-block-end: 1px solid #000; /* 邏輯下邊框 */
/* 替代 border-width: 0 1px 1px 0; */
}
.table-wrapper { container-type: inline-size; }
@container (max-width: 500px) {
.table-thin { display: block; overflow-x: auto; }
.table-thin thead { display: none; } /* 隱藏表頭,改用卡片視圖 */
.table-thin tbody tr { display: block; border-bottom: 2px solid #eee; padding: 1rem; }
.table-thin td { display: flex; justify-content: space-between; border: 0; padding: 0.25rem 0; }
.table-thin td::before { content: attr(data-label); font-weight: bold; color: #666; }
}
.grid-table {
display: grid;
grid-template-columns: minmax(120px, 2fr) 1fr 1fr; /* 最小 120px,比例分配 */
border-collapse: collapse; /* Grid 不支援,改用邊框模擬 */
}
.grid-table > * {
border: 1px solid #ddd;
border-width: 0 1px 1px 0;
padding: 0.5rem;
}
.grid-table > *:first-child { border-left-width: 1px; }
.grid-table > *:nth-child(3n+1) { border-left-width: 1px; } /* 每行第一個 */
<caption> 提供表格標題<th scope="col|row"> 明確標題範圍<thead>/<tbody>/<tfoot> 語義分組minmax()/%/fr細線表格的核心在於 border-collapse: collapse + 非對稱邊框寬度分配 (容器上左、單元格右下)。現代開發中,語義化 HTML + CSS 變數系統 + 容器查詢響應式,構建出既美觀又無障礙的數據表格組件。