Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable opacity on child element when parent element has opacity

Tags:

html

css

I have a container with an opacity of 0.8. At the background, I have an image that shines through the content div. Now, I have a photo of my client in this container. The problem is, that it uses the opacity of the parent element as the opacity for this image is just relative to the container and not to the body.

I have this code:

<div id="contentContainer" style="background: #FFFFFF; opacity: 0.8">     Content ...     <img src="..." style="opacity: 1.0" alt="Photo" /> </div> 

This does not work, as explained below.

Has anybody an idea?

like image 842
PDev Avatar asked Oct 18 '13 18:10

PDev


People also ask

How do you ignore the opacity of an element as a child?

This might help others who would want to use opacity, preventing a certain child element from getting opaque. You can use :not() Selector.

Is opacity inherited?

Opacity is not inherited, but because the parent has opacity that applies to everything within it. You cannot make a child element less transparent than the parent, without some trickery. Values are a number from 0 to 1 representing the opacity of the channel (the “alpha” channel).

Can I set an opacity only to the background image of a div?

There's no CSS property that you can use to change the opacity of only the background image. Unlike background colors, which allow you to adjust the alpha channel to control opacity, it simply doesn't exist for the background-image property.


1 Answers

Solved this problem by changing it to the following:

<div id="contentContainer" style="background: rgba(255,255,255,0.8);">     Content ...     <img src="..." alt="Photo" /> </div> 

Used just rgba alpha instead of opacity. Now it works.

like image 184
PDev Avatar answered Sep 21 '22 06:09

PDev