PNG alpha transparency, fast and easy
March 15th, 2006
- Category:
- CSS,
- Web Development
Here’s another “fast and easy” installment: I wondered today, how to add PNG alpha transparency support to Internet Explorer the easiest way possible, without longish scripts and instead easily maintainable in a style sheet . This is not meant to spoil anyone’s good work and as you can see, the basic technique wasn’t discovered by me. It’s just an alternate and hopefully easier way to do it.
An accordingly transparent PNG image, a transparent GIF image (Ugh, I thought I’d never had to use these again…) and the following snippet is all you need.
EOMB will render your beautiful PNG with the following, very simple HTML:
<img class="png" src="/path/to/your.png" alt="different story" />
IE needs to get some special treatment:
img.png {
background-image: expression(
this.runtimeStyle.backgroundImage = "none",
this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizingMethod='image')",
this.src = "/path/to/transparent.gif"
);
}
Though I said I wanted to avoid longish scripts, JavaScript (in disguise of Dynamic Properties) is still necessary.
this.runtimeStyle.filter = ... is to apply the appropriate filter for loading and rendering your PNG correctly. Note that the path to the PNG is read from the img element’s src attribute via this.src, so that when you have to change the path you do it once and only once in your HTML source and can leave the rest as supplied before. That’s why I used an expression for this at all and not simply the filter property.
The next expression replaces the PNG with the transparent GIF image so that the same PNG displayed by the AlphaImageLoader filter one level below shows up. This has to be done after we have passed the value of the src attribute to the filter.
Do not omit the first expression, although it seems useless! You would throw IE into an infinite loop loading the GIF over and over again. Don’t ask why, please… Even though I don’t like IE I don’t want to make it hang.
Important: IE 7 has native PNG alpha transparency support. You should use Conditional Comments to include the above style sheet in one or the other way (besides that your style sheet wouldn’t validate):
<!--[if lte IE 6]>
...
<![endif]-->
Some final notes:
- Of course you don’t have to rely on the class name "png". It’s just an example. You can use whatever selector
you wantIE lets you use. - If there is no JavaScript, IE simply renders your PNG image without alpha transparency.
- If JavaScript is turned on, but ActiveX not, you’ll get nothing.
- The AlphaImageLoader filter is supported in IE 5.5+/Win.
- A little demo is here.
24 Comments
Comments feed
Comment by kosmar, March 16th, 2006 at 12:05 pm
Comment by Rioo, June 8th, 2006 at 10:29 am
Comment by abba bryant, June 28th, 2006 at 7:12 pm
Author Comment
Comment by Klaus, June 29th, 2006 at 12:13 pm
Comment by christof haemmerle, September 30th, 2006 at 8:21 am
Comment by Taote, December 19th, 2006 at 8:54 pm
Author Comment
Comment by Klaus, December 19th, 2006 at 10:49 pm
Comment by snf, January 28th, 2007 at 10:03 pm
Comment by Yuri Shoroxof, February 2nd, 2007 at 1:00 pm
Author Comment
Comment by Klaus, February 2nd, 2007 at 1:53 pm
Comment by Julian, February 8th, 2007 at 2:06 am
Author Comment
Comment by Klaus, February 8th, 2007 at 8:17 am
Comment by schnuck, February 26th, 2007 at 4:53 pm
Author Comment
Comment by Klaus, February 26th, 2007 at 6:00 pm
Comment by GiorgosK, March 8th, 2007 at 9:52 pm
Pingback by Transparent PNGs in IE without javascript » GeoLand.org, March 9th, 2007 at 1:50 pm
Pingback by Better PNG Transparency Fix for IE Browser at Axel Segebrecht, March 29th, 2007 at 2:35 pm
Comment by Yuri, May 14th, 2007 at 4:47 pm
Pingback by WebPRO BrightLight » Blog Archive » Internet Explorer 7 is here, August 21st, 2007 at 3:40 pm
Comment by Technophreak, August 26th, 2007 at 5:04 pm
Pingback by The Three Marketeers » A site’s spine, March 20th, 2008 at 10:42 am
Comment by Taha Paksu, April 5th, 2008 at 11:06 pm
Comment by Brad, April 10th, 2008 at 11:29 am
Comment by wilq32, February 6th, 2009 at 10:54 am