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.
You can use pure CSS 3:
rgba(red, green, blue, alpha)
, wherealpha
is the level of transparency you want. There is no need for JavaScript or jQuery.Here is an example:
Almost all these answers assume the designer wants a solid color background. If the designer actually wants a photo as the background the only real solution at the moment is JavaScript like the jQuery Transify plugin mentioned elsewhere.
What we need to do is join the CSS working group discussion and make them give us a background-opacity attribute! It should work hand in hand with the multiple-backgrounds feature.
This is the best solution I could come up with, NOT using CSS 3. And it works great on Firefox, Chrome and Internet Explorer as far as I can see.
Put a container DIV and two child DIVs in the same level, one for content, one for background. And using CSS, auto-size the background to fit the content and put the background actually in the back using z-index.
background-color:rgba(255,0,0,0.5); as mentioned above is the best answer simply put. To say use CSS3, even in 2013, is not simple because the level of support from various browsers changes with every iteration.
While
background-color
is supported by all major browsers (not new to CSS3) [1] the alpha transparence can be tricky, especially with Internet Explorer prior to version 9 and with border color on Safari prior to version 5.1. [2]Using something like Compass or SASS can really help production and cross platform compatibility.
[1] W3Schools: CSS background-color Property
[2] Norman's Blog: Browser Support Checklist CSS3 (October 2012)
The problem is, that the text actually HAS full opacity in your example. It has full opacity inside the
p
tag, but thep
tag is just semi-transparent.You could add an semi-transparent PNG background image instead of realizing it in CSS, or separate text and div into 2 elements and move the text over the box (for example, negative margin).
Otherwise it won't be possible.
EDIT:
Just like Chris mentioned: if you use a PNG file with transparency, you have to use a JavaScript workaround to make it work in the pesky Internet Explorer...
It's better to use a semi-transparent
.png
.Just open Photoshop, create a
2x2
pixel image (picking1x1
can cause an Internet Explorer bug!), fill it with a green color and set the opacity in "Layers tab" to 60%. Then save it and make it a background image:It works cool, of course, except in lovely Internet Explorer 6. There are better fixes available, but here's a quick hack: