Make PNG transparency work in Internet Explorer

Advertisement

Directly inserting images in PNG format with transparency will leave you with a white spot when display in Internet Explorer. It makes your image looks ugly and this is really the last thing any web designer or webmaster would want on their website. Here’s a solution on how to solve this white spot and let the PNG transparency recovers.

Let’s take a look at the difference of PNG images (with transparency) on both major browsers: Mozilla Firefox and Internet Explorer 6

Mozilla Firefox

PNG with transparency blends in nicely with no problem

PNG image in Mozilla Firefox

Internet Explorer 6

Internet Explorer does not turn your transparency transparent, thus leaving you with a big ugly white spot.

PNG image in Internet Explorer

Solution

Create a container to store your image. In this case I use a <div>.
Create your <div> inside your <body>, just like this.

<body> <div class="flower"></div> </body>

Next, create a <style> if you dont have one. Make sure they are between your <head> </head>. Put the following css inside.

<style>
body {background-color:#000}
div.flower {background:url(flower-transparent.png) no-repeat; height:100px; width:100px}
</style>

The CSS codes above displays your PNG image in a <div>. Works fine for Mozilla Firefox, but not for Internet Explorer. To get it working cross browser, create another set of css just for Internet Explorer right below your <style> </style>. Insert the following codes.

<!–[if gte IE 5]>
<style type="text/css">
div.flower {
background:none;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=’flower.png’ ,sizingMethod=’crop’);
}
</style>
<![endif]–>

Your IE should now give you a perfect blend like the picture below.

PNG image in Internet Explorer

Download my working example.

Related Contents

Sponsors

Posted by hongkiat in Web Tricks , at 12.17.06

Tags: , , , ,

Comments

  1. Jason December 17th, 2006

    Cool! Never knew it could be done that way.

    Reply
  2. tihtahpah December 18th, 2006

    oi..hongkiat..i linked u to my blog :) good stuff…hitswhore!

    Reply
  3. Adam December 18th, 2006

    Yea I couldn’t be bothering doing to to my site, I’ve got so many PNGs on there any majority of people use Firefox and soon IE7 so if your lazy.

    Reply
  4. Ryan December 18th, 2006

    There are a few drawbacks to this method.

    The first is that this is an ActiveX filter, so people who have security features blocking ActiveX controls might have a problem seeing it.

    The other is that if you want to put any information inside the div where you have the png as a background, it won’t show up. This AlphaImageLoader puts a mask over the entire div that it’s applied to, so any fields that are interactive won’t be able to be accessed, such as form elements.

    The way to get around this would be to use the following structure:

    #outerdiv { positioning of background and interior information }

    #middlediv { AlphaImageLoader script with png as background, no positioning }

    #innerdiv { positioned inside the above two divs, with your information }

    The innerdiv’s relative/absolute positioning will cause it to float above the background div, letting you get to text fields or form buttons. The outerdiv will let you position this entire thing so that you can move it around the page.

    Hope this adds a little more for those trying it out.

    Reply
  5. Ashish Mohta December 18th, 2006

    Thx for the tip.I was looking for it .

    Reply
  6. hongkiat December 18th, 2006

    Adam:Firefox is soon to be taking over. This is to cover up the loophole of IE6

    Ryan: Your method works too, and thanks for providing another alt solutions to this.

    Reply
  7. Michael December 18th, 2006

    …and it doesn’t validate :-(

    Reply
  8. Ryan December 18th, 2006

    I just make my code W3C compliant. If someone wants to use a non-compliant browser, their loss.

    Reply
  9. Ryan December 18th, 2006

    Here’s some more info on what I was talking about:

    http://spiltmilkmedia.com/ryan/blog/?p=221

    Reply
  10. Eric December 18th, 2006

    Love it, but how can I implement this on an enterprise level? I know I can make my graphic designers drool if I tell them I can seamlessly integrate all those transparent gradients they want to use, but isn’t there a better way than creating 3 DIVs with individual CSS definitions? Can we at least use all this code in the ?

    Reply
  11. Eric December 18th, 2006

    The BODY, that is…

    Reply
  12. Ryan December 18th, 2006

    You only need three divs if you want to position the element, and then also have some interactive elements inside.

    If you just want text, don’t worry about the fact that the AIL filter will mask your div, and just use one.

    Reply
  13. brett borders December 18th, 2006

    Doesn’t IE 7 handle .png transparencies OK?

    Reply
  14. lazy11 December 18th, 2006

    seriously – WHY ON GOD’S GREEN EARTH would you bother with this?
    look at the fat chunk of shit that you have to maintain now as a result of this senseless hack.
    even maintained a site that uses this “trick”

    no thanks – stick to something simpler – or do a better job when designing.

    you’re asking for trouble.

    Reply
  15. john brown December 18th, 2006

    Yes, it is a “shitty” solution – but don’t blame the messenger – this is IE’s fault. It’s a “good” hack that opens up fantastic site builds.

    Another downside which is rarely mentioned: there is *NO* way to get alpha transparent PNGs to work in IE using HTTPS (including this hack).

    We’ve also noticed some inconsistencies with color using this method as well.

    Good hack – but test often and KNOW THE LIMITATIONS.

    Reply
  16. Micheal D December 18th, 2006

    Well Lazy, I’m not quite agree with your statement. It alwz been a web designer’s dream to have a image format that truly supports tranparency (something better than GIF). no point doing graphics that only cater on the specific background right? Don’t you use gif transparency?

    Reply
  17. Jim Geurts December 18th, 2006

    There are a couple hacks in javascript to automatically perform this hack, so that you don’t need to manually maintain css files. For example, have a look at the IE7 javascript hack (http://dean.edwards.name/IE7/). It includes many other “fixes” to fix IE’s shitty engine.

    Reply
  18. Ryan December 18th, 2006

    Lazy11: This isn’t a senseless hack, it’s what you have to do to get a PNG transparency to work. I didn’t write IE, but I did have to figure this stuff out to make IE work correctly.

    And I suppose if we are supposed to do a better job with designing, that means we can never use transparency again?

    IE7 renders alpha channels correctly, so there is only a year or so before most users will have gotten past IE6, and we can look back on this and laugh.

    Until we get to that point, go ahead and shun clever tricks to design a page the way you want, not the way Microsoft wants.

    Reply
  19. Christian Trejo December 18th, 2006

    Thanks i was needing this, very nice work.

    Reply
  20. Pablo Iranzo Gómez December 18th, 2006

    At my site, I’ve used the following pngfix.js downloadable from http://alufis35.uv.es/websvn/filedetails.php?repname=spip&path=%2Fmollio%2Fbranches%2F1.9%2Fjs%2Fpngfix.js&rev=0&sc=0

    Regards
    Pablo

    Reply
  21. Chef Groovy December 18th, 2006

    How many people still use IE5? Everyone won’t be using IE7 in year, or in 4 years.

    cool hack btw

    Reply
  22. Slav Erik December 18th, 2006

    I’ve seen this hack before on alistapart, and I’d love to use it but the limitations put me off. Mostly the fact that it wont validate. I like to write accessible semantic xhtml and if that means a little less bling then so be it. After all it may look pretty but what does it add to the value of the page? Not enough to justify the hack IMHO.

    Reply
  23. Cyrus December 18th, 2006

    At least a possibility, if needed, …

    Reply
  24. reddog December 18th, 2006

    the easiest way to do this is to simply load pngfix.js by http://dean.edwards.name
    also, do not make it “if gte IE 5″…it is better to make it “if lte IE 6.9″, that way IE 6 and below will use it but now IE7, which does png transparency just fine.

    One note though, and something to try with yours, make an image a link; then see what part of the image is left. I made some style image links for menu. Only the actual image was clickable…the part that was transparent got totally ripped out. Food for thought.

    While I commend your effort, a simple link/attachment in the head of your html to the pngfix.js file cures all the headaches.

    Reply
  25. Crazy TV Online December 19th, 2006

    Great effect but if you use multiple pngs, it means multiple divs and lots more css..dang.

    Reply
  26. Roy Tomeij December 19th, 2006

    Also don’t forget that it looks like a background, but in fact it is an ActiveX object. The problem with this is that you can’t use background positioning. I ran into this trying to use a PNG using this technique and have it align to a DIV’s bottom (for a drop shadow). I could have added an extra DIV, but didn’t since I believe in semantics.

    Reply
  27. carey December 20th, 2006

    this is awesome…

    Reply
  28. su27 December 23rd, 2006

    pretty good method for the stupid IE.

    Reply
  29. Awb January 2nd, 2007

    Doesn`t work for me. I get no image. Moreover, I get no more image even in Mozzila and Opera. The code is probably interfering with something… My properties are defined into a separate .css file. How do you integrate that piece of code into the .css file? Bloddy I.E….

    Reply
  30. hongkiat January 2nd, 2007

    perhaps you can send me your codes and I’ll take a look for you

    Reply
  31. Keith D Commiskey January 9th, 2007

    Not sure why folks would criticize something that simply provides an additional possible approach for something that may help even just a couple other folks. If it doesn’t help you, fine… but from what I’ve read, it does help quite a few others.

    I probably won’t use it, but I like the thoughts and information provided, the positive follow-ups, and the time you took to share your findings.

    Negative comments should never discourage innovation…

    Reply
  32. deef January 10th, 2007

    A long and interesting read. I have just finished a site for a local rock band and wanted the eye-candy that I think should go with that type of site. Png’s offer the creative flexibility I want. so any help to get around sodding IE is to be applauded. I’m glad that the majority of comments have been positive even if those commenting may not use this particular fix. Respect to anyone who offers solutions to problems. It’s how things move forward. No?. Personally I’m glad I found this site. Cheers.

    Reply
  33. techconet February 3rd, 2007

    Thanks for your tips….

    Reply
  34. michael February 15th, 2007

    is there a way to get this to work from an inported css in a conditional comment? – I can get it to work inline but not via inport

    Reply
  35. michael February 15th, 2007

    ** import that is

    Reply
  36. zacksmith March 31st, 2007

    thanks for explaining this! good stuff!

    Reply
  37. Amr Shimi April 21st, 2007

    At my site, I’ve used the following pngfix.js downloadable from http://alufis35.uv.es/websvn/filedetails.php?repname=spip&path=%2Fmollio%2Fbranches%2F1.9%2Fjs%2Fpngfix.js&rev=0&sc=0

    Regards
    Pablo
    ===================================================
    Thats really brilliant ;) well done,
    now i dont have to put the image in div and add a style.

    Regards,
    Amr

    Reply
  38. Jan April 24th, 2007

    This works in IE but sadly, nothing shows up in Firefox :(

    Reply
  39. godote April 29th, 2007

    i use php script. nohack

    Reply
  40. Hairgel_Addict May 19th, 2007

    first of all, thanks a lot for the solution, i’ve been looking for it for some time :)

    second, there’s a possibility to implement this without conditional comments, since your method is considered as a “hack” why not make it simplerwith the use of another one using just CSS? :)

    The following “hack” works in Firefox, Opera, Konqueror & Safari:

    *|html .classname { }

    and the best thing about this “hack” is that any version of IE doesn’t “see” it. So you can have:

    div.flower {
    background:none;
    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=’flower.png’ ,sizingMethod=’crop’);
    }

    *|html div.flower {background:url(flower-transparent.png) no-repeat; height:100px; width:100px}

    without cluttering up HEAD section :)

    oh, and btw wordpress doesn’t play well with apostrophes… you might want to insert code section inside <code> tags, so that when people copy/paste it they won’t have any problems. sorry, if that was mentioned somewhere above in comments, didn’t read ALL of them :)

    Reply
  41. hongkiat May 19th, 2007

    That’s good, thanks for the tips :-)

    Reply
  42. Amitav Roy June 9th, 2007

    hey man thanks so much for the nice tip. i can use it in many places.

    Reply
  43. Jackson Capper June 20th, 2007

    This is a fantastic solution, and have been using it for a while now with really good results. The hack is definately well worth it, and I don’t see why people have to post useless negative comments when it’s obviously helping people.

    Reply
  44. I hate IE June 22nd, 2007

    The image appears blank in firefox or safari on windows or a mac. I even tried your example.

    Reply
  45. Eric July 23rd, 2007

    Ryan,

    that was a great fix. i am using a conditional statement to pull a specific div for IE that will allow for png backgrounds. I then relatively positioned another div inside and the form works.

    thanks

    Reply
  46. Feli November 28th, 2007

    Does the PNG repeatable in IE?

    Reply
  47. steve January 3rd, 2008

    another way to do this is to add a background color to the PNG file. IE6 and lower will display the background colors; other browsers will obey your transparency instructions. A utility called “TweakPNG” can do this: http://www.mywebsite.force9.co.uk/png/edit-png.htm

    Adding a background color using this utility is NOT the same as adding a layer in Photoshop, matching the colors and flattening layers. You are actually editing the PNG files specifications.

    I realize this won’t work where your background isn’t simple but this solution doesn’t require any coding at all.

    Reply
  48. Psycho Ape January 29th, 2008

    yea this doesn’t work in IE5 or IE6, just checked it’s not working for me.

    Reply
  49. Darcy February 20th, 2008

    The working example doesn’t work because the image is linked as “flower.png” in one place and “flower-transparent.png” in another. With that changed, it works properly in FF and IE6.

    Reply
  50. Panakaj February 25th, 2008

    yes in sample code, name of the image need to change. But problem is that in IE6 I’m getting a security warning.

    Reply
  51. diego nunes April 17th, 2008

    . . It’s always good to notice that sometimes this technique won’t work unless you explicit specify width and height for the container element.
    . . Altought you did it in the article, you didn’t stressed out this need and most people won’t do that (’cause most of time it’s not a desirable behavior).

    . . Amplexos.

    Reply
  52. Honza Odvárko May 4th, 2008

    Good work,

    just one thing to make it perfect – rename flower-transparent.png to flower.png since CSS in transparency.html is written for this image. In article itself there are also mentioned two different filenames, but I suppose that only one image needed.

    Reply
  53. roxzen June 3rd, 2008

    I don’t get it, why use flower-transparent.png when there’s only one image?

    Reply
  54. shahin June 3rd, 2008

    Thanks ! Can any one tell me how to fix background : url(image.png) problem with css. Please email me shahinbdboy@gmail.com. Glad to get email with solution.

    Reply
  55. Abhishek Dilliwal June 8th, 2008

    Thanks for the tutorial I was looking for this only… btw IE is worst browser i have ever seen

    Reply
  56. slam June 24th, 2008

    i’d tried but fail in IE

    Reply
  57. zangra June 27th, 2008

    Thanks a lot. It works like a charm!

    Reply
  58. BrainPain June 27th, 2008

    I´m using another way. I save this pics as 8 bit png with alpha-transparency. 256 colors are almost enough for buttons, badges and so on.

    Reply
  59. Zubair Ahmed July 29th, 2008

    I was facing this problem since a last few months, thanks for your article, it really helped me a lot. I just did some few tweaks in code so that it works well in IE6, IE7, FF and Safari :)

    Reply
  60. adi August 17th, 2008

    Don’t use external css file, it won’t work.

    Reply
  61. fragge August 20th, 2008

    Cheers for this, ugly as anything hack but at least I can workaround and actually make a decent layout without getting grilled by clients for sites not working correctly in IE6. Soon it will die like IE5… we can only hope ^_^

    Reply
  62. nitin October 1st, 2008

    kam kaj chad ka ghara baijo kanjaro 1 kam ta tuhada kolo hunda nahi hai bada vada vada akhra vich likhya hoya hai ki png transpareny in ie……

    Reply
  63. Gabe October 24th, 2008

    Pablo & Amir, I like the pngfix.js implementation. But it doesn’t work for me. Do you know if this supercedes any styling in Joomlaworks’ Frontpage Slideshow? I use this slideshow in a WPRemix theme. The transparency is within the slideshow which is styled by template_css.php located outside the theme. Any hints or specifics? Appreciate any help — from anyone in this list.

    Thanks,
    Gabe

    Reply
  64. Raffi November 19th, 2008

    Hi Ryan, I was hoping you had a working example of your solution to the IE6 png problem that I could look at. If you do please email me a link. Thank you very much.

    -Raffi K.
    raffik@hotmail.com

    Reply
  65. Shayne November 19th, 2008

    Hi Guys

    I’ve tried following the methods shown but none of them seem to be working for me, all that happens is the whole image dissappears as appossed to the block dissappearing.

    Can any one help?

    I have several pages with the same images on them, they are all linked to a stlye sheet

    Reply
  66. Lenny December 13th, 2008

    Re: Best Resource for PNG Issues On the Net

    Might I add that if anyone is making a website in the Uk, Ireland and im sure most other European countries & possibly USA, Many Public Sector Establishment’s like Social Secvices, Hospital’s, Police, Civil Servent Offices, County Council’s, Public Sector.

    Are all using IE6, Its Security Policy.

    idiots

    Reply
  67. softvel February 21st, 2009

    Hola..
    vi el pequeño tutorial, me baje el codigo , lo ejecuto, sin embargo ami me ocurre todo lo contrario, en IE lo veo perfecto, pero en Firefox no sale la imagen de transparencia, por que sale eso?

    espero la respuesta a mi correo, gracias.
    Atte: softvel

    Reply
  68. Kia May 10th, 2009

    Lenny –

    Firefox is FAR more secure than any version of Internet Explorer.

    That it’s a “security” policy to use IE6 for any government agency is…sad. Very sad. Apparently some governments must like to use a browser with poor coding and as many security holes as swiss cheese.

    Reply
  69. Rahul June 8th, 2009

    OMG this was as simple as that. And I’d been look all over. thanks hongkiat.

    Reply
  70. ram June 12th, 2009

    it is not working when you deploy the file in Jboss server

    Reply

Trackbacks

  1. Daniel Fischer - Got Fisch? » Blog Archive » How to get PNG image transparency working in Internet Explorer
  2. Making PNG image transparency work in Internet Explorer · Style Grind
  3. Prime News Blog » Blog Archive » hentay xmen How to get PNG image transparency working in Internet Explorer
  4. Daryn’s Blog! » How to make PNG transparency work in Internet Explorer
  5. Make PNG transparency work in Internet Explorer at What’s Innate?
  6. Łukasz Schabowski // Weblog » Blog Archive » Internet Explorer - przezroczystość PNG
  7. Cartoons Fans Lounge
  8. The Web Design Blog
  9. links for 2006-12-19 at found_drama
  10. Sfruttare la trasparenza delle png su iexplorer! » 4MJ
  11. 陈谦的blog
  12. 煎蛋 » How to get PNG image transparency working in Internet Explorer
  13. 301志{从这里出发,去梦想的草原} » Blog Archive » 如何让PNG图片在网页里变透明?
  14. 鬼仔’s Blog » links for 2006-12-23
  15. shirazlabs.com Blog » I.E png image fix.
  16. Easy Webbers » Speedlinking Week 1 (2007)
  17. MyLiveSignature: Bring Analogue back to Digital Signature
  18. Make PNG transparency work in Internet Explorer
  19. anime fetish
  20. uniform sex series
  21. چه كنيم كه فايلهاي PNG در Internet Explorer هم پس زمينه شفاف داشته باشند « باور
  22. eleven3 » Blog Archive » building with layers and pngs
  23. 如何让PNG格式的透明图片在IE6下正常显示 | 帕兰卓一得
  24. Blog Design Review - Webmaster Malaysia Forum
  25. PNG Alpha Transparent Problem in IE<7 « Welcome To My BLOG
  26. internet explorer browser life make web good
  27. How to Make an Impressive Animated Landscape Header with jQuery | Build Internet!

Leave a reply