一個div同時設置background-color和background-image的話,color是處于img層下方的,無法實現遮罩效果,所以需要再創建一個div作為其子div,然后設置子div的背景顏色,介紹兩種方法:
第一種,代碼如下:
css:
.wrap{
position: relative;
background: url(i/pic4.jpg) no-repeat;
-webkit-background-size: 100%;
background-size: 100%;
}
.warp-mask{
height:100%;
width:100%;
background: rgba(0,0,0,.4);
}html:
<div class="wrap">
<div class="wrap-mask"></div>
</div>第二種,通過after偽元素設置,代碼如下:
css:
.wrap{
position: relative;
background: url(i/pic4.jpg) no-repeat;
-webkit-background-size: 100%;
background-size: 100%;
}
.wrap-mask:after{
position: absolute;
top: 0;
left: 0;
content: "";
background-color: yellow;
opacity: 0.2;
z-index: 1;
width: 100%;
height: 100%;
}html:
<div class="wrap">
<div class="wrap-mask"></div>
</div>總結
以上所述是小編給大家介紹的通過css使用background-color為背景圖添加遮罩效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對軟件開發網網站的支持!
|
新聞熱點
疑難解答