Two methods of adding masking effect to background image by CSS using background-color

Time:2019-8-12

If a div sets backgroundcolor and background-image at the same time, the color is below the IMG layer and can not achieve the masking effect, so we need to create a div as its sub-div, and then set the background color of the sub-div. Two methods are introduced:

First, the code is as follows:

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>

The second is through the after pseudo-element settings, the code is as follows:

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>

summary

Above is the introduction of background-color to add masking effect for background image through css. I hope it will be helpful to you. If you have any questions, please leave a message for me, and the editor will reply to you in time. Thank you very much for your support to developpaer.