直接給 <table> 設定 opacity 或 IE filter:alpha 會導致內容(文字、圖片)跟著透明。需透過結構分離解決。
<style>
table {
width: 300px; height: 300px;
background: #fff;
filter: alpha(opacity=50); /* IE8- */
opacity: 0.5; /* 現代瀏覽器 */
}
#content-layer {
position: absolute; /* 脫離表格透明度影響 */
/* left/top 由 JS 或父層 relative 定位決定 */
}
</style>
<table>
<tr><td>
<div id="content-layer">
<p>文字內容</p>
<img src="logo.gif" />
</div>
</td></tr>
</table>
.transparent-table {
background: rgba(255, 255, 255, 0.5); /* 半透明背景色,不影響子元素 */
/* 或背景圖 */
background: url(bg.png) center/cover;
background-color: rgba(255,255,255,0.5);
}
rgba() / hsla() 僅影響背景層,子元素完全不透明。::before 搭配 opacity + z-index: -1。