Home >Web Front-end >CSS Tutorial >How Can I Control Parent DIV Opacity Without Affecting Child DIV Opacity?

How Can I Control Parent DIV Opacity Without Affecting Child DIV Opacity?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-07 11:07:13481browse

How Can I Control Parent DIV Opacity Without Affecting Child DIV Opacity?

Controlling Opacity in Parent DIV without Impacting Child DIV

In web development, achieving transparency effects while maintaining content clarity can be challenging. One common requirement is to set opacity in a parent DIV without affecting the opacity of its child DIVs.

To accomplish this effect, you can utilize the CSS opacity property within the parent DIV. By setting the opacity to a value between 0 and 1, you regulate the transparency of the background content within the parent DIV.

Example:

In the CSS code below, we set the parent DIV's opacity to 0.5, which allows 50% transparency for the background image:

.parent {
  background-image: url(../images/madu.jpg);
  opacity: 0.5;
}

Preserving Child DIV Color While Using Opacity

To preserve the black color of the child DIV while setting opacity in the parent DIV, utilize the following CSS property within the child DIV:

.child {
  color: black;
  background-color: transparent;
}

By specifying the background color as transparent in the child DIV, the color of its text remains intact, even with the parent DIV's transparency effect.

The above is the detailed content of How Can I Control Parent DIV Opacity Without Affecting Child DIV Opacity?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn