DevPub
Free Mousepad

Site5 Review: The Affordable Host

Site5 Review: The Affordable HostA look at the services offered by one of the most affordable Linux webhosts after a 3-year test period, Site5.com. Continue Reading

Website Statistics Now With Google Trends

Website Statistics Now With Google TrendsThe Google Trends service now offers website visitor statistics, aside from search query information. Continue Reading

Making PNG images transparent in IE6

Learn how to use a JavaScript trick that makes PNG alpha transparency work in Internet Explorer 6 simply by including a JavaScript file in your page.

On Friday, November 16th 2007 at 11:25 PM
By Andrew Pociu (View Profile)
*****   (Rated 4.7 with 24 votes)

PNG Alpha-Transparency

Transparent PNGPNG transparency, unlike GIF transparency supports full alpha blending, so that the background blends into the transparent area depending on the value at which the alpha channel is set. Point being: there's no doubt why you may want to use PNGs in your website. As you may know, PNG transparency is supported by Internet Explorer 7 as well as Firefox, thus you have the most popular browsers covered already. However, Internet Explorer 6 does not support PNG transparency, so we need to find a workaround.


PNG Transparency with Internet Explorer 6

The workaround for IE6 involves using a short JavaScript code that uses a 1 pixel transparent GIF file to simulate the transparency for IE6. Here is the code for the JavaScript file:

if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent && navigator.userAgent.indexOf("Opera")==-1)

{

    document.writeln('<style type="text/css">img { visibility:hidden; } </style>');

    window.attachEvent("onload", LoadPng);

}

 

function LoadPng()

{

    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');

    var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

 

    for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--)

    {

        if (itsAllGood && img.src.match(/\.png$/i) != null)

        {

            var src = img.src;

            img.style.width = img.width + "px";

            img.style.height = img.height + "px";

            img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"

            img.src = "1px.gif";

        }

        img.style.visibility = "visible";

    }

}

 All you need to do is place this JavaScript in your page (or in a JS file and then in your page), as well as a 1 pixel GIF file named 1px.gif that is transparent. Believe or not, you're done. Here is a sample HTML file that benefits from this hack:

<html>

<head>

<title>Transparent JS</title>

<script type="text/javascript" src="TransparentPng.js"></script>

</head>

<body>

<div style="background-color: #666666; text-align: center;">

<img src="Transparent.png" alt="Transparent" />

</div>

</body>

</html>

You can file all this code in the ZIP file attached to this tutorial, as well as the 1 pixel transparent GIF file and a sample transparent PNG file.

Download Download PNGTransparency.zip


The Downside

There is something you should know: if you are going to have a lot of PNG files that use transparency in a single page, it's going to increasing the loadng time because Internet Explorer will have to process the JavaScript code and apply the transparency for all those files. You shouldn't see any serious increased loading time for 10 transparent PNG files or less. However try placing 100 and it becomes a real problem. So try not to overuse this workaround, at least until most IE6 users migrate to IE7 and you can afford to alienate the few remaining users.


Rate Rate this tutorial

Comment Comment on this tutorial
Name: Email:
Message:

Comment Current Comments
by Dr. Strooi on Tuesday, December 11th 2007 at 11:29 PM

Thank you so much! This is the best solution I've seen 'till now. It works like a charm. Only png's used as background-images and rollovers remain untransparent, but I worked around it with layers on top of eachother. Turning the visibility of a layer off doesn't work, but you can solve that with a "hide layer" on onLoad (only when the onload is connected to an image) Great work! I hope my addition to it makes it even more usable.

by Minu on Saturday, December 29th 2007 at 12:24 PM

I too find this tutorial very useful, although I'm having an issue with my rollover images. I see Dr. Strooi has said that he had to add extra layers etc. Can this be explained in more depth. Here's the site that I'm currently working on, it's not finished yet... http://www.livingthevision.org/index2.html

by Cobbler on Wednesday, March 19th 2008 at 10:39 AM

Hello, This code is great. But how can I apply it for images those not normal images, but located in a form? I mean: <form name=....> <input type='image' src='transparent.png'> </form>

by Cobbler on Wednesday, March 19th 2008 at 10:40 AM

Hello, This code is great. But how can I apply it for images those not normal images, but located in a form? I mean: <form name=....> <input type='image' src='transparent.png'> </form>

by Cobbler on Wednesday, March 19th 2008 at 10:46 AM

And what about rollover images?

by Arti Ajans on Monday, April 14th 2008 at 08:04 AM

This is doesnt working. http://www.artiajans.net

by David Brazil on Monday, April 14th 2008 at 01:11 PM

Chapado.... Very Good !

by madhukar on Friday, April 25th 2008 at 03:16 AM

This is simply GREAT

by Santosh on Friday, May 16th 2008 at 05:10 AM

This is simply GREAT........ Thank you so much!

by RV on Sunday, May 25th 2008 at 07:47 PM

Thanx Mate, Great help. Works fine for me!

by David on Tuesday, June 24th 2008 at 05:40 AM

it's cool thank you very much.. this article help me to finish my task :)

by hekimboard on Tuesday, July 1st 2008 at 01:12 PM

<a href="http://www.izokon.com" >hekimboard</a>

by hekimboard on Tuesday, July 1st 2008 at 01:12 PM

<a href="http://www.izokon.com" >hekimboard</a>

by dis cephe on Thursday, July 10th 2008 at 11:20 AM

<a href="http://www.izokon.com" >Dış Cephe Kaplama</a> <a href="http://www.izokon.com" >hekimboard</a> <a href="http://www.izokon.com" >fibercement</a> thanks a lot...

by söve on Saturday, November 8th 2008 at 05:07 AM

<a href="http://www.biancaboya.com">söve</a> <a href="http://www.biancaboya.com">boya</a> <a href="http://www.biancaboya.com">mantolama</a> <a href="http://www.biancaboya.com">yalıtım</a> <a href="http://www.biancaboya.com">izolasyon</a> <a href="http://www.biancaboya.com">dış cephe</a> <a href="http://www.biancaboya.com">antibakteriyel boya</a> <a href="http://www.biancaboya.com">texture</a> <a href="http://www.biancaboya.com">grenli</a> <a href="http://www.biancaboya.com">kartonpiyer</a> <a href="http://www.biancaboya.com">siding</a> <a href="http://www.biancaboya.com">vernik</a> <a href="http://www.biancaboya.com">ısı yalıtım</a> <a href="http://www.biancaboya.com">fosforlu boya</a> <a href="http://www.biancaboya.com">yol çizgi boyası</a>

by söve on Saturday, November 8th 2008 at 05:07 AM

<a href="http://www.biancaboya.com">söve</a> <a href="http://www.biancaboya.com">boya</a> <a href="http://www.biancaboya.com">mantolama</a> <a href="http://www.biancaboya.com">yalıtım</a> <a href="http://www.biancaboya.com">izolasyon</a> <a href="http://www.biancaboya.com">dış cephe</a> <a href="http://www.biancaboya.com">antibakteriyel boya</a> <a href="http://www.biancaboya.com">texture</a> <a href="http://www.biancaboya.com">grenli</a> <a href="http://www.biancaboya.com">kartonpiyer</a> <a href="http://www.biancaboya.com">siding</a> <a href="http://www.biancaboya.com">vernik</a> <a href="http://www.biancaboya.com">ısı yalıtım</a> <a href="http://www.biancaboya.com">fosforlu boya</a> <a href="http://www.biancaboya.com">yol çizgi boyası</a>

by suyoz on Saturday, February 21st 2009 at 12:26 AM

Thank you people very much.

by suyoz on Saturday, February 21st 2009 at 12:26 AM

Thank you people very much.

by ilser on Tuesday, February 24th 2009 at 03:58 AM

www.ilserguvenlik.com.tr

by ilser on Tuesday, February 24th 2009 at 03:58 AM

www.ilserguvenlik.com.tr

by sameer on Tuesday, March 17th 2009 at 12:56 AM

not working on IE 7 in different machine,

by John on Wednesday, June 10th 2009 at 12:00 PM

This is GREAT! Thank you so much! Works fine for me www.lublurossiju.ru !

by John on Wednesday, June 10th 2009 at 12:01 PM

This is GREAT! Thank you so much! Works fine for me www.lublurossiju.ru !