想幫表格或整個頁面加上漸層底色,在 CSS 裡最直接的方式是調用 IE 的 gradient 滤镜。它的語法本身不難,只要抓對兩個色碼與一個方向參數就好。
filter: progid:DXImageTransform.Microsoft.gradient(startcolorstr=起始色, endcolorstr=結束色, gradientType=方向)
startcolorstr 與 endcolorstr 是漸層的起訖顏色,寫成 #AARRGGBB 的形式,前面兩位是透明度。
gradientType 決定方向:0 表示由上而下(垂直),1 表示由左而右(水平)。
<style>
td {
text-align:center;
color:white;
font-family:宋體;
font-size:14px;
}
table {
table-layout:fixed;
border:1px dashed blue;
width:567px;
height:234px;
filter:progid:DXImageTransform.Microsoft.gradient(startcolorstr=#FFFF0000, endcolorstr=#FFFFFF00, gradientType=0);
}
</style>
這段讓整個表格擁有由上到下的紅→黃漸層。若只想讓某幾格有漸層、其他格維持純色,就把 filter 從 table 規則移到 td 的 inline style 上,逐格指定:
<table border="0" cellspacing="20" cellpadding="2">
<tr>
<td style="filter:progid:DXImageTransform.Microsoft.gradient(startcolorstr=#FFFF0000, endcolorstr=#FFFFFF00, gradientType=1)">欄位標題</td>
<td style="filter:progid:DXImageTransform.Microsoft.gradient(startcolorstr=#FF00119D, endcolorstr=#FF09FFFF, gradientType=1)">內容說明文字</td>
</tr>
<tr>
<td style="filter:progid:DXImageTransform.Microsoft.gradient(startcolorstr=#FFFFEE00, endcolorstr=#FF09FF00, gradientType=1)">第二列標題</td>
<td style="filter:progid:DXImageTransform.Microsoft.gradient(startcolorstr=deeppink, endcolorstr=#FF09FF00, gradientType=1)">第二列內容</td>
</tr>
</table>
startcolorstr 也接受顏色名稱,像上面的 deeppink 就是合法寫法,不必非得寫成十六碼。
這個 filter 屬於 IE 私有屬性,Firefox、Chrome、Safari 等瀏覽器完全不認,因此它只能當作「加分效果」,不能當作唯一依賴。
表格文字建議保持白色或淺色,因為漸層底色多半偏亮或偏飽和,深色字容易看不清。
cellspacing 給得太大時,間隙會露出頁面底色,這有時反而是想要的效果,可依設計調整。
若需要跨瀏覽器的漸層,較穩妥的做法是改用 modern 的 background: linear-gradient(...),並保留 filter 作為舊版 IE 的 fallback,兩者並存即可。
table-layout:fixed 值得一併加上,它讓欄寬依第一列決定,大量資料時不必逐格算寬度,版面會穩定得多。