萬盛學電腦網

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

HTML 表格立體邊框效果實作:從屬性到 CSS 3D 轉型指南

舊時代產物:屬性式 3D 邊框

早期 HTML 透過 bordercolorlight 與 bordercolordark 模擬立體感,配合 border、cellspacing 等屬性:

<table border="1" cellspacing="0" width="100%" 
       bordercolorlight="#333333" bordercolordark="#efefef">
  <tr bgcolor="#cccccc">
    <td>cnbruce</td><td>cnbruce</td><td>cnbruce</td><td>cnbruce</td>
  </tr>
  <tr bgcolor="#cccccc">
    <td>cnbruce</td><td>cnbruce</td><td>cnbruce</td><td>cnbruce</td>
  </tr>
</table>

缺點明顯: 僅支援單色邊框、無法控制陰影漸層、不支援圓角、語意與表現耦合、RWD 極難處理,**已被 HTML5 棄用**,僅供維護舊專案參考。

現代 CSS 立體邊框方案

方案一:border-style: outset/inset(最簡單,效果有限)

.table-3d-simple {
  border-collapse: separate;
  border-spacing: 0;
  border: 4px outset #ccc; /* 整體外凸 */
}
.table-3d-simple td {
  border: 2px inset #fff; /* 儲存格內凹 */
  background: #f9f9f9;
}

瀏覽器自動根據邊框色計算高光/陰影,但色彩控制力弱,且 border-collapse: separate 會產生雙線縫隙。

方案二:漸層邊框 + 陰影(推薦,完全可控)

利用 linear-gradient 模擬光源方向,配合 box-shadow 強化立體感:

.table-3d-modern {
  border-collapse: collapse;
  width: 100%;
  max-width: 800px;
  margin: 2rem auto;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  box-shadow: 
    0 4px 12px rgba(0,0,0,0.08),
    0 0 0 1px rgba(0,0,0,0.05); /* 外層微邊框 */
  border-radius: 8px; /* 整體圓角 */
  overflow: hidden;   /* 裁切內部圓角 */
}

.table-3d-modern th,
.table-3d-modern td {
  padding: 14px 20px;
  text-align: left;
  border-bottom: 1px solid #e8eef4;
}

/* 表頭:深色背景 + 頂部高光邊框 */
.table-3d-modern thead th {
  background: linear-gradient(180deg, #2c3e50 0%, #1a252f 100%);
  color: #fff;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 2px solid #3a506b; /* 分隔線 */
  /* 頂部高光模擬 */
  background-clip: padding-box;
  position: relative;
}
.table-3d-modern thead th::before {
  content: "";
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.15), transparent);
}

/* 儲存格:細微內凹感 */
.table-3d-modern tbody td {
  background: #fff;
  background-image: linear-gradient(180deg, #fdfdfd 0%, #f5f7fa 100%);
  transition: background 0.2s ease;
}
.table-3d-modern tbody tr:last-child td {
  border-bottom: none;
}
.table-3d-modern tbody tr:hover td {
  background: #eef5ff;
  background-image: linear-gradient(180deg, #eef5ff 0%, #dfeaff 100%);
}

/* 列首欄加強 */
.table-3d-modern tbody th {
  background: #f8f9fa;
  font-weight: 500;
  color: #333;
  border-right: 1px solid #e8eef4;
}

方案三:Neumorphism (新拟态) 風格表格

近年流行的柔和立體風格,適合深色模式:

.table-neumorphism {
  border-collapse: separate;
  border-spacing: 8px; /* 必須分離邊框才能做陰影 */
  background: #e0e5ec;
  border-radius: 16px;
  padding: 16px; /* 容器內距 */
  box-shadow: 
    8px 8px 16px #bec5cd,
    -8px -8px 16px #ffffff;
}
.table-neumorphism th,
.table-neumorphism td {
  padding: 12px 18px;
  border-radius: 8px;
  background: #e0e5ec;
  box-shadow: inset 4px 4px 8px #bec5cd, inset -4px -4px 8px #ffffff;
  border: none;
}
.table-neumorphism thead th {
  box-shadow: inset -4px -4px 8px #bec5cd, inset 4px 4px 8px #ffffff; /* 反向凸起 */
  color: #555;
  font-weight: 600;
}

注意: border-collapse: separate 與 border-spacing 會破壞表格對齊精度,複雜表格慎用。

最佳實踐總結

場景推薦方案關鍵技術
一般資料表格方案二(漸層+陰影)linear-gradient, box-shadow, border-collapse: collapse
儀表板/卡片式表格方案三border-collapse: separate, inset box-shadow
舊專案維護方案一或保留屬性bordercolorlight/dark(不建議新專案使用)

無障礙與效能提醒

  • 立體效果**不可**犧牲對比度,確保文字對背景對比度 ≥ 4.5:1 (WCAG AA)。
  • 避免在大量儲存格(>500)套用複雜漸層/陰影,會影響渲染幀率;必要時簡化為純色邊框。
  • border-radius 於 border-collapse: collapse 表格僅對最外層生效,內部圓角需改用 separate 或偽元素模擬。

現代 CSS 賦予我們對光影的精細掌控,拋棄僵化的屬性式立體邊框,擁抱漸層、陰影與動畫,打造既美觀又符合工程規範的資料呈現體驗。

copyright © 萬盛學電腦網 all rights reserved