Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add different opacities to nested items?

Tags:

html

css

I have two nested items in HTML and I want to give the wrraping one opacity 0.8 and the one inside it opacity 1.
I think I understand why it does not work, but, how can I mimick that effect?
Here is a simplified HTML that shows the problem, I want the green square to be solid.

<div style='background-color:red;
            width: 500px;
            height: 500px;
            border: 1px solid black;
            position: absolute;
            top:0;
            left:0;
            opacity:0.8'>

    <div style='width:150px; height:150px; background-color:green; opacity:1'>
      Some content
    </div>
</div>
like image 342
Itay Moav -Malimovka Avatar asked Mar 08 '11 20:03

Itay Moav -Malimovka


People also ask

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

You can add a container's sibling absolutely positioned behind container, with the same size, and apply opacity to it. And use no background on your container. Now container's children have no opaque parent and the problem vanishes.

How do you inherit opacity?

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

In which range opacity should be given?

The opacity property controls transparency and opacity of an element. Its values range from 0 to 1. Assuming defaults at parent level, an element with an opacity of 1 is completely opaque, whereas and element with an opacity of 0 is completely transparent.

What is opacity and how does it work?

An opaque substance transmits no light, and therefore reflects, scatters, or absorbs all of it. Both mirrors and carbon black are opaque. Opacity depends on the frequency of the light being considered. For instance, some kinds of glass, while transparent in the visual range, are largely opaque to ultraviolet light.


1 Answers

If you use the rgba CSS property instead of the opacity property you can achieve this:

<div style='background-color:rgba(0, 255, 0, 0.8) ;width: 500px; height: 500px; border: 1px solid black; position: absolute; top: 0; left: 0'>
<div style='position: relative; width: 150px; height: 150px; background-color: rgba(0, 0, 255, 1);'>aaaaaaaaa<br>aaaaaaaaa<br></div>
</div>

Demo: http://jsfiddle.net/ScHgC/

like image 115
Graham Conzett Avatar answered Sep 22 '22 14:09

Graham Conzett