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. avo said over 1 year ago

    Hey,

    hmm in the css you use, where could be use the “this.runtimeStyle.background-position-y” width -10px??

    by removing the old background image, you remove also the old background position, that’s why…

  2. Komodek said over 1 year ago

    Very nice hack, thanks! :)

  3. daniel lopes said over 2 years ago

    This solution cause a runtime error because the server will no found url none for backgroundImage … this solution will generate many errors in your server log.

  4. Smart Pad said over 2 years ago

    I can see how annoyed you are with IE 6 or IE browsers. How I wish also that all browsers follow the same standard so that we, developers will no longer worry about cross-browser compatibility issues. Anyway, is there any way to exclude the transparent regions of png images from the clickable area?

  5. Komodomedia » Blog Archive » CSS Sliding Sprite Windows said over 2 years ago

    [...] an alpha-transparent PNG, which as you know, doesn’t display correctly unless you use an IE alpha image loader filter, which as you know doesn’t play nicely with background image positions. With this method, you [...]

  6. links for 2008-08-29 « Bread Butter ‘n’ Rock&Roll said over 2 years ago

    [...] CSS PNG Image Fix for IE Komodomedia – modified CSS snippet replace PNG images with their AlphaImageLoader equivalent (tags: webdesign) [...]

  7. Mister said over 2 years ago

  8. trauthjusetus said over 2 years ago

    I agreed with you

  9. Lauren G said over 2 years ago

    I laughed so hard at your first few paragraphs. I also love to use the phrase “interwebs”!

    First saw your website on Smashing for your foliage-meter (which I love btw), now here I am again thanks to Google.

    Thanks for making me giggle this Friday, 4 hours from quitting time!

  10. Ron B said over 2 years ago

    On an express bug-fixing job with low budget I just did, this fix saved my but.

    The link issue can be solved by putting in a larger transparent.gif which covers the entire area if the png. That will get your pointer back. The png will crop it to it’s size.
    Otherwise the 1px gif will be the clickable area and that is a bit much to ask.

    If a file fails to validate on hacks or other business that you know will not cut it, IMO it still validates. UTF-8 entities have the nasty habit of doing this as well as ampersands which you occasionally need to create dynamic paths in css. One could argue that is a weak point in the validator.

  11. coti said over 2 years ago

    it seems that is overwrite the center alignment..

  12. Arturo said over 2 years ago

    great fix!

    could you provide this with the htc approach?
    that would be of great help. I’ve been trying to loook for that with no success

  13. handan said over 2 years ago

    webgis images not display at ie6 and ie6,
    ff is display
    what?

  14. elisa said over 2 years ago

    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

  15. Working Transparent 24bits PNGs in IE6 | leonardoPicado.com's blog said over 2 years ago

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

  16. Shobhit said over 2 years ago

    @Rogie: Thanks Mate..

  17. Rogie said over 2 years ago

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

  18. Shobhit said over 2 years ago

    @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

  19. Kral Oyun said over 2 years ago

    Thanks, it’s work perfectly..

  20. seutje said over 2 years ago

    @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

  21. Shobhit said over 2 years ago

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

  22. Cosmi said over 2 years ago

    Thanks, useful stuff.

  23. Kilian said over 2 years ago

    IE PNG Fix v1.0 RC5

    http://gedankenkonstrukt.de/iepngfixmod/iepngfix-mod.html

  24. Scott said over 2 years ago

    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.

  25. khairy M.shaban said over 2 years ago

    first i need to thank u

    so how i can this code for my web site

  26. Colin Reilly said over 2 years ago

    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?

  27. Colin Reilly said over 2 years ago

    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?

  28. Colin Reilly said over 2 years ago

    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?

  29. w3c.kz said over 2 years ago

    Good!

  30. Rhiannon said over 2 years ago

    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. :) :)

  31. Peachy.se » Blog Archive » Ogillar IE webbläsaren said over 2 years ago

    [...] CSS png fix för ie fungerade inte för mig. Länk Ej heller detta png fix: Länk Inte heller denna, vilket jag har för mig jag andvänt mig av [...]

  32. Zelek said over 2 years ago

    I’m an amatuer designer just started two months back and this png crap is completely ticking me off… I ain’t ever used css before so if anyone can help me out with some lil’ detailed info on how to do this I would really appreciate it. I’ve tried diff. methods n it don’t work. The closest i got to it was using the javascript tech. but that only turned my whole background that pathetic grey colour. PLS HALP!!!!!!!!
    the site i’m working on is
    http://www.spacegoatgoa.com

  33. Le Ngoc said over 2 years ago

    I used image transparent .png is all ok, but it don’t apply with background image. Help me please …

  34. Chris said over 2 years ago

    @Andrew

    If you look at the code for the fix, there is is a line that says something about this.src = transparent.gif

    This is a transparent gif that is need for the fix, so make sure you have a 1px by 1px transparent gif on your server and you can name it whatever you want and put it where ever you want, just make sure you change the code to match your image

  35. Andrew said over 2 years ago

    Hello and thanx for the fix.

    It does solve the problem with the png transparency of the previously centered background image that I have in the header of the page, except that now it isn’t centered anymore, it’s left aligned.
    I am using now class=”png”.
    And if I put the image in a img tag itcenters correctly, only that it shows an ‘x’ in the top left side of the image, like when the image is missing.

    Do you have any suggestions?

  36. Chris said over 2 years ago

    @Nick
    To get background png images to show up you need to apply the png class to your div tags.

    Either by
    div id="yourid" class="png"
    or
    div class="yourclass png"

    leave your other css code the same just add the png class.

    If you still have trouble just view source this page and scroll down until you find where his examples are

  37. Rose said over 2 years ago

    Hello Rogie,
    I’m on Mac, I write XHTML/CSS and a friend tests my pages on Windows, so… I came on your site. What a perfection, wow, I’m a beginner compared to that. You write a beautiful code…
    Obviously, I peered at it (saw your comment on top :o}} nice way to say things! For sure examples teach better than tutorials, don’t they!)

    A part from that, I want to ask you (will you give me an answer…?) HOW you manage to make these extremely precise small low-resolution PNGs ! I left the gifs recently because they are not enough, png is all new for me.
    Do you start from a big one in high resolution and put the size down? HOW? Or else?
    I don’t come to these results, it’s a pity. Would you give me a hint or is it too much asked?
    I like your style. So much!
    Normally, graphists are not developpers, and the other way round…

    regards and admiration,

    Rose

  38. Louise said over 2 years ago

    I’m doing exactly the same as Nick, virtually the same code..it’s for rollover navigation…

    #buttonHolder1{
    position: fixed;
    margin-bottom: 30px
    }

    #home{
    display: block;
    width: 79px;
    height: 30px;
    background: url(../img/navigation/home.png) no-repeat 0 0;
    margin-bottom: 30px;
    }

    #home:hover{
    background-position: 0 -30px;
    }





    Can you see where I’m going wrong?

  39. Nick said over 2 years ago

    I’m confused about how to get css background images working.

    I put ‘class=”png”‘ on the items, but it’s no good. Normal images are working, so the transparent.gif is correct.

    I’m just a little confused.

  40. Chris said over 2 years ago

    @Nick

    You need to put another div tag inside the one the holds the button image. This div needs to have position:relative;, and thats all in it to make the button work.

  41. Nick said over 2 years ago

    hello, very nice fix :P
    it works for background png’s but i’m experiencing this problem with my navigation bar png’s:
    i’m using the following code for my buttons, but the fix makes them unclickable, any ideas?

    .button a:link, .button a:visited {
    display: block;
    background: url(button.png) no-repeat;
    height: 26px;
    width: 141px;}
    .button a:hover {
    background-position: 0px -26px;}

    (the png has 52px height, using the first 26px for normal state and the bottom 26px for mouseover state)

  42. Rogie said over 2 years ago

    @Asif – I think you are going to run into issues with position:fixed; and IE6. Also, you will want to add the png class to that corner div like so: div class="corner1 png"...

  43. Asif said over 2 years ago

    Mate, this is something really good.

    I got a prob with my CSS though. The website is using CSS backgrounds in png. How do you apply this to CSS images / backgrounds?

    example in the css file:

    div.corner1 {
    background: url(images/bg_03.png) top right no-repeat;
    position: fixed;
    z-index: 4;
    top: 0;
    right: 0;
    width: 400px;
    height: 500px
    }

  44. Kevin said over 2 years ago

    Thank you very much Chris. I got it solved. YESSS!

  45. Chris said over 2 years ago

    @Kevin

    Sorry the post doesnt show html code so what you should put in your html is:

    DIV ID=”Maincontent” class=”png”

    You would obviously need to put carrots around this but the class is necessary so that the alpha fliter is used on the div

  46. Kevin Kashou said over 2 years ago

    @Chris,

    You didn’t finish what you were saying on the second post. Did it get cut off?

  47. David Bigelow said over 2 years ago

    This did not work for me. Suggestions?

  48. Chris said over 2 years ago

    @kevin

    I dont know why but it cut off the example of the div tag i wrote here it is:

  49. Chris said over 2 years ago

    @Kevin

    You need to create a DIV ID with a background property in CSS. Something like this:

    #main_content {
    height:439px;
    width:500px;
    background: url(‘YourImage.png’) 0 0 no-repeat;

    Then when put it in you html code you have to add the class ‘class=”png’ like this:

  50. Chris said over 2 years ago

    Thanks Rogie, that worked great. But i have another problem. I used fireworks to create a menu bar, and using the JavaScript that it created the hover states of some of the buttons which have transparency in them dont show the transparency. Does anyone know how to either preload pngs with transparency, or another solution.

Sorry, the comment form is closed at this time.