67

I want to achieve this using html and css:

schema

I have tried to set the opacity of the container to 0.3 and the box to 1, but it doesn't work: both divs have 0.3 opacity.

jsFiddle of my try here

The effect I am trying to achive is a popup box that comes on top of the page. It is highlighted by fading the content below (by lowering the opacity).

Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236

12 Answers12

103

You can use opacity in combination with background color, like this:

#container {
    border: solid gold 1px;   
    width: 400px;
    height: 200px;
    background:rgba(56,255,255,0.1);
}

#box {
    border: solid silver 1px;
    margin: 10px;
    width: 300px;
    height: 100px;
    background:rgba(205,206,255,0.1);
}
<div id="container">
    containter text
    <div id="box">
        box text
    </div>
</div>

​Live demo

GSerg
  • 76,472
  • 17
  • 159
  • 346
Rohit Azad Malik
  • 31,410
  • 17
  • 69
  • 97
  • This answer is really good. However, can you provide some details about browser compatibility with this method? – Sparky Mar 31 '13 at 00:09
  • 4
    @Sparky Here you go http://caniuse.com/#search=rgba ~ At the time of writing this has 85.51% browser support. – schalkneethling Nov 04 '13 at 11:51
  • 6
    This isn't really an answer. It doesn't really change the opacity, it changes the background color. – NonameSL Oct 12 '16 at 20:41
  • ie8 does not exist rgba() – WantToDo Mar 19 '17 at 11:59
  • Nice workaround, but only if you want to add opacity to things whose color is defined by you in CSS. If that is the case and you need to add opacity to multiple different colors [custom CSS properties can make it easier](https://stackoverflow.com/q/6962432/1494454). – totymedli Apr 23 '18 at 04:09
  • This should be the accepted answer, it is the simplest and most effective. It can be easily replaced with `color:` instead of `background-color:` if you're objective with this is different. – Jake Apr 04 '19 at 11:41
  • Will this work if I want to add opacity to a background-image? – Dror Bar Oct 13 '20 at 13:02
22

As far as I know you can't do it in a simple way. There a couple of options here:

  1. Use absolute positioning to position box "inside" the container.

    #container {
        opacity: 0.3;
        background-color: #777788;
        position: absolute;
        top: 100px;
        left: 100px;
        height: 150px;
        width: 300px;
    }
    #box {
        opacity: 1;
        background-color: #ffffff;
        position: absolute;
        top: 110px;
        left: 110px;
        height: 130px;
        width: 270px;
    }
    <div id="container"></div>
    <div id="box">
        <p>Something in here</p>
    </div>
  2. Use Javascript - almost the same as above, but position and size don't have to be hardcoded.

Community
  • 1
  • 1
Ilia Frenkel
  • 1,969
  • 13
  • 19
  • 2
    Just fyi, I think the numbering is off a lil'. – Whymarrh Apr 05 '12 at 01:03
  • 24
    This is not a solution as the box is no longer a child of the container. – Griffin Apr 05 '12 at 01:03
  • 1
    Another option might be to use jQuery UI Dialog (http://jqueryui.com/demos/dialog) – Ilia Frenkel Apr 05 '12 at 01:10
  • Ilia, do you have any link(s) to give a hint about using javascript for this? I have 50% opaque parent
  • 's containing child images which I want 100% opaque. Setting the
  • to `position:relative;` & the img to `position:absolute;` does NOT allow me to override the inherited opacity on the img, and I can't use absolute positioning for the
  • 's themselves (that's a mess ;-). In Javascript I tried `imgs[i].style.opacity = '1';`, but that also does not work to override the inherited opacity. If I understand correctly, I also can't use rgba because I need to affect the img, not just a color.
  • – govinda May 14 '12 at 21:04
  • Since this does not answer's the opacity problem for a child element, its not a correct answer – Nishant Patel May 05 '20 at 05:27
  • 1
    @NishantPatel I've provided a possible solution to the problem. It was up to the question's original author to either accept it or reject it. In addition, it was 8 years ago! A lot has changed on the web since eight years ago :-) – Ilia Frenkel May 06 '20 at 06:08
  • @I Agreed its a fairly old post. – Nishant Patel May 06 '20 at 07:42