Notes on getting PNG transparency in IE6 with pure CSS

Recently, I started building a site with a lot of transparent image elements. The trouble was the the client required support for IE6 as well as IE7. Now, IE 6 and lower do not support transparent PNGs out of the box, but I knew that there were workarounds for that.

The question was, which workaround to use? One method I saw involved using an Explorer “behavior.” Unfortunately, this solution required loading and HTC file, which is an ActiveX control. ActiveX controls aren’t loaded in IE6 with default security settings. Maybe there is a workaround for that, but I couldn’t find it in a timely fashion. Besides, the HTC solution requires an HTC file and a special GIF file be stored on the server. Adding mysterious stuff to the client’s file tree is something I really like to avoid.

Then I ran across a brand new article (based upon a much older article) at A List Apart: Super-Easy Blendy Backgrounds. This article describes a pure CSS technique of getting image transparency to work in IE 6.

The core of the solution is as follows:

  • First wrap the PNG in a div,
  • then create a separate style sheet for IE 6- using conditional comments.
  • Within the IE6- stylesheet, give that wrapper div the style “filter: progid:DXImageTransform.Microsoft.AlphaImageLoader( xsrc='PATH_TO_PNG', sizingMethod='image');
  • Then set the width and height of the wrapper div to the width and height of the PNG.
  • Lastly, hide the PNG itself by giving it display: none.

As I understand it, the way all of this works is that AlphaImageLoader displays the specified PNG as the background-image of the wrapper div; and AlphaImageLoader respects PNG transparency. The problem is that the physical PNG image overlays the wrapper div, effectively hiding the transparent background PNG. Hiding the original PNG with display: none solves that.

I did learn a couple of things while implementing this solution:

  • When specifiying a relative path to an image for the filter, use the relative path from the PAGE WHERE THIS CSS IS CALLED, NOT THE PATH FROM THE CSS FILE ITSELF, as you would be when specifying a background-image: url, for instance.
  • You MUST specify width and height for the container div, or the image will not show up.
  • I also used image instead of scale for my sizingMethod, because I wanted my PNGs to behave just like regular images, not to scale to the size of their container.

Leave a Reply

You must be logged in to post a comment.