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

Detailed tutorial on building a local idea activation server

Preface The blogger uses the idea IDE. Because th...

How to uninstall MySQL 5.7.19 under Linux

1. Find out whether MySQL was installed before Co...

Detailed explanation of JavaScript's built-in objects Math and strings

Table of contents Math Objects Common properties ...

Detailed explanation of Tomcat's Server Options

1. Configuration By default, the first two are no...

How to automatically back up the mysql database regularly

We all know that data is priceless. If we don’t b...

Take you to understand MySQL character set settings in 5 minutes

Table of contents 1. Content Overview 2. Concepts...

How to use video.js in vue to play m3u8 format videos

Table of contents 1. Installation 2. Introducing ...

React internationalization react-i18next detailed explanation

Introduction react-i18next is a powerful internat...

MySQL 8.0 New Features: Hash Join

The MySQL development team officially released th...

js to implement web calculator

How to make a simple web calculator using HTML, C...

Basic introductory tutorial on MySQL partition tables

Preface In a recent project, we need to save a la...

How to compile and install opencv under ubuntu

Easy installation of opencv2: conda install --cha...

JavaScript anti-shake and throttling explained

Table of contents Stabilization Throttling Summar...

CSS3 overflow property explained

1. Overflow Overflow is overflow (container). Whe...