2

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
  • Why you want opacity to be 0.5? background? The solution is set the opacity to the child elements, except for the iframe. Commented Aug 31, 2014 at 5:11
  • possible duplicate of Set opacity of background image without affecting child elements Commented Aug 31, 2014 at 5:58

2 Answers 2

4

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 opacity to the child elements, except for iframe.
  • If you have some transparent background in your parent, use rgba or transparent png.
Sign up to request clarification or add additional context in comments.

2 Comments

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()
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?
1

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

3 Comments

This didn't seem to work for me, I tried it earlier, but thanks!
@SethTaddiken I've added another option, please see it
Thanks, that would work but personally I would rather use an rgba value as a background color.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.