I don’t think it’s possible, but there is workarounds.
If you need opacity only in the background, you can apply a color rgba
:
background-color: rgba(0,0,0,.7);
Fiddle. Note that there is no support for IE<=8.
If you need to apply opacity to every element and not only in the background, you will have to break the hierarchy and turn them into brothers.
There is a third way, which works if you are seeking an effect overlay. It would create a pseudo-element ::before
covering every element .div-pai
and is below the .div-filho
:
.div-pai:before {
content: '';
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
background: white;
opacity: 0.3;
}
Fiddle.
Thus the :before
is a brother of .div-filho
and is below this without the need to z-index
due to the order of the elements. Note that the .div-pai
needs a position
other than static
, I didn’t add this to the code because your .div-pai
already is fixed
.
To .div-filho
also needs a position
other than static
to appear on the :before
. Otherwise, positioned elements always appear on static elements (see Stacking without z-indexin English).
For IE8 support, it is possible to use a .gif
/.png
with transparency as background, or apply the property filter
owner of IE5-8. I was unsuccessful in applying the filter
is a pseudo-element, but it works if we pollute the marking a little (by adding a div
).
To test on IE8: http://jsfiddle.net/qSsC3/12/show/
<div class="div-pai">
<div class="overlay"></div>
<div class="div-filho"></div>
</div>
.overlay {
content: '';
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
background: white;
filter: alpha(opacity=30); /* IE5-8 */
opacity: 0.3; /* IE9+ e todos browsers modernos */
}
Or, without creating any other element, another way that works cross-browser (IE8+), using an image as background: http://jsfiddle.net/qSsC3/17/show
.div-pai:before {
content: '';
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNXG14zYAAAAWdEVYdENyZWF0aW9uIFRpbWUAMDIvMDMvMTSFgaz/AAAADUlEQVQImWP4//+/LwAJSQNLpIypVwAAAABJRU5ErkJggg==);
}
I encoded the image as Data-URI to make it easier to put in the answer, but it won’t make a difference linking to a file .png
/.gif
(except that it would generate an extra request). I will also note that IE<8 does not support Data-Uris, although I believe that few people still care about support for IE<=7.
worked very well your suggestions, but in ie8 I did not succeed... It would be the only solution for ie8 a background image?
– Denis Bernardo
@Denisbernardo yes, um
.gif
/.png
with transparency would work well. Give me a second, it is possible to use thefilter
ie’s too.– Fabrício Matté
Excellent solution, worked perfectly. Thanks for the help, I will no longer need to use images... !
– Denis Bernardo