Dealing with IE6 and png's - part 2

Ok - here's the long awaited follow up to this little series of posts dealing with IE6 and png's.

The last time we saw how we can make use of transparent png's as backgrounds in our site layouts, included in our .css files. But what if we want to use png in our html source like so:

<img src="/images/transparentIllustration.png" heigth="20" width="200" />

Here's the way I prefer to deal with such situations. This method is a "implement once, work on all images" kinda thing.

The downside, but one I'm willing to live with, is that the IE user has to have JavaScript enabled.

First create a pngfix.js file and paste the following into it:

var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

if ((version >= 5.5) && (document.body.filters))
{
for(var i=0; i<document.images.length; i++)
{
var img = document.images[i]
var imgName = img.src.toUpperCase()
if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
{
var imgID = (img.id) ? "id='" + img.id + "' " : ""
var imgClass = (img.className) ? "class='" + img.className + "' " : ""
var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
var imgStyle = "display:inline-block;" + img.style.cssText
if (img.align == "left") imgStyle = "float:left;" + imgStyle
if (img.align == "right") imgStyle = "float:right;" + imgStyle
if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
var strNewHTML = "<span " + imgID + imgClass + imgTitle
+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
+ "
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "
(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"

img.outerHTML = strNewHTML
i = i-1
}
}
}

Then in the head of your page you do this:

<!--[if lt IE 7]>
   <script defer type="text/javascript" src="/js/pngfix.js"></script>
<![endif]-->

As much as I wish I was smart enough to come up with this my self, the credit goes to Bob Osola who in turn is crediting more people on his site.

Related Blog Entries

Comments
I tried using this and it didnt work. I am trying to have a shadow behind my main content on a patterned background just as your site. I dont have a set height for the content as it will vary from page to page depending on the text. How did you manage to get the shadow effect behind your main content on your site? Right now I just have it set as a background-image (png-24) that repeat-y on the content div.
# Posted By lavergara | 7/4/07 8:54 PM
Hi lavergara,

Could you give me an url so I can see what you are doing?
It'd be much easier to check out what might be wrong instead :)

Trond
# Posted By Trond Ulseth | 7/5/07 2:58 AM
Hi Trond, I somehow got the filter to work. I had my png in an image folder and on the IE6 hack filter I was referencing to it as src='../images/bg.png');. It worked when I moved the image to the root and referenced to it as src='bg.png'); which doesn't make sense to me as it called the image fine when I wasn't using the filter.
# Posted By lavergara | 7/5/07 8:28 AM
Hi lavergara,

That is weird, as I always have my images in a subfolder, and have no problems of the sort you describe.

Trond
# Posted By Trond Ulseth | 7/5/07 8:54 AM
On part 1 you explained how to get the drop shadow to show for the header part of your site. If you could, I would love to find out how you got the drop shadow for the remainder of the site (along the side of the #contentwrapper ). I love using patterned background and i like having the content on a div with a drop shadow just like your site is designed. I'm new to CSS so I'm learning as I go. Your site has been very helpful.
# Posted By lavergara | 7/6/07 1:29 PM
The backgrounds wont work on div's that do not have a height specified. Therefor I add height 100% to the containers. In the stylesheet for this blog it looks like this:

#contentwrapper {
   background-image: url(/images/content_bg1.png);
   background-repeat: no-repeat;
   }
   
   /* IE hack */
   * html #contentwrapper {
      height: 100%;
      background-image: none;
      filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='/images/content_bg1.png');
      }
# Posted By Trond Ulseth | 7/13/07 8:08 AM