CSS 給圖片或背景圖片加顏色遮罩


一個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>

 

 

https://blog.csdn.net/mr_fzz/article/details/53219367


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM