萬盛學電腦網

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

CSS 單一 Class 打造精美細線表格,零 HTML 侵入

核心優勢

只需在 <table> 加上 class="table",無需對 <tr>、<td> 逐個加 Class,極大降低 HTML 耦合與維護成本。

引入方式

<link rel="stylesheet" href="./css/table.css" />
<table class="table" style="width: 600px">...</table>

table.css 完整規則

/* 表格基礎 */
.table { width: 100%; padding: 0; margin: 0; font-family: Arial, Tahoma, Verdana, Sans-Serif, "Microsoft YaHei"; border-left: 1px solid #add8e6; border-collapse: collapse; }

/* 表頭 */
.table th { font-size: 12px; font-weight: 600; color: #303030; border-right: 1px solid #add8e6; border-bottom: 1px solid #add8e6; border-top: 1px solid #add8e6; letter-spacing: 2px; text-align: center; padding: 10px 0; background: url(../images/tablehdbg.png); white-space: nowrap; overflow: hidden; }

/* 單元格 */
.table td { border-right: 1px solid #add8e6; border-bottom: 1px solid #add8e6; background: #fff; font-size: 12px; padding: 3px 3px 3px 6px; color: #303030; word-break: break-all; word-wrap: break-word; white-space: normal; }

/* 隔行變色(手動加 class="color") */
.table td.color { background: #edf7f9; }

/* 連結樣式 */
.table td a:link, .table td a:visited { font-weight: 400; color: #2259d7; text-decoration: none; }
.table td a:hover { text-decoration: underline; color: #303030; }
.table td a:active { color: #2259d7; }

客製化指南

  • 修改邊框色:全域搜尋 #add8e6 替換為目標色值。
  • 調整字體/間距:只需修改 .table、.table th、.table td 三組規則。
  • 自動斑馬紋:現代專案建議改用 .table tr:nth-child(even) td { background: #edf7f9; } 純 CSS 實現,無需手動加 Class。
copyright © 萬盛學電腦網 all rights reserved