background-color: rgba(0, 0, 0, 0.4);
rgba 中的最后一個參數 0.4 就是想要的透明度,范圍在0~1之間。
在 ie 中一般是這樣的:
background-color: rgb(0, 0, 0); filter: alpha(opacity=40);
opacity 表示透明度,它的值范圍在 0~100 之間
那么如何兼容各瀏覽器呢?只要把它們寫在一起就行了。
由于 ie 不支持 rgba,所以會忽略之。其他瀏覽器對于自己不支持的,一般也會忽略。
下面來個示例:
HTML 代碼:
<body> <p class="non-transparent"> aaaaa </p> </body> <p class="transparent"> <p class="box"> box </p> </p>
CSS 代碼:
.non-transparent:hover { background-color: yellow; } .transparent { position: absolute; top: 0; left: 0; text-align: center; width: 100%; height: 100%; filter: alpha(opacity=40); background-color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0.4); } .box { background-color: yellow; width: 50%; height: 50%; position: relative; left: 5%; top: 10%; }
顯示效果:
chrome:
firefox:
opera:
ie8:
另外,在 chrome、firefox、opera 中也可以這樣:
opacity: 0.4;
但是這樣的話,會把所有子元素的透明度也設置為同樣的值,效果如下圖:
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com