Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Cross-Browser Compatible CSS Opacity

22 Responses »

Playing around in Photoshop, I understand the role that opacity can play in the design of an image. Why can't we take that over to the web? CSS opacity has been overlooked for quite some time and there doesn't seem to be one solid, cross-browser solution to the problem. Over the past day or two I've been looking at ways to create opacity in images WITHOUT using JavaScript. This worked for me using images and DIV elements.

.show-50 { -moz-opacity:.50; filter:alpha(opacity=50); opacity:.50; }

Example

Opacity Rules!

The downside to the above code, of course, is that it's not great when it comes to standards. As a developer though, I know that the most important thing is that it works! Tested in IE6, IE7, Firefox 2+, Safari, and Opera 9.

Discussion

  1. January 17, 2008 @ 11:10 am

    The only drawback with CSS opacity is that the text inside the div is blurred as well. Adding more opacity to the text inside the dic container do not solve the problem.

    css-tricks.com doucmented this issue and found a solution to this problem.

  2. September 9, 2008 @ 3:42 pm

    For IE 8 that code needs to be changed to:

    .show-50 { -moz-opacity:.50; -ms-filter:”alpha(opacity=50)”; filter:alpha(opacity=50); opacity:.50; }

    And to support old 1.x versions of Safari:

    .show-50 { -khtml-opacity:.50; -moz-opacity:.50; -ms-filter:”alpha(opacity=50)”; filter:alpha(opacity=50); opacity:.50; }

    See http://blogs.msdn.com/ie/archive/2008/09/08/microsoft-css-vendor-extensions.aspx

  3. January 13, 2009 @ 11:02 pm

    Thank you very much! :)

  4. February 16, 2009 @ 12:40 pm

    Thanks. Helped a lot ;)

  5. pierce o' broin
    April 24, 2009 @ 6:38 am

    Thanks for the suggestion but this rule can BREAK IE. I used it in conjunction with a large amount of javascript (and jquery) and it apparently caused a memory leak. BEWARE!!!!

  6. June 9, 2009 @ 7:24 pm

    So, how exactly do I use this?

    .show-50 – How can I apply this to specific tags? If I change it to .opaque-text {css here} will it work, or does that .show-50 part make it work?

  7. June 16, 2009 @ 5:33 pm

    Rich, the .show-50 is simply the class name, you can change this to whatever suits you. To implement this you’d use the following HTML:

    <div class=”show-50″>Content</div>

    Where “Content” is whatever you want in the container. If you changed the CSS to .opaque-text, you’d simply use:

    <div class=”opaque-text”>Content</div>

  8. August 2, 2009 @ 3:46 am

    heiy…it’s work for in all my testing browsers (firefox, IE:using IETester,opera,safari windows and chrome). thanx david! :)

  9. August 3, 2009 @ 4:46 pm

    This is pretty well documented and used often. It’s a cool feature that deserves attention, but calling it “overlooked for quite some time” is stretching too far in my opinion.

  10. October 3, 2009 @ 6:02 pm

    Neat, but does not comply to standards. Using a semi-transparent 1×1 px gif and a little absolute positioning does the trick for me :)

  11. October 31, 2009 @ 6:30 pm

    To make it AND valid, AND ok for IE, AND ok for Safari, AND ok for moz, AND etc….

    /**/


    (I hope all the code came trough)

  12. March 23, 2010 @ 6:51 pm

    Just tried this on a website i’m building

    It worked fine in mozilla firefox, chrome and safari and all varients of IE

    However the text inside the container that has opacity also has opacity which is unwanted

    Whats more, in IE the clear type effect is remove

    It would be good if you had a solution that also tackled that :)

  13. oneofthem
    March 24, 2010 @ 11:38 pm

    @Search Engine Optimisation:

    Whatever is inside the container, just select it with css and set the opacity to 1.0

  14. ih8divs
    April 22, 2010 @ 8:27 pm

    If you want text to appear and make an image transparent use the above mentioned style in the img tag and not in a surrounding div tag

    Content Text Goes Here.

    That should take care of your text problems inside the div

  15. ih8divs
    April 22, 2010 @ 8:36 pm

    ok that didnt show up.. lets try this…just take teh spaces out.

    If you want text to appear and make an image transparent use the above mentioned style in the img tag and not in a surrounding div tag

    Content Text Goes Here

  16. ih8divs
    April 22, 2010 @ 8:39 pm

    part 3 ? Replace [ ] with

    [div]
    [img src='name.ext' alt='tool_tip' border='0'
    style=' -khtml-opacity:.50;
    -moz-opacity:.50;
    -ms-filter:”alpha(opacity=50)”;
    filter:alpha(opacity=50);
    opacity:.50;']Content Text Goes Here.
    [div]

  17. ih8divs
    April 22, 2010 @ 8:42 pm

    You could also write the style in your style sheet and add the class to the img tag
    [img src='name.ext' class='show-50' border='0' alt='tool_tip']

  18. April 29, 2010 @ 12:25 pm

    @Tony: Semi-transparent 1×1 gif? Gif does not support alpha transparency. You have to specify which colors will be transparent. Can you provide more insight on how you make a div look dimmed with a gif?

  19. arthur
    June 14, 2010 @ 8:47 pm

    @Tony: Too bad gifs only support boolean transparency.

  20. arthur
    June 14, 2010 @ 8:50 pm

    @Juan Mendes: He’s likely mistaking it for a png, which does work—except for our buddy IE6. But its likely you’d already got a png fix going on so it should be too tricky.

  21. doug
    June 16, 2010 @ 4:31 am

    @Tony: Not sure how you’re achieving this but due to the matting on GIFs you cannot achieve alpha transparency. The only way you can achieve true transparency in a fully rendered image is by using 24bit PNG files however this is not support by IE6 (as usual!) without using the (well documented) IE PNG transparency behaviour hack which uses all sorts of gubbings like CSS and javascript… which, if memory serves, also isn’t standards compliant! :)

  22. tomás
    June 24, 2010 @ 11:34 am

    @Tony: That only works for backgrounds, and it must be a png, because there aren’t semi-transparent gif’s.
    If you want to make the text or the images transparent, how do you do it?

    You’re right, it doesn’t comply to standards… =(

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!