Two ways to add a mask effect to the background image using background-color through CSS

Two ways to add a mask effect to the background image using background-color through CSS

If a div sets background-color and background-image at the same time, the color is below the img layer and the masking effect cannot be achieved. Therefore, you need to create another div as its child div, and then set the background color of the child div. There are two methods:

The first one, 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 method is to set it through the after pseudo element. 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>

Summarize

The above is what I introduced to you about using background-color through CSS to add a mask effect to the background image. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

<<:  Detailed explanation of the decimal padding problem of decimal data type in MySQL

>>:  CSS and HTML and front-end technology layer diagram

Recommend

The difference between MySQL execute, executeUpdate and executeQuery

The differences among execute, executeUpdate, and...

JS realizes automatic playback of timeline

Recently, I have implemented such an effect: clic...

Summary of the main attributes of the body tag

bgcolor="text color" background="ba...

SMS verification code login function based on antd pro (process analysis)

Table of contents summary Overall process front e...

xtrabackup backup and restore MySQL database

Due to some of its own characteristics (locking t...

Detailed explanation of the loop form item example in Vue

Sometimes we may encounter such a requirement, th...

What are the attributes of the JSscript tag

What are the attributes of the JS script tag: cha...

Detailed explanation of HTML form elements (Part 1)

HTML forms are used to collect different types of...

Zabbix uses PSK shared key to encrypt communication between Server and Agent

Since Zabbix version 3.0, it has supported encryp...

How to customize Docker images using Dockerfile

Customizing images using Dockerfile Image customi...

Five things a good user experience designer should do well (picture and text)

This article is translated from the blog Usability...

How to set up FTP server in CentOS7

FTP is mainly used for file transfer, and is gene...