IE8 使用 filter: alpha(opacity=20) 實現半透明遮罩時,背景會變成不透明的黑色,而 Chrome/Firefox 正常顯示半透明。根因在於 IE 濾鏡需要元素擁有 Layout (hasLayout) 且背景必須為圖片而非純色。
.mask {
width: 100%; height: 100%;
background-color: #000; /* 純色背景觸發 bug */
position: fixed; top: 0; left: 0;
opacity: 0.2;
filter: alpha(opacity=20); /* IE8 */
-ms-filter: "alpha(opacity=20)"; /* IE8 標準語法 */
zoom: 1; /* 觸發 hasLayout */
z-index: 1;
}
.mask {
/* 方案 A:1px 半透明 PNG */
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==);
/* 方案 B:相對路徑圖片 */
/* background-image: url(../img/mask.png); */
background-repeat: repeat;
background-color: transparent; /* 移除純色背景 */
/* 其他屬性保持不變 */
}
background-color 在濾鏡下會被當作不透明處理。zoom: 1 或 width/height 觸發 hasLayout 是濾鏡生效前提。.mask {
background-color: rgba(0, 0, 0, 0.2); /* IE9+ 全支援 */
/* IE8 降級:可用條件註釋載入 PIE.htc 或單獨提供半透明 PNG */
}
專案若已放棄 IE8 支援,直接使用 rgba() 或 hsla() 最簡潔標準。