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

Transplanting the mkfs.vfat command in busybox under Linux system

In order to extend the disk life for storing audi...

Web developers are concerned about the coexistence of IE7 and IE8

I installed IE8 today. When I went to the Microso...

Creating a file system for ARM development board under Linux

1. Please download the Busybox source code online...

JavaScript countdown to close ads

Using Javascript to implement countdown to close ...

Introducing the code checking tool stylelint to share practical experience

Table of contents Preface text 1. Install styleli...

How to manage users and groups when running Docker

Docker is a management tool that uses processes a...

Detailed tutorial on installing harbor private warehouse using docker compose

Overview What is harbor? The English word means: ...

JDBC Exploration SQLException Analysis

1. Overview of SQLException When an error occurs ...

Detailed explanation of uniapp painless token refresh method

When the front-end requests the interface, it is ...

MySQL query_cache_type parameter and usage details

The purpose of setting up MySQL query cache is: C...

Navicat for MySQL tutorial

First, you need to download and install Navicat f...

Computed properties and listeners details

Table of contents 1. Calculated properties 1.1 Ba...

MySQL 5.7.17 winx64 installation and configuration tutorial

Today I installed the MySQL database on my comput...

Detailed explanation of Nginx reverse proxy example

1. Reverse proxy example 1 1. Achieve the effect ...