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;

Comments

137 Comments »

Pages:
  1. elisa Jul 16 @ 00:33

    it seems that the links are not clickable at all… is there any solutions? i’ve tried to add the “cursor: pointer;” rule, but nothing happened!
    thanks

  2. [...] Solution There are many ways to go around this problem, last night while working for the brand new Fuel Economy Sitelet for [...]

  3. Shobhit Jul 07 @ 05:36

    @Rogie: Thanks Mate..

  4. Rogie Jul 05 @ 14:55

    @Shobhit: No, this hack does not validate. You would need to put it in an IE conditional comment.

  5. Shobhit Jul 04 @ 00:09

    @seutje: it might be shit for you, but it might be really important for a person whose site is not validating just cause of this FIX
    and standards are standards they need to be followed at any case

  6. Kral Oyun Jul 03 @ 09:33

    Thanks, it’s work perfectly..

  7. seutje Jul 03 @ 07:55

    @shobhit: fuck validating, it’s worthless and irrelevant, if the shit works, it works

    I don’t need W3C telling me it’s “not right”

    shit that W3C considers “correct” shows different on every browser, so screw them

  8. Shobhit Jul 02 @ 10:54

    Does this Hack VALIDATES CSS????? as per W3C Standards

  9. Cosmi Jul 01 @ 10:13

    Thanks, useful stuff.

  10. Kilian Jun 26 @ 06:19
  11. Scott Jun 25 @ 14:54

    I like your site a lot, and I’ve been tracking down a viable CSS IE6 PNG fix for hours now… but when I open this page in IE6, it doesn’t display properly - all of your other pages are fine though. Might want to have a quick peek in ie6.

    Keep up the great stuff tho, your site is really great.

  12. khairy M.shaban Jun 25 @ 12:34

    first i need to thank u

    so how i can this code for my web site

  13. Colin Reilly Jun 25 @ 10:15

    sorry. the html read itself.

    <div class=”infobox png”>

    <p>My info is shown here.</p>

    </div>

    (no textarea)

    for some reason, when i use a <p> tag, or a <span> tag to float left, style text, or whatever, in IE6, you cant select the text.

    do you know the solution to this problem?

  14. Colin Reilly Jun 25 @ 10:14

    sorry. the html read itself.

    <div class=”infobox png”>

    <p>My info is shown here.</p>

    </div>
    </textarea>

    for some reason, when i use a <p> tag, or a <span> tag to float left, style text, or whatever, in IE6, you cant select the text.

    do you know the solution to this problem?

  15. Colin Reilly Jun 25 @ 10:08

    i had a problem with this,

    for IE, i have a div with a png background. it looks like this.

    My info is shown here.

    for some reason, when i use a tag, or a tag to float left, style text, or whatever, in IE6, you cant select the text.

    do you know the solution to this problem?

  16. w3c.kz Jun 20 @ 03:53

    Good!

  17. Rhiannon Jun 20 @ 03:46

    I love you, you are a genius. After tearing my hair out with png fixes for a couple of sites i have largely tried to avoid using pngs AT ALL, until recently when i dared to have another go, been tearing out my hair again as none of the others seem to work completely right until i found your lovely lovely fix. You have saved my hair, my sanity and my website which now looks damn hot even in IE. :) :)

Leave a comment

  • Required
  • Required
    • Currently 3.33/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5