Is it possible, using CSS only, to make the background
of an element semi-transparent but have the content (text & images) of the element opaque?
I'd like to accomplish this without having the text and the background as two separate elements.
When trying:
p {
position: absolute;
background-color: green;
filter: alpha(opacity=60);
opacity: 0.6;
}
span {
color: white;
filter: alpha(opacity=100);
opacity: 1;
}
<p>
<span>Hello world</span>
</p>
It looks like child elements are subjected to the opacity of their parents, so opacity:1
is relative to the opacity:0.6
of the parent.
As per my point of view best way to use background color with opacity if we use this then we will not lose opacity for the other elements like test color and border etc..
Use background color with opacity
background-color:rgba(R,G,B,Opacity);
CSS3 has an easy solution of your problem. Use:
Here, rgba stands for red, green, blue and alpha value. Green value is obtained because of 255 and half transparency is obtained by 0.5 alpha value.
If you are a Photoshop guy, you can also use:
Or:
There's an easier solution to put an overlay over an image on the same div. It's not the right use of this tool. But works like a charm to make that overlay using CSS.
Use an inset shadow like this:
That's all :)
A while back, I wrote about this in Cross Browser Background Transparency With CSS.
Bizarrely Internet Explorer 6 will allow you to make the background transparent and keep the text on top fully opaque. For the other browsers I then suggest using a transparent PNG file.
http://jsfiddle.net/x2ukko7u/?