CSS3 sets a mask for the background image and solves the problem of mask style inheritance

CSS3 sets a mask for the background image and solves the problem of mask style inheritance

In many cases, you need to process the background of the picture, such as setting transparency, blurring, etc.

However, if you set these effects directly on the tag where the background image is located, these styles will be inherited by the child tags.

Example 1: Setting a blur effect on the background label affects the text in the sub-labels

   <style>
        .parent{
            background: url('./test.jpg') no-repeat center;
            filter: blur(3px)
        }
        .son{
            filter: blur(0);
            /*
            Setting the same attribute in a child tag cannot override the inherited style*/
        }
    </style>

    <div class="parent">
        <p class="son">Hello</p>
    </div>

Solution:

Add a tag to the parent tag, make it absolutely positioned and fill the parent tag, and set the background/style in the tag.

   <style>
        .parent{
            position: relative;
            /*Let the parent tag be relatively positioned, and let .middle be relatively positioned*/
        }
        .middle{
            background: url('./test.jpg') no-repeat center;
            filter: blur(3px);

            position: absolute;
            height: 100%;
            width: 100%;
            
            z-index: -1;
            /*Reduce the layer height to prevent covering other sub-elements*/
        }
        .son{
        }
    </style>
    
    <div class="parent">
        <div class="middle"></div>
        <p class="son">Hello</p>
    </div>

This concludes the article on how to set a mask for a background image using CSS3 and solve the problem of mask style inheritance. For more information on CSS3 background image masking, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Introduction to the difference between OBJECT and EMBED tags used to display flash content

>>:  Detailed explanation of the new array methods in JavaScript es6

Recommend

Can't connect to local MySQL through socket '/tmp/mysql.sock' solution

Error message: ERROR 2002: Can't connect to l...

Vue.js cloud storage realizes image upload function

Preface Tip: The following is the main content of...

The 6 Most Effective Ways to Write HTML and CSS

This article shares the 6 most effective methods,...

Simple analysis of EffectList in React

Table of contents EffectList Collection EffectLis...

Detailed tutorial on docker-compose deployment and configuration of Jenkins

Docker-compose deployment configuration jenkins 1...

Detailed explanation of Windows time server configuration method

Recently, I found that the company's server t...

MySQL index usage instructions (single-column index and multi-column index)

1. Single column index Choosing which columns to ...

JavaScript Interview: How to implement array flattening method

Table of contents 1 What is array flattening? 2 A...

How to install elasticsearch and kibana in docker

Elasticsearch is very popular now, and many compa...

Six weird and useful things about JavaScript

Table of contents 1. Deconstruction Tips 2. Digit...

Detailed explanation of Vue-Jest automated testing basic configuration

Table of contents Install Configuration Common Mi...

Practical method of deleting associated tables in MySQL

In the MySQL database, after tables are associate...

In-depth explanation of the global status of WeChat applet

Preface In WeChat applet, you can use globalData ...

Why MySQL does not recommend using null columns with default values

The answer you often hear is that using a NULL va...