根據設計需求,有時需要完全移除表格邊框、僅保留外框、或只保留橫向分隔線。本文提供三種層級的解法,從最簡單的 HTML 屬性到靈活的 CSS 選擇器控制。
<table border="0">
<tr><td>無邊框資料</td><td>資料</td></tr>
</table>
border="0":瀏覽器預設不繪製邊框<style>
.no-border-table { border-collapse: collapse; }
.no-border-table td, .no-border-table th { border: none; }
</style>
<table class="no-border-table">...</table>
border-collapse: collapse 合併邊框模型,避免雙重線td/th 邊框background-color 做區塊區隔.outer-border-only { border-collapse: collapse; }
.outer-border-only { border: 2px solid #333; }
.outer-border-only td, .outer-border-only th { border: none; padding: 12px; }
.horizontal-only { border-collapse: collapse; }
.horizontal-only th, .horizontal-only td { border: none; border-bottom: 1px solid #eee; padding: 10px 12px; }
.horizontal-only thead th { border-bottom: 2px solid #333; font-weight: 600; }
.horizontal-only tbody tr:last-child td { border-bottom: none; }
.vertical-only { border-collapse: collapse; }
.vertical-only td, .vertical-only th { border: none; border-right: 1px solid #eee; padding: 10px 12px; }
.vertical-only tr td:last-child, .vertical-only tr th:last-child { border-right: none; }
.zebra { border-collapse: collapse; }
.zebra td, .zebra th { border: none; padding: 12px; }
.zebra tbody tr:nth-child(odd) { background: #f9f9f9; }
.zebra tbody tr:hover { background: #eef; }
border="0"@media (max-width: 600px) {
.responsive-table {
display: block;
overflow-x: auto;
border: none; /* 移除外框,靠容器滾動條 */
}
.responsive-table td, .responsive-table th {
border-right: none; /* 直線在橫向滾動無意義 */
border-bottom: 1px solid #eee;
}
}
| 需求 | 建議做法 | CSS 關鍵字 |
|---|---|---|
| 完全無邊框 | CSS 類別 + border:none | border-collapse:collapse; border:none |
| 僅外框 | table 加 border,td/th 無 border | table{border:2px solid} td{border:none} |
| 僅橫線 | td/th 只保留 border-bottom | border-bottom:1px solid #eee |
| 僅直線 | td/th 只保留 border-right | border-right:1px solid #eee |
| 斑馬紋無線 | nth-child 交替背景 | tr:nth-child(odd){background:#f9f9f9} |
| 行動版隱藏直線 | 媒體查詢移除 border-right | @media(max-width:600px){td{border-right:none}} |
根據設計稿需求選擇適當層級:原型開發用 border="0" 最快;正式專案請用 CSS 類別系統化管理,支援主題切換、深色模式、響應式斷點與設計系統整合。