經過css使用background-color爲背景圖添加遮罩效果

一個div同時設置background-color和background-image的話,color是處於img層下方的,沒法實現遮罩效果,因此須要再建立一個div做爲其子div,而後設置子div的背景顏色,介紹兩種方法:css

第一種,代碼以下:html

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僞元素設置,代碼以下:web

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>

 借鑑文章出處:https://blog.csdn.net/mr_fzz/article/details/53219367url

相關文章
相關標籤/搜索