在網頁設計中,為表格添加背景色彩能有效提升資料的可讀性與視覺層次。HTML 提供了多種層級的背景色設置方式,分別針對整個表格、單一列、甚至單一儲存格。本文將詳細說明各種設置方法與語法。
使用 <table> 標籤的 bgcolor 屬性,可一次套用至整張表格:
<table bgcolor="#f0f0f0">
<tr><td>儲存格 1</td><td>儲存格 2</td></tr>
</table>
在 <tr> 標籤上加上 bgcolor,即可讓該列呈現特定顏色:
<table>
<tr bgcolor="#e0e0e0">
<td>列背景色範例</td><td>資料</td>
</tr>
</table>
分別在 <td>(一般儲存格)或 <th>(標題儲存格)加上 bgcolor:
<table>
<tr>
<th bgcolor="#d0d0d0">標題欄</th>
<td bgcolor="#ffffcc">內容欄</td>
</tr>
</table>
bgcolor 屬性在 HTML5 中已被列為過時屬性,現代開發建議改用 CSS background-color 樣式,以利維護與關注點分離。#RRGGBB)、RGB 函數或顏色關鍵字。<style>
table { background-color: #f0f0f0; }
tr.highlight { background-color: #e0e0e0; }
td.special { background-color: #ffffcc; }
</style>
<table>
<tr class="highlight"><td>...</td></tr>
<tr><td class="special">...</td></tr>
</table>
掌握這些技巧,即可靈活控制表格呈現效果,打造更專業的網頁介面。