一个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
【转】通过css使用background-color为背景图添加遮罩效果
原文地址:https://www.cnblogs.com/seanyan/p/9299188.html