DevPub
Free Mousepad

How Google Works

How Google WorksA look at the myths and facts about Google, and what you should know about Google's inner workings. Continue Reading

Search Engines and You: Getting Crawled

Search Engines and You: Getting CrawledHow to get your newly launched web site to be indexed by search engines and how directories can help you. 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.8 with 11 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