Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I do not want to inherit the child opacity from the parent in CSS

Tags:

css

I do not want to inherit the child opacity from the parent in CSS.

I have one div which is the parent, and I have another div inside the first div which is the child.

I want to set the opacity property in the parent div, but I don't want the child div to inherit the opacity property.

How can I do that?

like image 874
Lion King Avatar asked Apr 24 '11 11:04

Lion King


People also ask

How do you change the opacity of a parent without affecting a child?

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.

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

Basically, you can't override the opacity of a child. The answer you might be looking for will depend on what it is you are actually trying to do. Paulie is right, opacity effects the entire element (including children) and can't be reversed by a child element.

Is opacity inherited CSS?

The opacity property in CSS specifies how transparent an element is. Opacity has a default initial value of 1 (100% opaque). Opacity is not inherited, but because the parent has opacity that applies to everything within it.


2 Answers

Instead of using opacity, set a background-color with rgba, where 'a' is the level of transparency.

So instead of:

background-color: rgb(0,0,255); opacity: 0.5; 

use

background-color: rgba(0,0,255,0.5); 
like image 122
Dan Blows Avatar answered Oct 14 '22 06:10

Dan Blows


Opacity is not actually inherited in CSS. It's a post-rendering group transform. In other words, if a <div> has opacity set you render the div and all its kids into a temporary buffer, and then composite that whole buffer into the page with the given opacity setting.

What exactly you want to do here depends on the exact rendering you're looking for, which is not clear from the question.

like image 32
Boris Zbarsky Avatar answered Oct 14 '22 08:10

Boris Zbarsky