半透明表格能讓背景圖片或色彩若隱若現,提升視覺層次。本文介紹從傳統濾鏡到現代 RGBA 的完整演進。
table {
background-color: rgba(51, 102, 153, 0.5); /* 藍色 50% 透明 */
}
/* 或單獨設定儲存格 */
td {
background-color: rgba(255, 255, 255, 0.8); /* 白色 80% 不透明 */
}
優點:語法簡潔、不影響子元素透明度、所有現代瀏覽器支援。
<td style="background-color: #336699;
filter: Alpha(opacity=50);">
表格內容
</td>
注意:filter 會讓子元素一同透明,IE9+ 已棄用。
.transparent-table {
position: relative;
z-index: 0;
}
.transparent-table::before {
content: "";
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
background: url(bg.jpg) center/cover;
opacity: 0.3;
z-index: -1;
}
<body style="background: url('bg.jpg') center/cover fixed;">
<table class="semi-transparent">
<tr><th>項目</th><th>數值</th></tr>
<tr><td>項目一</td><td>100</td></tr>
</table>
</body>
<style>
.semi-transparent {
background: rgba(255, 255, 255, 0.85);
border-collapse: collapse;
width: 80%;
margin: 2rem auto;
}
.semi-transparent th,
.semi-transparent td {
border: 1px solid rgba(0, 0, 0, 0.1);
padding: 12px;
}
</style>
善用半透明效果,能為數據表格增添現代設計感。