Cross-Browser Transparency via CSS
Shortest post ever! You can quickly and easily apply transparency to any supportive element by adding the following CSS code your stylesheet:
selector {
filter: alpha(opacity=50); /* internet explorer */
-khtml-opacity: 0.5; /* khtml, old safari */
-moz-opacity: 0.5; /* mozilla, netscape */
opacity: 0.5; /* fx, safari, opera */
}
Check the code comments to see what’s doing what, and feel free to adjust the level of transparency by editing the various property values. Also, remember to replace “selector
” with the target element of your choice.
By the way, I’ve got a metric tonne of juicy CSS posts scheduled for the next few months. So whatever you do, stay tuned!
About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
22 responses to “Cross-Browser Transparency via CSS”
This CSS is good, but it has a small problems with validating. It is not validated by W3, causes of filter, -khtml-opacity and -moz-opacity attributes (W3 accepts only opacity attribute, and only in CSS3).
For standards compliance, use only the last rule and have it fail in IE and some others, or use the fixed background trick if possible. (Is extensibility defined in CSS?)
@ Simon Sigurdhsson :
Conditional comments for IE should ensure standards compliance.
Unfortunately, without using a browser detection script server-side or client-side to serve the non-standard CSS to Mozilla and kHTML, you’ll have to use CSS hacks to target these browsers.
STL
A trick for standard compliance?
Different css loaded via CSS browser detection or XSLT! :)
@Cooltad: True, validation is not top priority, but IMO proprietary stuff should be avoided, since they lead to a non-standardized mess — making life difficult for everyone.
@TaiPhanMem.org:
Is proper validation really that big of a deal? I mean, seriously, what honestly matters is that the code works well with everything. Validation should be a secondary priority.
Not to double-post but I managed to find the ‘blackhole’ link in Firefox by simply disabling CSS. So I’d prefer I don’t get banned. My IP starts with 76.111…
There’s also this lovely little collection:
<style type="text/css">
div.bg-trans
{
border:5px solid #000000;
/* FF2 & Opera Hack */
background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAA1JREFUCNdjYGBgOAMAANEAzdAP7DMAAAAASUVORK5CYII=);
/* Safari and FF3 support CSS3 syntax already */
background:rgba(0,0,0,0.8);
}
</style>
<!--[if IE]><style type="text/css">
/* For Your IEs Only */
div.bg-trans
{
background:transparent;
filter:progid:DXImageTransform.Microsoft.gradient
(startColorstr=#DD000000,endColorstr=#DD000000);
zoom:1;
}
</style><![endif]-->
Demo:
http://www.webdevelopedia.com/better_opacity.html
That certainly works better than it looks! It would be great to merge these methods with those in the article to produce the “ultimate” cross-browser transparency article! Thanks for sharing Bill! :)
@Cooltad: No worries! I only blacklist the bad guys! ;)
Hi just had a look at the opacity in i.e. and apparently there is a problem in getting it to work in both ie 7 and 8. They have a solution on
www . quirksmode.org/css/opacity.html
.opaque {
// first!
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
// second!
filter: alpha(opacity=50);
}
I havent used i.e. 8 yet so its not a big problem but might as well fix things now for the future.
@khaled: Thanks for the tip — I will be sure to include that method in the next article on this subject :)