Latest TweetsWordPress and the Blank Target Vulnerability (aka rel noopener + noreferrer): perishablepress.com/wordpress-… #WordPress #security #html
Perishable Press

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!

Jeff Starr
About the Author Jeff Starr = Creative thinker. Passionate about free and open Web.
Archives
22 responses
  1. TaiPhanMem.org January 28, 2009 @ 4:23 am

    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).

  2. Simon Sigurdhsson January 28, 2009 @ 4:55 am

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

  3. @ 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

  4. A trick for standard compliance?
    Different css loaded via CSS browser detection or XSLT! :)

  5. Simon Sigurdhsson January 30, 2009 @ 3:01 pm

    @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.

  6. @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.

  7. 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…

    You have fallen into a trap!

    There is nothing here for you. So what are you doing here? Go home.
    Our robots.txt file explicitly forbids your presence at this location.

    Please note that the following Whois data will be reviewed carefully.
    If it is determined that you suck, you will be banned from this site forever.

    76.111.!!.!!!
    Comcast Cable Communications, Inc. JUMPSTART-5 (NET-76-96-0-0-1)

    Comcast Cable Communications, Inc. DC-27 (NET-76-111-0-0-1)

    # ARIN WHOIS database, last updated 2009-01-29 19:10
    # Enter ? for additional hints on searching ARIN’s WHOIS database.

  8. There’s also this lovely little collection:

    <style type="text/css">
    div.bg-trans
         {
              border:5px solid #000000;
              /* FF2 &amp; 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

  9. Jeff Starr

    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! :)

  10. Jeff Starr

    @Cooltad: No worries! I only blacklist the bad guys! ;)

  11. 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.

  12. Jeff Starr

    @khaled: Thanks for the tip — I will be sure to include that method in the next article on this subject :)

[ Comments are closed for this post ]