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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2021-06-15
  • 2021-12-06
猜你喜欢
  • 2021-11-29
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2021-12-19
  • 2022-12-23
相关资源
相似解决方案