How to apply an opacity without affecting a child element with html/css?

Go To StackoverFlow.com

34

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

2012-04-05 00:52
by Benjamin Crouzier
Possible duplicate of CSS - Opaque text on low opacity div?Félix Gagnon-Grenier 2016-03-22 01:07
@FélixGagnon-Grenier I don't think that this solution will benefit OP since he'll probably want to fade the content too - xpy 2016-03-22 07:51


19

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.

<!DOCTYPE html>
<html lang="en">
    <head>
        <style>
            #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;
            }
        </style>
    </head>
    <body>
        <div id="container"></div>
        <div id="box">
            <p>Something in here</p>
        </div>
    </body>
</html>
  1. Use Javascript - almost the same as above, but position and size don't have to be hardcoded.
2012-04-05 00:59
by Ilia Frenkel
Just fyi, I think the numbering is off a lil' - Whymarrh 2012-04-05 01:03
This is not a solution as the box is no longer a child of the container - Griffin 2012-04-05 01:03
Another option might be to use jQuery UI Dialog (http://jqueryui.com/demos/dialog - Ilia Frenkel 2012-04-05 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 2012-05-14 21:04


  • 69

    Hi you can used opicity with background color as like this

    css

    #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);
    }
    

    HTML

    <div id="container">
    containter text
    <div id="box">
        box text
    </div>
    </div>
    

    ​Live demo http://jsfiddle.net/rohitazad/yT6nG/29/

    2012-04-05 05:04
    by Rohit Azad
    This answer is really good. However, can you provide some details about browser compatibility with this method - Sparky 2013-03-31 00:09
    @Sparky Here you go http://caniuse.com/#search=rgba ~ At the time of writing this has 85.51% browser support - schalkneethling 2013-11-04 11:51
    you are the man - Josh 2015-04-29 14:58
    doesn't work in chrome for different alpha - SuperUberDuper 2016-07-08 12:42
    This isn't really an answer. It doesn't really change the opacity, it changes the background color - NonameSL 2016-10-12 20:41
    ie8 does not exist rgba( - WantToDo 2017-03-19 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 - totymedli 2018-04-23 04:09


    4

    Try using rgba as a 'pre content' overlay to your image, its a good way to keep things responsive and for none of the other elements to be effected.

    header #inner_header_post_thumb {
      background-position: center;
      background-size: cover;
      position: relative;
      background-image: url(https://images.pexels.com/photos/730480/pexels-photo-730480.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb);
      border-bottom: 4px solid #222;
    }
    
    header #inner_header_post_thumb .dark_overlay {
      position: relative;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      background-color: rgba(0, 0, 0, 0.75);
    }
    
    header #inner_header_post_thumb .dark_overlay .container .header-txt {
      padding-top: 220px;
      padding-bottom: 220px;
      color: #ffffff;
      text-align:center;
    }
    
    header #inner_header_post_thumb .dark_overlay .container .header-txt h1 {
      font-size: 40px;
      color: #ffffff;
    }
    
    header #inner_header_post_thumb .dark_overlay .container .header-txt h3 {
      font-size: 24px;
      color: #ffffff;
      font-weight: 300;
    }
    
    header #inner_header_post_thumb .dark_overlay .container .header-txt p {
      font-size: 18px;
      font-weight: 300;
    }
    
    header #inner_header_post_thumb .dark_overlay .container .header-txt p strong {
      font-weight: 700;
    }
    <header>
      <div id="inner_header_post_thumb">
        <div class="dark_overlay">
          <div class="container">
            <div class="row header-txt">
              <div class="col-xs-12 col-sm-12">
                <h1>Title On Dark A Underlay</h1>
                <h3>Have a dark background image overlay without affecting other elements</h3>
                <p>No longer any need to re-save backgrounds as .png ... <strong>Awesome</strong></p>
    
              </div>
            </div>
          </div>
        </div>
      </div>
    </header>

    See a working codepen here

    2018-01-12 15:55
    by Jon Bennett
    This answer is better as it retains the cascad - DDD Soft 2018-03-05 16:29


    2

    Apply this css rule

    .alpha60 { 
    
    /* Fallback for web browsers that doesn't support RGBa */ 
    
    background: rgb(0, 0, 0); 
    
    /* RGBa with 0.6 opacity */ 
    
    background: rgba(0, 0, 0, 0.6); 
    
    /* For IE 5.5 - 7*/ 
    
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000,      endColorstr=#99000000); 
    
    /* For IE 8*/ 
    
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000,     endColorstr=#99000000)"; 
    }
    

    In addition to this, you have to declare background: transparent for IE web browsers.

    For more details visit the following link:

    http://robertnyman.com/2010/01/11/css-background-transparency-without-affecting-child-elements-through-rgba-and-filters/

    2012-04-05 01:04
    by vinitha
    @Bojangles, please read the OP again... the OP only wants a transparent background, and this answer, although a bit sloppy, provides a link to a great workaround - Sparky 2013-03-31 00:15


    1

    Another workaround is to simply use an overlay background to create a similar effect.

    I personally like a black overlay with about a 65% opacity, but for what you are trying to do you may want to use a white overlay at round 70%.

    Create a small (100 x 100 or less) PNG in Photoshop or GIMP that has the color and opacity you want. Then just set that as the background of your light box.

    If you create multiple PNGs at different opacities you can easily switch between them with JS or dynamically at load via backend scripting.

    It's not technically what you are trying to do, but aesthetically it can give a very similar effect and UX wise accomplishes the same thing. It is also very easy to do, and widely supported across pretty much everything.

    2018-03-29 13:45
    by Brian D.


    0

    Any child of an element with opacity set will take on that opacity.

    To achieve this style you could use rgba colours and filters for IE for the background, and opacity on the textual elements. So long as the second box isn't a child of one of the text elements, then it won't inherit the opacity.

    2012-04-05 00:58
    by michaelward82


    0

    This might not be the most orthodox method but you can use a small semi-transparent background image for each div / container that repeats. It does seem that in this day and age you should be able to achieve this in pure (simple not hackish) css with no js but as the answers above show it isn't that straight forward...

    Using a tiled image might seem dated but will work no worries across all browsers.

    2014-05-06 19:31
    by megaSteve4


    0

    Using background-color: rgba(#777788, 0.3); instead of opacity could maybe fix the problem.

    2018-05-09 07:33
    by Claudio
    rgba has not this sintax but rgba(255,0,0,0.3);DaFois 2018-05-09 07:54
    @DaFois is correct but the answer did solve my issue - FllnAngl 2018-09-17 09:53
    Ads