萬盛學電腦網

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

HTML 表格邊框樣式現代化:border-collapse、邊框合併、圓角、陰影與響應式設計

核心屬性:border-collapse

值行為適用場景
collapse (推薦)相鄰儲存格共用單一邊框,border-spacing 無效多數資料表格、Excel 風格
separate (預設)儲存格各有獨立邊框,可設 border-spacing卡片式、需間距設計

現代基礎樣式模板

<style>
.table {
  width: 100%;
  border-collapse: collapse;        /* 關鍵:合併邊框 */
  font-family: system-ui, sans-serif;
  font-size: 0.9rem;
  line-height: 1.5;
}
.table th, .table td {
  border: 1px solid #dee2e6;        /* 統一邊框 */
  padding: 0.75rem 1rem;
}
.table thead th {
  background: #f8f9fa;
  font-weight: 600;
  text-align: left;
  position: sticky; top: 0; z-index: 1; /* 滾動固定表頭 */
}
.table tbody tr:nth-child(even) { background: #fdfdfd; }
.table tbody tr:hover { background: #eef5ff; }
</style>

進階視覺效果

<!-- 1. 圓角表格(需 separate + 隱藏外層邊框) -->
.table-rounded {
  border-collapse: separate;
  border-spacing: 0;
  border-radius: 12px;
  overflow: hidden; /* 裁切內部邊框圓角 */
}
.table-rounded th:first-child { border-top-left-radius: 12px; }
.table-rounded th:last-child  { border-top-right-radius: 12px; }
.table-rounded tr:last-child td:first-child { border-bottom-left-radius: 12px; }
.table-rounded tr:last-child td:last-child  { border-bottom-right-radius: 12px; }

/* 2. 陰影提升 */
.table-elevated { box-shadow: 0 4px 12px rgba(0,0,0,0.08); }

/* 3. 無邊框極簡風 */
.table-minimal th, .table-minimal td { border: none; border-bottom: 1px solid #eee; }
.table-minimal tr:last-child td { border-bottom: none; }

響應式表格容器

<div class="table-responsive">
  <table class="table">...</table>
</div>

<style>
.table-responsive { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-responsive > .table { min-width: 600px; } /* 強制觸發滾動 */
@media (max-width: 576px) {
  .table-responsive > .table { min-width: 0; width: 100%; }
}
</style>

無障礙邊框對比度

  • 邊框顏色對比度需 ≥ 3:1 (WCAG 1.4.11 Non-text Contrast)。
  • #dee2e6 (Bootstrap 預設) 對白底約 1.6:1 ❌,建議改 #adb5bd (3.1:1) 或 #868e96 (4.5:1)。
copyright © 萬盛學電腦網 all rights reserved