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

Collection of 25 fonts used in famous website logos

This article collects the fonts used in the logos...

How to implement Nginx configuration detection service status

1. Check whether the check status module is insta...

Summary of Vue 3 custom directive development

What is a directive? Both Angular and Vue have th...

How to install iso file in Linux system

How to install iso files under Linux system? Inst...

Don’t bother with JavaScript if you can do it with CSS

Preface Any application that can be written in Ja...

How to add vim implementation code examples in power shell

1. Go to Vim's official website to download t...

Tutorial for installing MySQL 8.0.18 under Windows (Community Edition)

This article briefly introduces how to install My...

10 content-related principles to improve website performance

<br />English address: http://developer.yaho...

MySQL Failover Notes: Application-Aware Design Detailed Explanation

1. Introduction As we all know, in the applicatio...

CentOS 8 custom directory installation nginx (tutorial details)

1. Install tools and libraries # PCRE is a Perl l...

Detailed explanation of the use of state in React's three major attributes

Table of contents Class Component Functional Comp...

How to install MySQL 5.7.17 and set the encoding to utf8 in Windows

download MySQL official download, select Windows ...

Diagram of the process of implementing direction proxy through nginx

This article mainly introduces the process of imp...