Proper PNG support even with MSIE
Historic content
Well, whaddya know? Internet Explorer 6 is history, and even Internet Explorer itself has been discontinued. Good riddance! But this page may serve as a reminder of the many reasons why web-developers desperately longed for that moment!The Problem
Every major browser has PNG support, except MSIE for Windows. (The Mac version, in contrast, has full PNG support.) MSIE for Windows only supports 8bit PNG images, i.e. binary transparency like GIF89a. If you are using Internet Explorer version five or six, you will see an ugly grey box around this image, a 24bit PNG image with alpha channel:
The Workaround
The quick-and-dirty solution for this Windows-specific misbehaviour is, of course, a Windows-specific hack. The trick is to put a transparent image of the same size in place of the desired PNG image, and then have a proprietary DirectX filter load the PNG image. Like this:
<img width="200" height="200" src="transparent_pixel.gif" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader( src='krabbetrans.png',sizingMethod='scale')">
The obvious drawback of this method is that all other browsers only show the transparent image placeholder, i.e. nothing. With Internet Explorer 5/6 you should see the same image as above, only this time with the correct background instead of grey:
The Solution: An External CSS File
Luckily, we don't have to worry about dynamically altering the content for Internet Explorer on all pages or some other tedious adaption. We just need one extra CSS rule to fix all
img { behavior: url("ugly_msie_fix.htc"); }
Where ugly_msie_fix.htc
contains a script to dynamically
alter all img
objects it is applied on:
<public:component> <script> var trans_pixel = "transparent_pixel.gif"; var msie5_or_6 = navigator.userAgent.indexOf("MSIE 5.5") != -1 || navigator.userAgent.indexOf("MSIE 6.0") != -1; if (msie5_or_6) { if (element.src.toLowerCase().substr(element.src.length-4) == ".png") { var oldSrc = element.src; element.src = trans_pixel; element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + oldSrc + "',sizingMethod='scale')"; } } </script> </public:component>
Of course, this requires active scripting to be enabled. But hey, we're dealing with Internet Explorer users here!
Conclusion
Although this proprietary hack is quite ugly, the good news is that you can finally start using PNGs with alpha transparency, (potentially) saving lots of webspace and bandwith.
Computer Stuff
- Knob-Out
- The Wonders of Virtual Reality
- Quine Scarf
- Warum PHP Scheiße ist
- Proper PNG support even with MSIE
- Tiny Speech Synth in JavaScript
- „Anführungszeichen“
- IE crash in 11 bytes
- Abschied von MySQL
- More…
Blog
- Sep 2024 – 3
- Aug 2024 – 5
- Jul 2024 – 6
- Jun 2024 – 6
- May 2024 – 7
- Apr 2024 – 8
- Mar 2024 – 4
- Feb 2024 – 9
- Jan 2024 – 10
- Dec 2023 – 8
- Nov 2023 – 1
- Oct 2023 – 5
- Sep 2023 – 5
- Aug 2023 – 8
- Jul 2023 – 1
- Apr 2023 – 1