122

I want to set Opacity of div's background without affecting contained element in IE 8. have a any solution and don't answer to set 1 X 1 .png image and set opacity of that image because I am using dynamic opacity and color admin can change that

I used that but not working in IE 8

#alpha {
    filter: alpha(opacity=30);
    -moz-opacity: 0.3;
    -khtml-opacity: 0.3;
    opacity: 0.3;
}

and

rgba(0,0,0,0.3)

also.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
  • You can't do this. The property opacity affect all the content of your element (other html elements + text). But I don't understand why you don't want use png. You only will have to change the image together you change the css (because I think, you have different css which can be switch by admin) – Elorfin Apr 14 '11 at 11:06
  • @Corum: How wrong you are (just as much as I was). It is possible. Check this blog post out. http://robertnyman.com/2010/01/11/css-background-transparency-without-affecting-child-elements-through-rgba-and-filters/ Incredibly clever. – Robert Koritnik Apr 14 '11 at 12:28
  • Just posted here: It might help I hope! – Armel Larcier Aug 01 '12 at 08:16

8 Answers8

261

The opacity style affects the whole element and everything within it. The correct answer to this is to use an rgba background colour instead.

The CSS is fairly simple:

.myelement {
    background: rgba(200, 54, 54, 0.5);
}

...where the first three numbers are the red, green and blue values for your background colour, and the fourth is the 'alpha' channel value, which works the same way as the opacity value.

See this page for more info: http://css-tricks.com/rgba-browser-support/

The down-side, is that this doesn't work in IE8 or lower. The page I linked above also lists a few other browsers it doesn't work in, but they're all very old by now; all browsers in current use except IE6/7/8 will work with rgba colours.

The good news is that you can force IE to work with this as well, using a hack called CSS3Pie. CSS3Pie adds a number of modern CSS3 features to older versions of IE, including rgba background colours.

To use CSS3Pie for backgrounds, you need to add a specific -pie-background declaration to your CSS, as well as the PIE behavior style, so your stylesheet would end up looking like this:

.myelement {
    background: rgba(200, 54, 54, 0.5);
    -pie-background:  rgba(200, 54, 54, 0.5);
    behavior: url(PIE.htc);
}

[EDIT]

For what it's worth, as others have mentioned, you can use IE's filter style, with the gradient keyword. The CSS3Pie solution does actually use this same technique behind the scenes, but removes the need for you to mess around directly with IE's filters, so your stylesheets are much cleaner. (it also adds a whole bunch of other nice features too, but that's not relevant to this discussion)

starball
  • 20,030
  • 7
  • 43
  • 238
Spudley
  • 166,037
  • 39
  • 233
  • 307
24

it's simple only you have do is to give

background: rgba(0,0,0,0.3)

& for IE use this filter

background: transparent;
zoom: 1;    
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#4C000000,endColorstr=#4C000000); /* IE 6 & 7 */
-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#4C000000,endColorstr=#4C000000)"; /* IE8 */

you can generate your rgba filter from here http://kimili.com/journal/rgba-hsla-css-generator-for-internet-explorer/

clairesuzy
  • 27,296
  • 8
  • 54
  • 52
sandeep
  • 91,313
  • 23
  • 137
  • 155
  • 1
    **What do gradients have to do with opacity?** :S – Robert Koritnik Apr 14 '11 at 11:28
  • @Robert Koritnik: See my version of this answer for a better explanation - http://stackoverflow.com/questions/5160419/red-bg-black-field-with-opacity-on-85-pink-text/5160668#5160668 also here: http://stackoverflow.com/questions/4788564/transparency-and-text-problem/4788642#4788642 – thirtydot Apr 14 '11 at 11:45
  • @robert, it's a only IE filter which available in web it's gave same effect as rgba gave & i am not a filter expert . – sandeep Apr 14 '11 at 11:47
  • @Robert it just works ;) [see this link](http://robertnyman.com/2010/01/11/css-background-transparency-without-affecting-child-elements-through-rgba-and-filters/) - @sandeep I fixed the filter code to add the necessary ingredients(transparent background and hasLayout), change the filter order .. feel free to rollback if you don't agree ;) – clairesuzy Apr 14 '11 at 12:13
  • @clairesuz, check my link it's also give these properties but when i work i didn't use these extra properties .It's work fine without it also. – sandeep Apr 14 '11 at 12:20
  • @clairesuzy: WOW! That's a very clever trick. I'll upvote this answer because it is answers exactly what was being asked. +1 from me then. This answer should be accepted as the correct one. – Robert Koritnik Apr 14 '11 at 12:27
  • 1
    @sandeep filters won't work without hasLayout, you may have another hasLayout property in your code, width maybe? I'm working one-handed..broke my wrist hence my slow replies, but here's [**jsfiddle**](http://jsfiddle.net/clairesuzy/tTAWF/) with the relevant IE code in a conditional comment for comparison of the two methods – clairesuzy Apr 14 '11 at 12:39
10

opacity on parent element sets it for the whole sub DOM tree

You can't really set opacity for certain element that wouldn't cascade to descendants as well. That's not how CSS opacity works I'm afraid.

What you can do is to have two sibling elements in one container and set transparent one's positioning:

<div id="container">
    <div id="transparent"></div>
    <div id="content"></div>
</div>

then you have to set transparent position: absolute/relative so its content sibling will be rendered over it.

rgba can do background transparency of coloured backgrounds

rgba colour setting on element's background-color will of course work, but it will limit you to only use colour as background. No images I'm afraid. You can of course use CSS3 gradients though if you provide gradient stop colours in rgba. That works as well.

But be advised that rgba may not be supported by your required browsers.

Alert-free modal dialog functionality

But if you're after some kind of masking the whole page, this is usually done by adding a separate div with this set of styles:

position: fixed;
width: 100%;
height: 100%;
z-index: 1000; /* some high enough value so it will render on top */
opacity: .5;
filter: alpha(opacity=50);

Then when you display the content it should have a higher z-index. But these two elements are not related in terms of siblings or anything. They're just displayed as they should be. One over the other.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
2

Try setting the z-index higher on the contained element.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
1

What about this approach:

 <head>
 <style type="text/css">
  div.gradient {
   color: #000000;
   width: 800px;
   height: 200px;
  }
  div.gradient:after {
   background: url(SOME_BACKGROUND);
   background-size: cover;
   content:'';
   position:absolute;
   top:0;
   left:0;
   width:inherit;
   height:inherit;
   opacity:0.1;
  }
 </style>
 </head>
 <body>
  <div class="gradient">Text</div>
 </body>
Daniel
  • 99
  • 1
  • 2
0

It affects the whole child divs when you use the opacity feature with positions other than absolute. So another way to achieve it not to put divs inside each other and then use the position absolute for the divs. Dont use any background color for the upper div.

-1

Maybe there's a more simple answer, try to add any background color you like to the code, like background-color: #fff;

#alpha {
 background-color: #fff;
 opacity: 0.8;
 filter: alpha(opacity=80);
}
-1

Use RGBA or if you hex code then change it into rgba. No need to do some presodu element css.

function hexaChangeRGB(hex, alpha) {
    var r = parseInt(hex.slice(1, 3), 16),
        g = parseInt(hex.slice(3, 5), 16),
        b = parseInt(hex.slice(5, 7), 16);

    if (alpha) {
        return "rgba(" + r + ", " + g + ", " + b + ", " + alpha + ")";
    } else {
        return "rgb(" + r + ", " + g + ", " + b + ")";
    }
}

hexaChangeRGB('#FF0000', 0.2);

css ---------

background-color: #fff;
opacity: 0.8;

OR

mycolor = hexaChangeRGB('#FF0000', 0.2);
document.getElementById("myP").style.background-color = mycolor;
Gajender Singh
  • 1,285
  • 14
  • 13