I am using something similar to the following code:
<div style="opacity:0.4; background-image:url(...);">
<div style="opacity:1.0;">
Text
</div>
</div>
I expected this to make the background have an opacity of 0.4 and the text to have 100% opacity. Instead they both have an opacity of 0.4.
You can use CSS 3
:before
to have a semi-transparent background and you can do this with just one container. Use something like thisThen apply some CSS
Sample: http://codepen.io/anon/pen/avdsi
Note: You might need to adjust the
z-index
values.I would do something like this
CSS:
It should work. This is assuming you are required to have a semi-transparent image BTW, and not a color (which you should just use rgba for). Also assumed is that you can't just alter the opacity of the image beforehand in Photoshop.
Children inherit opacity. It'd be weird and inconvenient if they didn't.
You can use a translucent PNG file for your background image, or use an RGBa (a for alpha) color for your background color.
Example, 50% faded black background:
You can use Sass'
transparentize
.I found it to be the most useful and plain to use.
See more: #transparentize($color, $amount) ⇒ Sass::Script::Value::Color
The following methods can be used to solve your problem:
CSS alpha transparency method (doesn't work in Internet Explorer 8):
Use a transparent PNG image according to your choice as background.
Use the following CSS code snippet to create a cross-browser alpha-transparent background. Here is an example with
#000000
@ 0.4% opacityFor more details regarding this technique, see this, which has an online CSS generator.
Just make sure to put width and height for the foreground the same with the background, or try to have top, bottom, left and right properties.