萬盛學電腦網

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

HTML 表格細線邊框實作:border-collapse:collapse + 單側邊框 + 圓角技巧

打造 1px 單線、可控顏色、支援圓角的專業級表格邊框

核心 CSS:合併邊框模式

.hairline-table {
  border-collapse: collapse;  /* 合併相鄰邊框為單線 */
  width: 100%;
}
.hairline-table th,
.hairline-table td {
  border: 1px solid #ddd;     /* 統一單像素實線 */
  padding: 10px 12px;
}

進階:層級化邊框顏色

.hairline-table {
  border-collapse: collapse;
  border: 1px solid #006600;       /* 外框深綠 */
}
.hairline-table thead th {
  border-color: #009900;           /* 表頭中綠 */
  background: #e8f5e9;
}
.hairline-table tbody td {
  border-color: #ddd;              /* 內格淺灰 */
}
.hairline-table tbody tr:last-child td {
  border-bottom-color: #006600;    /* 底部外框呼應頂部 */
}

圓角表格:separate 模式 + overflow 隱藏

.rounded-table {
  border-collapse: separate;   /* 必須 separate 才能 border-radius */
  border-spacing: 0;           /* 消除間隙 */
  border: 1px solid #ccc;
  border-radius: 8px;
  overflow: hidden;            /* 裁切內部直角 */
  width: 100%;
}
.rounded-table th:first-child { border-top-left-radius: 6px; }
.rounded-table th:last-child  { border-top-right-radius: 6px; }
.rounded-table tbody tr:last-child td:first-child { border-bottom-left-radius: 6px; }
.rounded-table tbody tr:last-child td:last-child  { border-bottom-right-radius: 6px; }

單側邊框設計 (極簡風)

.minimal-table {
  border-collapse: collapse;
}
.minimal-table th,
.minimal-table td {
  border: none;
  border-bottom: 1px solid #eee;  /* 僅底部分隔線 */
  padding: 12px 16px;
}
.minimal-table thead th {
  border-bottom-width: 2px;
  border-bottom-color: #333;
  font-weight: 600;
  text-align: left;
}
.minimal-table tbody tr:last-child td {
  border-bottom: none;            /* 最後一列無分隔線 */
}
.minimal-table tbody tr:hover td {
  background: #f5f5f5;            /* 懸停列高亮 */
}

避坑檢查清單

  • ❌ <tr style="border:..."> 無效,邊框只能套用於 table、th、td (或 col 配合 border-collapse:collapse)。
  • ❌ border-collapse: collapse 時 border-radius 於 table 無效。
  • ✅ 使用 box-sizing: border-box 確保 padding 不撐大儲存格。
  • ✅ 大型表格加 table-layout: fixed 提升渲染效能。

靈活運用 border-collapse 模式、層級化顏色與圓角技巧,能實現從資料密集型後台表格到極簡展示型表格的多樣視覺需求。

copyright © 萬盛學電腦網 all rights reserved