早期「另類圓角表格」用大量 `
<style>
.table-rounded {
border-collapse: separate; /* 必須 separate 才能 border-radius */
border-spacing: 0; /* 消除間隙 */
border: 2px solid #43b5c9; /* 外邊框 */
border-radius: 12px; /* 整體圓角 */
overflow: hidden; * 裁剪內部溢出 */
width: 100%; max-width: 600px;
}
.table-rounded th,
.table-rounded td {
border: none; /* 內部無邊框 */
border-bottom: 1px solid #e0e0e0; /* 僅橫向分隔線 */
border-right: 1px solid #e0e0e0; /* 僅縱向分隔線 */
padding: 12px 16px;
}
.table-rounded th { background: #43b5c9; color: #fff; font-weight: 600; }
.table-rounded tr:last-child td { border-bottom: none; }
.table-rounded tr td:last-child { border-right: none; }
/* 四角單元格圓角微調 (可選) */
.table-rounded tr:first-child th:first-child { border-top-left-radius: 10px; }
.table-rounded tr:first-child th:last-child { border-top-right-radius: 10px; }
.table-rounded tr:last-child td:first-child { border-bottom-left-radius: 10px; }
.table-rounded tr:last-child td:last-child { border-bottom-right-radius: 10px; }
</style>
<table class="table-rounded">
<thead><tr><th>姓名</th><th>部門</th><th>薪資</th></tr></thead>
<tbody>
<tr><td>張三</td><td>技術部</td><td>¥25,000</td></tr>
<tr><td>李四</td><td>產品部</td><td>¥22,000</td></tr>
</tbody>
</table>
.table-striped tbody tr:nth-child(odd) { background: #f9f9f9; }
.table-striped tbody tr:nth-child(even) { background: #fff; }
.table-striped tbody tr:hover { background: #e3f2fd !important; } /* !important 覆蓋斑馬紋 */
.table-striped td.num { text-align: right; font-variant-numeric: tabular-nums; } /* 數字右對齊等寬 */
.table-striped td.center { text-align: center; }
.table-sticky {
overflow-y: auto; max-height: 400px; /* 容器可滾動 */
}
.table-sticky table { border-collapse: collapse; width: 100%; }
.table-sticky th {
position: sticky; top: 0; /* 核心:黏性定位 */
background: #fff; /* 必須有背景色避免穿透 */
z-index: 1;
box-shadow: 0 2px 2px -1px rgba(0,0,0,.1); /* 視覺分層 */
}
**容器必須有高度限制且 `overflow: auto/scroll`**,`th` 才會在滾動時固定。
@media (max-width: 640px) {
.table-responsive-card thead { display: none; } /* 隱藏表頭 */
.table-responsive-card tbody tr {
display: block; border: 1px solid #ddd; border-radius: 8px; margin-bottom: 12px; padding: 12px; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.table-responsive-card td {
display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid #f0f0f0;
}
.table-responsive-card td:last-child { border-bottom: none; }
.table-responsive-card td::before {
content: attr(data-label); /* 用 data-label 顯示原表頭 */
font-weight: 600; color: #666; flex-shrink: 0; margin-right: 16px;
}
.table-responsive-card td:only-child { border-bottom: none; padding-top: 0; }
}
HTML 需添加 `data-label`:`
@media print {
.table-sticky { overflow: visible; max-height: none; }
.table-sticky th { position: static; box-shadow: none; }
.table-responsive-card tbody tr { break-inside: avoid; box-shadow: none; border: 1px solid #000; }
}
src/
├── components/
│ ├── DataTable.vue/.tsx # 封裝 TanStack Table + 上述所有 CSS 效果
│ ├── TableSkeleton.vue # 骨架屏
│ └── TableToolbar.vue # 匯出/列選擇/全局搜索
├── styles/
│ ├── _table-core.scss # collapse/separate 基礎、變量
│ ├── _table-rounded.scss # 圓角
│ ├── _table-striped.scss # 斑馬紋/hover
│ ├── _table-sticky.scss # 固定表頭
│ ├── _table-responsive-card.scss # 卡片模式
│ └── _table-print.scss # 打印樣式
└── hooks/
└── useTable.ts # 邏輯復用
表格特效不再是「黑魔法」,而是 **CSS 現代特性 (sticky、grid/flex、var、media) + 語義化 HTML + 無障礙屬性** 的工程化組合。封裝為組件庫,一次投入,全項目受益。