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

  1. Bill said over 2 years ago

    I like it. Except the CSS won’t validate (or as you say, it’s hacky).

    I think it would be much better to use “conditional comments” so that only IE6 will see it.

    Conditional comments are ideal because they are real W3C-compliant comments that only IE6 chooses to read. So, you get the best of both worlds. I would do something like this:


    @import "/png-fix.css";

    and put all of the hacky stuff inito png-fix.css.

  2. Bill said over 2 years ago

    Actually the conditional comments worked so well in my example above, you can’t even see them. But, you get the idea.

  3. Rogie said over 2 years ago

    Great suggestion @Bill. I have updated the tutorial to reflect your additions :)

  4. Jasper said over 2 years ago

    don’t care if it’s hacky – if it works where it’s supposed to, that’s all I care about. I shall banish it to the depths of my IE6 CSS file in the morning! many thanks for this – I’ve just been looking for this very solution!

  5. Kerwin said over 2 years ago

    I’ve read the same on some other blog, but it was getting way more into the scripting side.
    Thkx for making it a lot more easier to understand, it’s such a shame nobody thougt of this a few years ago…

  6. Jeff Byrnes said over 2 years ago

    Hmmm…tried it on my site, doesn’t seem to be working. Not sure what I missed, perhaps you can take a peek & point me in the right direction?

  7. Bill said over 2 years ago

    BTW, If you have IE7 installed as your default IE, but you have a copy of IE6 running as a standalone for testing, your standalone install of IE6 will think that it is IE7 when it looks at the conditional comments. You’ll want to test the CSS out without the conditional comments to make sure it’s working. Then add the conditional comments back in so that it never interferes with good browsers.

  8. Mike said over 2 years ago

    @Bill: You should be able to fix conditional comments using the methods over at PIE – they’ve always done the trick for me.

    Great tip for fixing PNGs. I long for the day when we won’t have to use these hacks any more ;)

  9. Rogie said over 2 years ago

    @All: Word to your mother on gettin’ rid of these hacks. One day IE6 will be banished to the fires of Mount Doom as it should. For now it will just infect us and make us weaker.

    @Jeff: Dude, tell me what’s going on or link me. I’d love to help you out on this issue. Maybe it’s a wee glitch in this script…

  10. zoel said over 2 years ago

    must be used “/style/images/transparent.gif”? ……

  11. Franck said over 2 years ago

    Great ! That is THE technic to fix PNG transparency.
    Thanks a lot.

  12. Christoph said over 2 years ago

    It’s not working in IE6. Lock at the screenshot: http://www.cgdesign.de/screen.gif

  13. Neil said over 2 years ago

    Does this work with repeated background images? (Something that has also been missing from other techniques).

  14. Rogie said over 2 years ago

    @Neil: Unfortunately, repeating background images doesn’t work with this technique. Man, I wish it did!

    @Christoph: Hmm, I can’t seem to replicate your screenshot. Are you running multiple versions of IE?

  15. Riccardo Giuntoli said over 2 years ago

    Nice article. Finally a good solution to the PNG problem in IE6.
    Thank u!!!

  16. bob said over 2 years ago

    Nice article with great solution to this problem. Otherwise users have to shift to .GIF but unfortunately they don’t support 24bit colors and alpha transparencies.

  17. Chris Basey said over 2 years ago

    Great trick – however it doesn’t seem to work if the background image is a link. Or rather – the technique works, but the image isn’t linked anymore.
    I am using background images for navigation – using image replacement, and while the transparency works like a charm they’re not clickable now
    Is there a way round this?

  18. Rogie said over 2 years ago

    @Chris: The trick should work as a link, in fact I will update this post to show a link, however it is a known glitch that in IE6 any child link tag within a AlphaImageLoader filtered tag will be rendered useless. That is to say, you got a link in any tag that has an AlphaImageLoader filter, it will NOT work. However, It may work when applied with JavaScript.

  19. Tom H said over 2 years ago

    Hi, I tried this method but Im having the problem where it displays the png correctly, but over the top of that has the non-loaded image box with a red x. Any ideas? Many Thanks

  20. Rogie said over 2 years ago

    @Tom H: Tom, be sure that you have the transparent.gif image uploaded to your server and that the path to it is correct.

  21. Simon said over 2 years ago

    Hi
    I would like to use this for a white logo on different coloured backgrounds. It needs to be positioned on the right hand side of my header using css. Will it position bottom right? Thanks

  22. Rogie said over 2 years ago

    @Simon: Sure, add some css to make it absolutely positioned, or better yet absolutely positioned within a relatively positioned header. Then apply add the rules bottom:0; right:0; That’s it!

  23. Luke said over 2 years ago

    Who knew? I could have sworn that azimuth property had something to do with accessibility for the deaf! This works very nicely btw :) and I added a js file that automatically adss the .png class to png images to ease it up a bit.

    Good job!

  24. Jerry said over 2 years ago

    A “bag of smashed buttholes”? Classic!

  25. Bjarne J said over 2 years ago

    Hi there

    A really clever attempt to “crack” the IE png-problem.

    One thing that bothers me…what about the performance ?. Normally expressions in CSS are really performance-heavy, and your solution “connects” directly to all img-elements in the DOM !

    Cheers

    BJ

  26. Bill said over 2 years ago

    Anyone who is having trouble with using PNGs in links on IE6 needs to avoid having the links absolutely positioned. For some reason IE6 will not let you click on a PNG background image when it’s absolutely positioned. I believe there is a way to fiddle with the z-index and trigger hasLayout in order to fix it. I can’t remember the details offhand, but doing a search for “absolutely positioned PNGs in IE6″ should give you some more info.

  27. Anna Vester said over 2 years ago

    Rogie, nice one here. I have tweaked your code a little bit and it now works in IE6 with repeated background images. All you need to do is to change crop to scale. It works like magic!

    I wrote about this addition to your png fix in my blog.

    Regards.

  28. Rogie said over 2 years ago

    @Luke: crap! azimuth….ok, lets choose a rule that doesn’t hinder anyone.

    @Bjarne: Word. Maybe a rule of * html img.png would suit better. That way this script only works on images with a class of “png”.

    @Anna: Very smart of you. However, scale will only work in one axis only, so if you need to repeat-x or repeat-y, you are golden, however if you need to actually repeat as in tile, I guess you are out of luck ;)

  29. akella said over 2 years ago

    really nice hacky code!

    But did you think about productivity of the script? As you might know expression will be calculated on each action on a page(click, resize, hover), not so good when you got dozen of them.
    And actually there is a way to execute it once on load. I’m just not sure weather your code taking it into account or not?

  30. Confluence: Site Design said over 2 years ago

    Some links for light reading – November 13th, 2007…

    Semantify, and CSS tools based on Blueprint…

  31. Thierry said over 2 years ago

    CSS expressions are evil ;-)

    http://developer.yahoo.com/performance/rules.html#css_expressions

  32. Rogie said over 2 years ago

    @Theirry: I totally agree. So you have 2 solutions:

    1: Hand code filters for PNG transparency.

    2. Recode the core of IE6 to make it NOT crap.

    Enjoy.

  33. Jasper said over 2 years ago

    I’m working on a website with a lot of png. This hack is beautifull. But i’ve seen to run in to a problem. For a specific object I overrule the standard background png defined before, to this I need to use “!important” on my background value in css (talking about a background on a div). But when I use “!important” the hack doesn’t work for that object. Now I’m going to try to figure out anther work around, but maby some of you readers have already got the solution? Many thanks, Jasper – mediaCT

  34. Riddle said over 2 years ago

    Thierry: They’re not if you know how to use them.

    Calculating height for element can and should be done real-time, without any checks, but if you simply replace elements or add new content, you should do this:

    jscript: expression(
    this.p ? 0 : (
    //your code here,
    this.p = 1
    )
    );

    This way code gets executed only once. I’ve been using them for a looong time and I haven’t encountered any problems.

    Rogie, let me be an attention ho and link to my fixes for adv CSS. ;-)

    And one more, I finished it yesterday. Believe or not, but this splash page looks the same in IE6, thanks to expression(). :)

    One day IE6 will be banished to the fires of Mount Doom as it should. For now it will just infect us and make us weaker.

    This is soo true. Do any of you nay-sayers know that IE7 supports first-child, advanced selectors, transparent PNGs and has all the Position is Everything bugs squashed?

    Whereas I need 10KB of hacks for my site to look the same in IE6, I only need 1 for IE7 (usually Generated Content, which I’m using for some time to clear floats).

    Expression() FTW, we can’t live in the Stone Age forever.

  35. Nick said over 2 years ago

    Bravo! I’ll be using this on my next project.

    Beautiful footer for this comments box by the way.

    As for links within a container with this fix applied, you can give them “zoom: 1″ or “position: relative” to trigger hasLayout and they should work.

  36. Thierry said over 2 years ago

    @Riddle: How many times this.p is evaluated?

  37. Riddle said over 2 years ago

    Thierry: You make IE think more when you hover a link than this small condition based on expando. :)

  38. akella said over 2 years ago

    AFAIK dropping “this.” where it is possible will make expressions 1-2 times faster.
    just my 2c

  39. Thierry said over 2 years ago

    Riddle: but that’s the whole point, as making IE thinks more is not really a good thing.

    Anyway, looking at this example, why not going with “behavior” rather than “azimuth”?
    * html .png {
    behavior:expression((this.runtimeStyle.behavior="none")&&(/* the stuff goes here */))
    }

    this.runtimeStyle.behavior="none"
    Should make sure it is ran only once.

  40. Geekstr said over 2 years ago

    “That is because IE6 is a bag of smashed buttholes”

    LMFAO! I almost shot Diet Coke out my nose on that one!

  41. Jermayn Parker said over 2 years ago

    Kewl! First time I have seen your blog and so a good start with a very good article on this buggy IE! The best solution however would be a mass assassination of all IE6 but alas I doubt it :(

  42. Rogie said over 2 years ago

    @Thierry: This is just the sort of thing I love to get comments back on. Let me see if I can implement the changes and modify this entry to reflect your changes.

  43. Jeremy Aldrich said over 2 years ago

    This is so cool. I have always disliked the png fixes for ie 6 and therefore never used them. But now that i have a fix that takes place through the css and fixes background images, i might rethink that. Many thanks for this ingenious fix. Just so happens that i came across this site at the same time that i inherited a site built by someone who didn’t know png’s didn’t work in 6. You saved me a lot of headache. Thanks again.

  44. Luke Veach said over 2 years ago

    @Riddle:
    your example pages are excellent.

    I took a look at the studio page link and was noticing that IE was downloading the button PNG everytime the background image shifted colors, is this just a caching issue on my side?

  45. Peter Velichkov said over 2 years ago

    you may find my version more advanced (also it was refactored by CNET.com javascript developer)
    http://blog.creonfx.com/internet-explorer/ie6-png-transparency-fix-javascript
    or the refactored version
    http://blog.creonfx.com/internet-explorer/ie6-png-transparency-fix-with-javascript-v20

  46. Justin said over 2 years ago

    This is a great bit of coding!!!!

    I hope someone could adapt it to make it work for repeated background images though, then it would be a killer script!!! Mainly for helping with background image borders that go from a colour to transparent where you can only use a png image.

  47. Justin said over 2 years ago

    Sorry, should have read all the posts in here first!!! Got it to work using Anna Vester’s code. Thanks.

  48. Rogie said over 2 years ago

    @Peter: Great work. For those that want to use a JavaScript framework, that is bomb.

  49. Luke Veach said over 2 years ago

    Rogie: Thanks much for the excellent examples and explanation (forgot to mention that in my comments above)!

  50. Peter said over 2 years ago

    np – frameworks save me a lot of time because i use it for couple of others scripts and not using a framework would cost me the same bandwidth penalty

    Cheers

  51. E11 said over 2 years ago

    Hi there,
    Thanks for the fix. I was using a Javascript fix (http://homepage.ntlworld.com/bobosola/pngtestfixed.htm) that worked similarly except you had to assign a width for all images which is fine until you start using background images.
    Both worked except for the top and bottom background images as shown here (http://www.e11world.com/pngfix.jpg)
    Can anyone tell me what I’m doing wrong?? Original page files are here (http://www.e11world.com/interactive/)

  52. Tom Hooper said over 2 years ago

    As it’s replacing the foreground image with a transparent gif, I assume Right-Click > Save Image As.. won’t work?

  53. Wouter de Bie said over 2 years ago

    Great stuff! Thank you very much!

  54. Rogie said over 2 years ago

    @Tom: You got it right. Stickin’ it to the IE6 users.

  55. phoose said over 2 years ago

    Shouldn’t the conditional comment target IE6 and below as opposed to the above which only targets below IE6?

    IE: if lte IE 7

  56. Rogie said over 2 years ago

    @phoose: “lte” stands for “less than OR equal to”, so in the case of “if lte IE6″, the code will work on all Explorer browsers less than or equal to IE6 that support conditional comments of course.

  57. Gregori said over 2 years ago

    Hi!
    I finished the translation of your article for the Portuguese language. Thanks for the permission.

    If you want to see:
    PNG Image Fix for IE (pt-br)

  58. Whatever-ishere said over 2 years ago

    thanks for the GREAT post! Very useful…

  59. danielpunt said over 2 years ago

    It works great, but the background images are positioned in the upper left corner, while they were centered or positioned right in the original CSS.

  60. Anders said over 2 years ago

    Hey Rogie! Thanks for this great tutorial. One question though.. How come IE6 shows the alt-tag next to the image? Is there some way to prevent this without removing the alt-tag? I kinda want it to validate. :)

    Thanks again.

  61. wzberger said over 2 years ago

    Unfortunately the W3C CSS validator recognizes some errors…

  62. Pix said over 2 years ago

    Thanks a lot! this tecnique works great but i’m having some trouble with png that need to be “rollovered”.
    in fact, if you have something like this

    a:hover { background: url(something.png) no-repeat bottom center;}

    this “fix” kills all the background position rule…
    any suggestion to avoid this?

  63. Rogie said over 2 years ago

    @Pix: Unfortunately, this won’t work with background-position in CSS. I haven’t seen IE’s ability to background position a filter…you are probably going to have to get really tricky with your CSS to pull that off.

    @Anders: I just tested it in IE6 and I cannot see the alt text next to the image. Are you sure you uploaded the transparent.gif file and it’s path is correct in the CSS?

  64. Euan said over 2 years ago

    This fix is great, I’m so happy with it. However, like the poster above me, I think it would kick even more butt if it could be used with the sliding doors technique for rollovers in IE6. Has anyone managed to achieve this with a png graphic and successfully implement this fix at the same time?

    Thanks :)

  65. Shaun said over 2 years ago

    Hi -

    Can someone please help me with this? I’ve got everything set up exactly (as far as I can tell) as it should be, but I still get the blue background. I’ve gotten it working putting the filter stuff inside a file tag for an image, but not in the stylesheet. I’m at a total loss. Check it out

    Thanks!

  66. Shaun said over 2 years ago

    Sorry, that link should be to http://www.tarves.net/test

  67. Santi said over 2 years ago

    GREAT FIX!!!!!!!!!!!!!!!!!!!!!!!!!

    the best that i saw.

  68. Joefrey Mahusay said over 2 years ago

    Thanks for sharing this wonderful css code. im gonna use this to my personal site. :)

  69. David Westerdael said over 2 years ago

    This workedperfectly for me thanks for the help! In a few cases I decided it would just be easier to build a duplicate png as a gif image and call that when ie6 loaded, but otherwise where i really didn’t want to give up quality I kept my pngs.

    For all if you need a great way to use multiple versions IE’s there is a cool program I got yesterday. here is the link : http://tredosoft.com/Multiple_IE

  70. Brie said over 2 years ago

    I have a form over a png background image that I can’t get to work – when this is applied there you can’t focus on the fields in IE6.

    I was able to get links over png background images to work just by adding position:relative, but that didn’t work for the form.

    Any ideas?

  71. Jeremy Anderson said over 2 years ago

    Forgive me if any of this has been addressed. I’ve been working quite a bit with AlphaImageLoader and these issues so I thought I would answer a few inquiries.

    First, the AlphaImageLoader filter does screw up links. Not always. It depends on the CSS properties assigned to the link. There is a fix that seems to work. If you assign a position: relative to the “a” tag it will usually fix it.

    Second, the AlphaImageLoader does allow for repeating background images or positioning background images within an element. However, for certain types of images, such as 1px by X, x or y repeats or 1px repeats, you can set the sizingMethod to scale. Scale will stretch the image to fit the defined area.

  72. Jeremy Anderson said over 2 years ago

    @Brie, the position: relative on the input fields should fix your form focus problem.

  73. Barbara said over 2 years ago

    Thank you so much! This was the simple solution I was looking for. So now we can only wait for the last IE6.- users to upgrade their software to give us webdesigners/developers some time to die our grayed hairs…
    http://stmap.beautifullalaland.com/index07-2.html

  74. Mark Abucayon said over 2 years ago

    I used it already and it works, it saves my day. Thanks

  75. Elisa said over 2 years ago

    I’ve been struggling with IE and PNG just like the rest of you guys and this this has been the best implementation of the alphaImageLoader fix that I’ve seen. Easy and as clean as it can be in all it’s messyness ;)

    THANKS!

  76. Peter Velichkov said over 2 years ago

    as suggested for repeating there are not many possibilities, i tried scaling here – http://blog.creonfx.com/internet-explorer/ie6-png-transparency-css-background-repeat-fix

    also we may try flash, or repeating divs with javascript

    What do you think ?

  77. Barbara said over 2 years ago

    Another solution for the rollover problem can be a simple layer visibility exchange, like this (note that the layers are named “Layer1″ and “hiddenlogo”):

  78. Aadi said over 2 years ago

    Great article and thanks for the ideas. I had a 24 bit png image as a heading background and this solution didn’t work for me for some reason, so I used conditional comments and css rule in the conditional css file for IE:

    — THE CSS RULE —
    #inner h1{
    color:#fff;
    margin:0;
    padding:0;
    background-position:top center;
    background-color:transparent;
    background-repeat:no-repeat;
    background-image:url(../images/vts_title.png);
    text-indent:-9999px;
    }

    — CONDITIONAL RULE —

    — Conditional CSS rule in pngfix.css —
    #inner h1{ filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=’images/vts_title.png’, sizingMethod=’scale’);
    position:relative;
    width:556px;
    height:23px;
    }

    Setting the width and height fixed the issue in this case.
    Thanks very much ;)

  79. James said over 2 years ago

    Great ..thanks

    But small problem is there, if we want background-position:bottom; then its not possible in ie6. could u plz help me…

    hight is 300px image hight is 50px.. now i want image in bottom of the td… is it possible in ie6?

  80. Daan said over 2 years ago

    Hi there,

    great work on the hack. Been searching for something like this for a while. Here is my website where I am using your code. Im no pro or anything so u might see some bad coding (be warned).

    However the problem I have is that my links aint working anymore (in IE6 that is) Every other browser just sees my links like they should. Could u give me any help here?

    Link; http://www.onemind-designs.nl/_projecten/lsz/website

    Cheers,
    daan

  81. Sad Panda said over 2 years ago

    I, like a few others, do not have any success with this. I’m glad for all of you that have it working. I reduced things down to the simplest case and it still fails.

    transparent gif: check
    applied class .png to DIV: check
    css included in style tag: check
    made sure gif path in script was correct: check

    No love. Curiously, I tried several other solutions that people seem to have working, but none work for me at all.

    On another note, I came here with Firefox, but wanted to see if IE 6 works on your site here. Hitting this page in IE 6 crashes it hard. Now this could be( and the png problems as well ) because I am running IE 6 in Virtual PC, who knows? Microsoft products are utter garbage( I am typing this in Firefox on a rock-stable linux box ).

  82. Sad Panda said over 2 years ago

    Follow-up!

    http://24ways.org/2007/supersleight-transparent-png-in-ie6

    A little bigger than the solution here, but it works.

  83. Daan said over 2 years ago

    Anyone? I can’t get it fixed. I even used position: relative in every a link. Meaning a:hover and such in my case.

    Still hoping for a little help.

    Greetz,
    Daan

  84. Daan said over 2 years ago

    Okey, got it fixed now. Dont know how I did it since I did alot on it. So im very sorry for not being able to explain it.

    Just wanted to let you know :-)

  85. Tomas Hlustik said over 2 years ago

    PNG TRANSPARENCY IN STD MODE (INTERNET EXPLORER 6)

    If you are experiencing problems with this solution in IE’s “quirk mode” as I did (due to XML prolog on the first line of your XHTML code), try the above mentioned link:
    http://24ways.org/2007/supersleight-transparent-png-in-ie6

    This works flawlessly :).
    That’s why I don’t use this solution.

  86. stevo said over 2 years ago

    great workaround.

    thanks a lot

  87. WebDrops said over 2 years ago

    One of the site effect of this solution seems to be an extra request to the server of the for “GET /none” PER IMAGE. If you have a large number of requests, this can be significant. Haven’t looked at the solution in detail to figure out a fix..has anyone else seen this?

  88. EvanG said over 2 years ago

    Hello, as sb else said, this css solution is just perfect, it’s clean and also works on dynamically added content :D

    However, I come here to say that it caused bugs on a few GIF images that I had absolutely positionned. I don’t know why, but they were still here, but totally transparent. So I added something so the fix would only act on PNGs. The code I finally came up with :

    behavior: expression(
    !this.src.match(“png”) ? 0 : (
    this.p ? 0 : (
    ……
    this.p = 1
    )
    )
    );

    Thank you very much Riddle for the “p” trick !
    For background images I added the filter manually, just had to set a height and a width for IE6 only (1px).

  89. EvanG said over 2 years ago

    Sorry to comment twice, but I also found that on my website, only backgrounds set on “no-repeat” should be sized to “crop”, others should be “scale”, because I use 1px high images as background. So I modified the code in that purpose, it may help some people I guess. I have also addressed differently the issue I was talking about in my previous post, so it still supports the png class. Finally :

    * html img,
    * html .png {
    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 = “/images/common/transp.gif”
    ) : 0
    ) : (
    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.currentStyle.backgroundRepeat != \’no-repeat\’) ? this.runtimeStyle.filter = “progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\’” + this.origBg + “\’, sizingMethod=\’scale\’)” : 0,
    this.runtimeStyle.backgroundImage = “none”
    ), this.pngSet=true
    )
    )
    );
    }

    This css + giving 1px height to certain divs in IE6 (if no background appears at all, it means the div needs a 1px height) = everything works fine now (at least the best way that could be done) !

  90. EvanG said over 2 years ago

    oops sorry, you’ll have to replace the \’ with ‘ in my code.

  91. Nitesh said over 2 years ago

    Hey Dude
    This is brilliant.
    I am having one problem though:
    If I have nested divs and I apply the fix to the parent div, the content in the child div is somehow getting screwed up…!!

    I validated my html and everything seems fine there..

  92. vitaly said over 2 years ago

    Nice! Thank you for your post.

  93. Png transparente con css | Cmacias.com said over 2 years ago

    [...] entonces cuando buscando el archivo, me encontré con un artículo en el cuál decían que se podia utilizar la transparencia del png sin necesidad de archivos [...]

  94. mae said over 2 years ago

    do you know if this will work in ruby on rails generated images?

  95. Rogie said over 2 years ago

    @mae – Yeah, as long as they use an IMG tag or a class of “png”, you should be set. :)

  96. » CSS Collection 2008 CSS Concept: CSS can be just that easy.. said over 2 years ago

    [...] CSS PNG [...]

  97. Chris said over 2 years ago

    I have a div tag that has a png as the background. I used this code to show its transparency, but on the page there is a form over the png. For some reason the form cannot be focused on, and there is also an email link which doesnt work either.

  98. Rogie said over 2 years ago

    @Chris – Try to put another DIV tag around the form and give it a CSS position:relative;

  99. Kevin said over 2 years ago

    I see a lot of site but some of them don’t seem to work.

    So what is the most valid way of getting my PNG background image in css and PNG images on the page to work?

    Thanks.

  100. 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.

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

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

  103. David Bigelow said over 2 years ago

    This did not work for me. Suggestions?

  104. 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?

  105. 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

  106. Kevin said over 2 years ago

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

  107. 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
    }

  108. 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"...

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

  110. 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.

  111. 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.

  112. 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?

  113. 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

  114. 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

  115. 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?

  116. 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

  117. 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 …

  118. 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

  119. 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 [...]

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

  121. w3c.kz said over 2 years ago

    Good!

  122. 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?

  123. 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?

  124. 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?

  125. khairy M.shaban said over 2 years ago

    first i need to thank u

    so how i can this code for my web site

  126. 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.

  127. Kilian said over 2 years ago

    IE PNG Fix v1.0 RC5

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

  128. Cosmi said over 2 years ago

    Thanks, useful stuff.

  129. Shobhit said over 2 years ago

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

  130. 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

  131. Kral Oyun said over 2 years ago

    Thanks, it’s work perfectly..

  132. 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

  133. Rogie said over 2 years ago

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

  134. Shobhit said over 2 years ago

    @Rogie: Thanks Mate..

  135. 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 [...]

  136. 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

  137. handan said over 2 years ago

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

  138. 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

  139. coti said over 2 years ago

    it seems that is overwrite the center alignment..

  140. 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.

  141. 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!

  142. trauthjusetus said over 2 years ago

    I agreed with you

  143. Mister said over 2 years ago

  144. 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) [...]

  145. 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 [...]

  146. 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?

  147. 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.

  148. Komodek said over 1 year ago

    Very nice hack, thanks! :)

  149. 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…

  150. Russ said over 1 year ago

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

    That is all.

  151. Web-Betty said over 1 year 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!

  152. Kanedogg said over 1 year 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

  153. Jimbabwe said over 1 year 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.

  154. Alan Hogan said over 1 year 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

  155. Tim Lavelle said over 1 year 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;

  156. Tim Lavelle said over 1 year ago

    repeat*

    damn typos

  157. Fredi78 said over 1 year ago

    Estupendo, me funciona muy bien.

    Gracias.

  158. Matt! said over 1 year 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!

  159. flash tekkie said over 1 year 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.

  160. Warren said over 1 year ago

    ‘IE6 is a bag of smashed buttholes’

    very well said :) and thanks for the info’

  161. Jayant said over 1 year ago

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

  162. thomasvdb said over 1 year 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!

  163. BassEast said over 1 year ago

    doesn’t word

  164. 11111RAKE11111 said over 1 year 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!

  165. David Horn said over 1 year 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!!

  166. Tomac1 said over 1 year 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?

  167. dersleri said over 1 year 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

  168. Whitt said over 1 year ago

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

  169. Leo M. Aopo said over 1 year 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

  170. Sasha said over 1 year ago

    Works very well in IE6!

  171. IvanSok said over 1 year ago

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

  172. Misha said over 1 year ago

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

  173. jason said over 1 year 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?

  174. Souvit said over 1 year 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…

  175. Souvit said over 1 year ago

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

    Thanks

  176. derrick said over 1 year ago

    didn’t work for me in IE 6

    nice try though.

  177. derrick said over 1 year ago

    ok, got it to work.

    however, my linked images are clickable.
    any suggestions?

    thanks

  178. derrick said over 1 year 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!!!

  179. Apache & Me » Blog Archive » A more friendly .png fix for IE said over 1 year 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 [...]

  180. Music said over 1 year ago

    Cool thanks

Sorry, the comment form is closed at this time.