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