views:

1384

answers:

11

How can I fix PNG transparency bug in IE6 for background image?

+5  A: 

Use this: http://www.twinhelix.com/css/iepngfix/

This is also good for IE 5.5, but not for mac versions of IE or earlier version of IE.

I've used it on quite few sites and have had no problems with it.

There can sometimes be an ugly grey box around the PNG however until the script kicks in.

Tim Saunders
+2  A: 

The twinhelix png fix should help you

Steerpike
+2  A: 

For example with Dean Edwards' ie7.js

XOR
+5  A: 

Use PNG Behaviour.

ie6.css:

img {
   behavior: url("pngbehavior.htc");
}

page.html:

<!--[if IE 6]> 
  <link rel="stylesheet" type="text/css" href="ie6.css" />
<![endif]-->
vartec
+2  A: 

You could be brave and simply state that your site may not render well on IE6. Perhaps not the most commercially minded approach but we'd do all of ourselves a favor (even Microsoft) if we just let IE6 die. Of course since a large amount of online activity happens on corporate machines with IE6 nailed to them that isn't really going to happen soon. :(

AnthonyWJones
I agree. Anything besides a company website should stick to standards and ignore IE.
Zifre
A company website has even greater reason to stick to standards, the cost of developing for cross browser are much greater than dictating the browser, so you may as well say its going to be a standards only website get a browser that works with standards.
AnthonyWJones
You're forgetting about the cost of losing customers that don't have your supported browser and aren't willing to convert. That cost could be astronomically higher than the cost of multi-browser support.
Mystere Man
@Mystere: I don't think you can take this answer that seriously. I did state that its not a commercially minded approach. As with many things that would be good for everyone (like better management of the environment and elimination of poverty) what makes sense doesn't often make commercial sense.
AnthonyWJones
IE6 can be ditched. Seriously. Though it still has a market share of just over 21%.
iconiK
+9  A: 

I like this Javascript solution writen by David Cilley some time ago. It gets out of the way of compliant browsers and can be used with any back-end you want. It does still require a blank gif image though.

Add these functions to your HTML Header or other existing .js include:

<script type="text/javascript">
    function fixPngs(){
    // Loops through all img tags   
        for (i = 0; i < document.images.length; i++){
            var s = document.images[i].src;
            if (s.indexOf('.png') > 0)  // Checks for the .png extension
                fixPng(s, document.images[i]);
        }
    }

    function fixPng(u, o){
        // u = url of the image
        // o = image object 
        o.src = 'images/spacer.gif';  // Need to give it an image so we don't get the red x
        o.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + u + "', sizingMethod='scale')";
    }   
</script>

Put the following conditional comment at the bottom (footer section, just before </body> ):

<!--[if lte IE 6]>
    <script language="javascript" type="text/javascript">
        //this is a conditional comment that only IE understands. If a user using IE 6 or lower 
         //views this page, the following code will run. Otherwise, it will appear commented out.
        //it goes after the body tag so it can fire after the page loads.
        fixPngs();
    </script> 
<![endif]-->

For a more comprehensive approach to IE6 oddities, try the IE7 Javascript library.

Rob Allen
Are you sure this works on background images applied with CSS?
Josh Stodola
It does not work with background images in this setup, only img tags.
Rob Allen
+1  A: 

Be aware that the AlphaImageLoader transform can deadlock IE6.

Use PNG8s instead of regular PNG32s. You are restricted to 256 colors and 1 bit of alpha transparency, but it beats randomly deadlocking the browser.

George V. Reilly
A: 

To be a nitpicker, it's not a bug. IE6 simply doesn't offer PNG transparency.

Not supporting a feature is not the same thing as a bug.

Mystere Man
+1 for being correct and a bit pedantic
mkoistinen
+1  A: 

I'm going for the Necromancer badge here. :)

This isn't exactly a fix, but a handy workaround I'm using from time to time to support transparency on IE6 without any extra code at all. It won't always fit the bill, but pretty often it does.

The idea is that most of the time you will need to show your PNGs on a fixed color background anyway. A PNG file can have a background color specified, and IE6 will honor it. What you do is that you get the TweakPNG utility and open up your PNG file. There you will be able to add a bKGD chunk, which specifies the desired background color. Just type in the color that you will need to show the PNG on, and you're done.

One note - PNG files often include some values to compensate for different display devices. There might be things like intended color spaces, chromacities, gamma, etc. These aren't bad as per se, but IE somehow misinterpreted them, and the result was that the PNGs showed up darker than they should have been (or maybe IE was the only one who got it right? I don't remember...)

Anyway, if you want every browser to display your PNGs the same, you should delete these chunks with the above mentioned utility.

Vilx-
A: 
powpow
A: 

This is a great article about this problem. In summary, it provides a JS library called SuperSleight. I have used it in the past with a decent amount of success.

Randy Simon