Detailed explanation of the solution for CSS-opacity child elements to inherit the transparency of parent elements

Detailed explanation of the solution for CSS-opacity child elements to inherit the transparency of parent elements

In the process of writing the project page, I encountered the problem of child elements inheriting the transparency of parent elements. I searched a lot of documents and they all gave the first method. This method mainly solves simple scenes, but it is no longer applicable when setting a complex background.

Analysis of the reasons:

If the background color of the parent element is set to opacity: 0.5, the child element will inherit it. If opacity: 1 is set for the child element, the transparency of the child element is also set based on the parent element's 0.5.

First method:

When setting the transparency of the parent element background color, avoid using background: #000; opacity: 0.5. It is recommended to use background: rgba(0,0,0,0.5)

Second method:

If the background color is set to a complex background such as a gradient color, the first method is no longer applicable.

background-image: linear-gradient(-180deg, rgba(20,20,20,0.00) 19%, #303030 100%);
opacity: 0.5;

Because child elements inherit the opacity property of their parent elements, we make it not a child element. Add a child element, absolutely position it to the parent element, and then set the background color and transparency on the element.

<div class="container">
    <div class="content">
        <p>I am a DIV with class content</p>
        <p>My background is the background of class background</p>
        <p>Through relative positioning and absolute positioning, I moved the DIV with class background to my position. </p>
        <p>At the same time, my larger z-index makes it float on top of it. :)</p>
    </div>
    <div class="background"></div>
</div>
.container {
    width: 300px;
    height: 250px;
    color: #fff;
    position:relative;
}
.content {
    position:relative;
    z-index:5;
    width: 100%;
    height: 100%;
    overflow: hidden;
}
.background {
    background-color: #37a7d7;
    position:absolute;
    top:0px;
    left:0px;
    width:100%;
    height:100%;
    z-index:1;
    /*Compatible with IE7, 8 */
       -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
    filter: alpha(opacity=50);
    opacity:.5;
}

Reference: https://www.cnblogs.com/liu-l/p/3890861.html

This concludes this article on the solution to how CSS-opacity child elements inherit the transparency of parent elements. For more information about CSS-opacity child elements inheriting the transparency of parent elements, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. We hope that you will support 123WORDPRESS.COM in the future!

<<:  Detailed tutorial on installing Docker on CentOS 8.4

>>:  7 cool dynamic website designs for inspiration

Recommend

How to invert the implementation of a Bezier curve in CSS

First, let’s take a look at a CSS carousel animat...

How to configure CDN scheduling using Nginx_geo module

Introducing the Geo module of Nginx The geo direc...

How to test the maximum number of TCP connections in Linux

Preface There is a misunderstanding about the max...

Detailed installation and uninstallation tutorial for MySQL 8.0.12

1. Installation steps for MySQL 8.0.12 version. 1...

Mysql database recovery actual record by time point

Introduction: MySQL database recovery by time poi...

Instructions for using the meta viewport tag (mobile browsing zoom control)

When OP opens a web page with the current firmwar...

MySQL Interview Questions: How to Set Up Hash Indexes

In addition to B-Tree indexes, MySQL also provide...

JS implements dragging the progress bar to change the transparency of elements

What I want to share today is to use native JS to...

Creating a Secondary Menu Using JavaScript

This article example shares the specific code of ...

How to optimize a website to increase access speed update

Recently, the company has begun to evaluate all s...

Solve the problem of docker container exiting immediately after starting

Recently I was looking at how Docker allows conta...

Nginx location matching rule example

1. Grammar location [=|~|~*|^~|@] /uri/ { ... } 2...

mysql5.7.18.zip Installation-free version configuration tutorial (windows)

This is the installation tutorial of mysql5.7.18....

How to Use rsync in Linux

Table of contents 1. Introduction 2. Installation...

In-depth understanding of Vue's plug-in mechanism and installation details

Preface: When we use Vue, we often use and write ...