Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify parent divs opacity but make it not affect children HTML elements

I have a paragraph element inside a div. The div has an opacity of 0.3 & the paragraph has an opacity of 1.

When I show the elements, it appears the paragraph is transparent, like it has an opacity of 0.3.

Is there a way to make the paragraph inside the div have full opacity? Maybe I can set a CSS value for this?

<div style="opacity: 0.3; background-color: red;">
   <p style="opacity: 1;">abcde</p>
</div>
like image 273
sazr Avatar asked Nov 18 '11 03:11

sazr


People also ask

How do you use opacity without affecting children's elements?

Answer: Use the CSS RGBA colors There is no CSS property like "background-opacity" that you can use only for changing the opacity or transparency of an element's background without affecting its child elements.

Can you not inherit an opacity from a parent?

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).

How do I make opacity not affect my text?

The percentage of opacity is calculated as Opacity% = Opacity * 100 To set the opacity only to the background and not the text inside it. It can be set by using the RGBA color values instead of the opacity property because using the opacity property can make the text inside it fully transparent element.

How do you remove opacity from a child's element?

If you do not want to apply opacity to child elements, use RGBA color values instead (See "More Examples" below).


1 Answers

You can't, the opacity level is relative to the parent's opacity, always. So 1.0 inside 0.3 would be 100% of 0.3, which is 0.3, and 0.5 inside 0.3 would be 50% of 0.3 which is 0.15. If you're only using opacity for the background color, you can specify the color using the RGBA method so that the red will be opaque and not the content (and thus the paragraph inside it).

<div style="background-color: rgba(255, 0, 0, 0.3);">
   <p>abcde</p>
</div>

See here.

like image 138
animuson Avatar answered Sep 23 '22 01:09

animuson