萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML 表格特效:純 CSS 實現圓角表格、斑馬紋、懸停高亮、固定表頭、響應式卡片模式完整代碼庫

為什麼不用圖片/舊式巢狀表格做圓角?

早期「另類圓角表格」用大量 `` 1px 拼湊圓弧,**代碼膨脹、語義破壞、無法響應、維護地獄**。現代 CSS `border-radius` + `border-collapse: separate` 幾行代碼完美解決。

效果一:標準圓角表格 (Collapse 不支援圓角,必須 Separate)

<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; }

效果三:固定表頭 (Sticky Header) — 長表格必備

.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`:`¥25,000`。

效果五:可排序/可篩選/分頁 — 接入輕量庫

  • SortableJS:拖拽排序行。
  • List.js / **Grid.js** / **TanStack Table** (Headless, React/Vue/Solid/Svelte):**推薦 TanStack Table v8**,功能最全 (排序、篩選、分頁、分組、虛擬化、列顯隱、編輯)、無 UI 鎖定、TypeScript 一級支援。

打印優化 (續上文)

@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 + 無障礙屬性** 的工程化組合。封裝為組件庫,一次投入,全項目受益。

copyright © 萬盛學電腦網 all rights reserved