萬盛學電腦網

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

CSS 細線表格製作術:Border-collapse 與邊框藝術的完美結合

細線表格核心原理:Border Collapse 模型

border-collapse: collapse 讓相鄰單元格共享邊框,而非雙重邊框疊加。配合精確的邊框寬度分配,實現視覺上 1px 細線網格。

經典細線表格 CSS 完整實現

/* 表格容器 */
.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; }

斑馬紋 + 懸停高亮 (純 CSS)

.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; /* 懸停時邊框變色增強視覺 */
}

固定表頭滾動 (CSS Sticky)

.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; /* 強調分隔 */
}

語義化 HTML 結構 (無障礙基石)

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

現代 CSS 表格增強技巧

邏輯屬性適配 RTL

.table-thin td,
.table-thin th {
  border-inline-end: 1px solid #000;  /* 邏輯右邊框 */
  border-block-end: 1px solid #000;   /* 邏輯下邊框 */
  /* 替代 border-width: 0 1px 1px 0; */
}

容器查詢響應式 (CSS Container Queries)

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

列寬度自適應 (CSS Grid 表格)

.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> 語義分組
  • ✅ 大數據表格:虛擬滾動 (react-window/vue-virtual-scroller)
  • ✅ 列寬度:避免固定 px,用 minmax()/%/fr
  • ✅ 列對齊:數字右對齊、文字左對齊、居中僅用於狀態/操作列

總結

細線表格的核心在於 border-collapse: collapse + 非對稱邊框寬度分配 (容器上左、單元格右下)。現代開發中,語義化 HTML + CSS 變數系統 + 容器查詢響應式,構建出既美觀又無障礙的數據表格組件。

copyright © 萬盛學電腦網 all rights reserved