I have a <div> tag that I have styled to have opacity: 0.5;. Within that <div> tag I have an <iframe> that I don't want to have 50% opacity. I'm wondering if there's any way to style the <div> element and exclude the <iframe> element that is inside the <div> element.
2 Answers
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. So:
- Set the
opacityto the child elements, except foriframe. - If you have some transparent background in your parent, use
rgbaor transparentpng.
2 Comments
Brian Dillingham
Here is a jsFiddle demonstrating the rgba mentioned above. jsfiddle.net/z85fgupu Notice the black background is 0.5 opacity due to the a or alpha setting of rgba()
sethtadd
So then how do I add the same background color(with 50% opacity) as the other elements to the iframe(it has a border)? EDIT: Didn't realize, if I use rgba it doesn't effect the iframe does it?
As @ROX stated you can't. In my opinion setting opacity to other child elements can be good choice. Try this:
.your_div *:not(iframe) { opacity: 0.5; } Another option would be using a wrapper with relative position and all other elements with absolute position inside that wrapper. JSFiddle
opacityto be0.5? background? The solution is set the opacity to the child elements, except for theiframe.