Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set opacity in parent div and not affect in child div? [duplicate]

Hey i am searching in google but i can't fine any perfect answer

I want to Opacity in parent DIV but not Child DIV

Example

HTML

<div class="parent"> <div class="child"> Hello I am child  </div> </div> 

Css

.parent{ background:url('../images/madu.jpg') no-repeat 0 0; } .child{ Color:black; } 

Note: -- I want to background-image in Parent Div not Color

like image 538
Rohit Azad Malik Avatar asked Jun 04 '12 09:06

Rohit Azad Malik


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 I change the opacity of a DIV without affecting 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.

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


2 Answers

I know this is old, but just in case it will help someone else.

<div style="background-color: rgba(255, 0, 0, 0.5)">child</div>  

Where rgba is: red, green, blue, and a is for transparency.

like image 158
PaulSatcom Avatar answered Sep 21 '22 11:09

PaulSatcom


May be it's good if you define your background-image in the :after pseudo class. Write like this:

.parent{     width:300px;     height:300px;     position:relative;     border:1px solid red; } .parent:after{     content:'';     background:url('http://www.dummyimage.com/300x300/000/fff&text=parent+image');     width:300px;     height:300px;     position:absolute;     top:0;     left:0;     opacity:0.5; } .child{     background:yellow;     position:relative;     z-index:1; } 

Check this fiddle

like image 39
sandeep Avatar answered Sep 20 '22 11:09

sandeep