We’ve all seen them, the hoards of PNG fixes for IE6. That is because IE6 is a bag of smashed buttholes. I’m serious. It is. That is why we (web designers of the new world) have to continually come up with creative ways to solve the PNG issue. In case you are lost, just realize that in IE6, PNG images with transparency do not show their transparent regions, so you have to use some crazy IE6 proprietary filters. Moving on.

Lately, in projects I have been using a modified CSS snippet I found out on the interwebs to automagically replace PNG images with their AlphaImageLoader equivalent in IE6. Check this out here and I’ll explain and give an example:

The CSS/Code

* html img,
* html .png{
position:relative;
behavior: expression((this.runtimeStyle.behavior="none")&&(this.pngSet?this.pngSet=true:(this.nodeName == "IMG" && this.src.toLowerCase().indexOf('.png')>-1?(this.runtimeStyle.backgroundImage = "none",
this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizingMethod='image')",
this.src = "transparent.gif"):(this.origBg = this.origBg? this.origBg :this.currentStyle.backgroundImage.toString().replace('url("','').replace('")',''),
this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.origBg + "', sizingMethod='crop')",
this.runtimeStyle.backgroundImage = "none")),this.pngSet=true)
);
}
Update: This code changed to remove the behavior at runtime based on @Thierry’s suggestion.

For you purists out there, this is extremely hacky, so you may want to skip this whole entry entirely.

Update: : CSS Conditional comments for IE are a perfect way of hiding this hacky code from the good browsers. Here is a way to bring in the CSS for only IE 6 and below:

<!--[if lte IE 6]>
<link rel="stylesheet" type="text/css" href="png_fix.css" />
<![endif]-->

Ok, there won’t be much in the way of explanation here, but let me explain what this little bad-boy does.

The Selectors

* html img,
* html .png

The selector portion of the CSS rule targets which XHTML tags/classes this rule will be applied to. At first glance, it looks like all img tags as well as all tags with a class of "png". You’ll notice the (star) html in front of both of these. Wait, there is no “anytag” preceding the html tag, so this won’t get applied! True. For all non-crappy (that is a pro term by the way) browsers, this rule won’t get applied. However, since IE6 is a pile, it thinks that there is a tag before the html tag, so it will apply this rule. Great! Now IE6 is the only browser that will use this rule.

The Rest of the Code Jargon

Honestly, the rest you really don’t want to mess with much, but it suffices to say that this expression does a few things:

  • If the tag is an IMG tag, the expression checks to see if it is a .PNG image. If so, it applies a CSS AlphaImageLoader filter to the IMG tag to load the PNG file as a background image. It then points the src attribute of the image to a transparent gif so that the image isn’t overlaying it’s own background. Voila! Note: This only works well for images not resized by the browser. Also, you’ll need a transparent gif 1px by 1px image on your server.
  • If the tag is not an IMG tag, then this expression takes the CSS given background image and shoves it into a background AlphaImageLoader filter. It then removes the real CSS background-image rule so that the background and the filter are not conflicting.

Pretty cool! Please note that this CSS expression is pretty generic. If you have some crazy CSS-ing to do, you might want to stick with using your own methods or apply the AlphaImageLoader filter manually.

Also, if you are a standards-compliant junkie and this brushes you wrong, remember that this is intended to help with the crud of browsers. Sometimes we have to break our own rules to do the job.

A Demo!

Let’s see a demo! See the goodness below:

My Image Tag:

Twitterific Rules

My Generic Tag

Wish I could Play.

My Link Tag

Update: Links will need an additional rule of cursor: pointer; to restore the cursor. I’ll have to change that in the script to do that automatically

Comments

181 Comments

Pages:
  1. Music said 1253 days ago

    Cool thanks

  2. Apache & Me » Blog Archive » A more friendly .png fix for IE said 1259 days ago

    [...] png support for IE6. Like so many others, in the past I’ve used the TwinHelix solution. Rogie has his solution, Alan Hogan has his server side solution, and there are a million variations on these. Although the [...]

  3. derrick said 1267 days ago

    there is really no need for all this code.

    THE ABSOLUTE FIX:
    Use PNG 32 format
    Apply the STYLE=
    “filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sProperties)” … > to your div either directly or in CSS external file for ie.
    TO MAKE LINKS CLICKABLE INSIDE DIV:
    if you are using the above style on a div and there are links in that div, the links wont work. The Fix: the child div MUST have a position defined and the surrounding div CANNOT!
    Follow these easy instructions and it works perfect – no need for fancy fixes!
    If you know how to use conditional statements, it makes everything validate!!!

  4. derrick said 1267 days ago

    ok, got it to work.

    however, my linked images are clickable.
    any suggestions?

    thanks

  5. derrick said 1268 days ago

    didn’t work for me in IE 6

    nice try though.

  6. Souvit said 1269 days ago

    Hey sorry I dint get the transparent.gif image, sorry its working fine ……………..

    Thanks

  7. Souvit said 1269 days ago

    It works but not perfectly, image is coming with white border and a no image is coming too..

    anyway thanx for the hack great one…

  8. jason said 1270 days ago

    New to this, so forgive me for the naivete: tried the fix you suggest and it obliterated my page in IE7. As in, my background color and background image repeat along the bottom remained intact. Everything else? Done. Gone. Nowhere to be seen. But the background image slice (a png) looked damn good. Although, IE7 doesn’t really have an issue with it anyway. Did I link the css file incorrectly? I just copied and pasted it from your code up top (with corrected directory path, of course). Any advice?

  9. Misha said 1274 days ago

    дрОчь какой-то…
    блин, пиво хочется.. кто-то хочет пиво с рыбкой?

  10. IvanSok said 1274 days ago

    Хм… даже такое бывает.

  11. Sasha said 1274 days ago

    Works very well in IE6!

  12. Leo M. Aopo said 1276 days ago

    Let’s all work together to fight corruptions in Papua New Guinea and also minimise criminal activities such as rape, stealing and many many more.

    Kind regards,
    Leo Maino Aopo (Y-Saka) Mekeo
    Ph(D) +675 – 7193 7831

  13. Whitt said 1280 days ago

    You are freeaking awesome… I’ve tried all and nothing worked all the time.. this seems to work great!

  14. dersleri said 1282 days ago

    Cascading Style Sheets (CSS) web design lessons
    Css link Properties Attributes – examles

    http://css-lessons.ucoz.com/link-css-examples-1.htm
    http://css-lessons.ucoz.com/link-css-examples-2.htm

  15. Tomac1 said 1287 days ago

    Hi,

    I have problem with links in absolutely positioned div. I need Drag n drop menu with links but the linsk are not clickable.

    Transparent.gif is ok, having all .png a{position: relative; cursor: pointer;}
    I was tested z-index and some other solutions but its not functional in IE6.

    Anz ideas?

  16. David Horn said 1288 days ago

    Ah – this is great, thank you. I’ve been working a lot with Unit interactive’s PNG fix which (javascript based) has been perfect for 90% of my needs … however, it wouldn’t work with an AJAX script I had developed (or rather, I couldn’t get it to work), so this CSS version is just the ticket – thank you so much!!

  17. 11111RAKE11111 said 1293 days ago

    I dont know if it has been mentioned, but this hack did not work for me in IE unless a DOCTYPE was specified.

    great hack, thanks!

  18. BassEast said 1293 days ago

    doesn’t word

  19. thomasvdb said 1296 days ago

    Woohoo!
    Finally after trying several hacks this one works…

    The other ones did work but not in my menu. And your solution did it…

    Praise the lord!

  20. Jayant said 1300 days ago

    i tried using it as background image but it is not working. any solutions

  21. Warren said 1303 days ago

    ‘IE6 is a bag of smashed buttholes’

    very well said :) and thanks for the info’

  22. flash tekkie said 1306 days ago

    Any script execution at the Style Sheet stack of the browser is a performance issue and more importantly a security threat so the use of IE expressions should not be encouraged. It’s a non-standard approach that does not comply with W3C directives.

    Good news is Microsoft is ending expressions in Internet Explorer as announced on Thursday earlier this week. Indeed a very nice move towards the standard-compliant browser.

    The particular PNG fix should exclusively be targeted to IE6 only.

  23. Matt! said 1309 days ago

    Is it possible to use this like so:

    I tried doing that b/c I want to use the png in a background hover state.

    thanks!

  24. Fredi78 said 1313 days ago

    Estupendo, me funciona muy bien.

    Gracias.

  25. Tim Lavelle said 1317 days ago

    repeat*

    damn typos

  26. Tim Lavelle said 1317 days ago

    Lots of designers use the background: tag to place all of our background properties. This code snippet works really great, but would love to see it modified to also work with:

    background: somecolor url(somelink.png) 0 0 my-reapt-setting;

  27. Alan Hogan said 1318 days ago

    The example page is flaming, but my PNGGIF solution is the only one I know of that: Works with JS disabled + no momentary flicker in IE + easily works with background images, even those which are tiled or at a custom non-top-left position: http://demo.pixelsandpages.com/php-tests/images/test.html

  28. Jimbabwe said 1321 days ago

    Could just use png8 with alpha transparency. Will save lots of meaningless code, and whilst ie will get ugly gif like transparencies, at least the image will have a transparency.

    To make it work just change the type in fireworks to png8, change the transparency type to alpha and export as png.

    Sure beats making hacks.

  29. Kanedogg said 1322 days ago

    I also laughed histerically at your introduction, i even wondered if i was the only one whom has seen your hansolo impersonation ? Shhhhh only us nerds will see it ; )

    Nice work too man your type of thinking keeps us web legends alive …..

    Kane

  30. Web-Betty said 1322 days ago

    This works wonders, thanks. I hate having to use hacks for IE, but that’s just the way it goes. Since the W3C still reports that IE6 has a 25% market share as of August, I can’t let go of designing for it yet.

    Thanks again!

  31. Russ said 1330 days ago

    Your Foliage-o-meter is q-u-a-l-i-t-y.

    That is all.

Sorry, the comment form is closed at this time.