Drupal 5.1 + PNG + IE6

Sounds like a fun recipe huh? Well, having tried (and failed) previously to get this combination to work properly, I recently did some work for a client which demanded that .png files (Portable Network Graphic - image files which have transparent bits) were both used and looked correct in Internet Explorer 6.

Basically, I wanted to accomplish four main things:

  • I wanted a real .png file with full transparency to look correct in IE6, and behave as it’s supposed to (i.e. letting you see some or all of what’s beneath it).
  • I wanted to achieve this using only CSS – no Javascript, no PHP, no Flash, and no other trickery.
  • I wanted this to work for background .png files, that is files which are called from the style.css file like so:
    #div_1 {
    background-image: url(images/druplicon.png)
    }.
  • I wanted the solution to be as tidy as possible.

The trouble is that if you just try to load a .png into IE6 normally, you get the following outcome:

Firefox and IE6 .png comparison

Ouch!

After some googling around, and a bit of trial and error, I came up with a solution which satisfies all of the above criteria. Here’s what I did:

Step 1

Firstly, I inserted the following code into my page.tpl.php file directly before the closing head tag:

Remember to replace ‘your_theme_name’ with the actual name of your theme folder (e.g. 'garland' or 'pushbutton').

<!--[if IE 6]>
<style type="text/css" media="all">@import "/sites/all/themes/your_theme_name/ie6_fixes.css";</style>
<![endif]-->

If your Drupal install is in a subfolder (e.g. www/garland) you may instead need to use:

<!--[if IE 6]>
<style type="text/css" media="all">@import "/your_theme_name/sites/all/themes/your_theme_name/ie6_fixes.css";</style>
<![endif]-->

(which has an added ‘/your_theme_name’).

This code targets IE6 only, and tells it to load another stylesheet called ‘ie6_fixes.css’. The beauty of the code is that it will be ignored by all other browsers, which means that we can provide IE6 with extra instructions about what to do with our .png files. It also means that we can put our IE6 instructions in a separate file (ie6_fixes.css) and not clutter up our page.tpl.php file.

Step 2

Secondly, then, create the new CSS file. Give it a title of ‘ie6_fixes.css’, and save it to the root of your theme folder – i.e. the same place as your normal style.css file.

Now insert the following into your new ie6_fixes.css file. Again, as with the previous step:

  • replace ‘your_theme_name’ with the actual name of your theme folder, and also
  • replace ‘#div_1’ with your own div id or class.

(Line wraps marked »):

#div_1 {
background-image: none;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader »
(src='/sites/all/themes/your_theme_name/images/a_picture.png', sizingMethod='crop');
}

Again, as before, if your Drupal install is in a subfolder (e.g. www/garland) you may instead need to use:

#div_1 {
background-image: none;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader »
(src='/your_theme_name/sites/all/themes/your_theme_name/images/a_picture.png', sizingMethod='crop');
}

And that’s it! If we now look again at our .png file as viewed in IE6, the result is exactly as it should be.

Firefox and IE6 .png comparison 2

A small caveat:

I haven’t been able to get this method to work with repeating backgrounds yet. An obvious example would be if you wanted a shadow down either side of a centered design. The ideal way would be to cut a small image (maybe 1px high) and then repeat it vertically. My solution was to just create a larger/extended .png file which covers the whole repeating area. Whilst this solution isn’t perfect, it’s certainly not terrible either. Even though we are making a large (in dimensions) file, because it is mostly transparent the actual file size will remain relatively small, and it certainly looks a lot better than an IE6 mess!

18 comments

1
Athanasios LefterisJuly 9th 2007 @ 02:10PM

Thanks for this useful tip! I didn't knew you could enable alpha transparency for pngs in IE6 by just using css..

2
elvJuly 9th 2007 @ 03:18PM

The PNG fix for IE can actually work in IE 5.5 (not 5.0)

I think your best bet for this fix would be to use a script like the one from WebFX (http://webfx.eae.net/dhtml/pngbehavior/pngbehavior.html), or its improved implementation by deanEdwards (http://dean.edwards.name/IE7/notes/#PNG) to automate the process for every transparent PNG.
That way you don't need to target specific divs, and you can keep your style sheet clean.

3
JamesJuly 12th 2007 @ 06:34AM

I keep avoiding .pngs for this reason, but I think I'll give this a try.
Cheers.

4
Joe MoracaJuly 14th 2007 @ 10:42PM

thanks for the tip.

PS you have a really nice looking site !! great theme work

5
TomJuly 21st 2007 @ 05:28AM

You need to embrace the Firefox revolution and this will solve your problems... well, 90% of them. For the remaining 10% you'll still need IE. :)

6
JonathanJuly 30th 2007 @ 07:59PM

Thanks for this. With what little access I have to PCs running ie6, it appears to work.

It was important to me to have a solution that did not involve javascript on the template page.

7
JonathanJuly 30th 2007 @ 09:15PM

Darn. I thought this worked, but I guess the javascript solution I did before was still in the cache. It's really only one image (site logo) I need to apply the filter to. Is there a direct way to do simply that?

8
salvisAugust 1st 2007 @ 01:57PM

But how does Drupal do it? In Garland the Druplicon's background looks transparent (at least you can freely change the colors of the background gradient with the Colors module), and it works perfectly under IE6.

Surprisingly, themes/garland/logo.png has no transparency at all! As far as I can tell, Drupal can do it without resorting to IE-specific hacks, but how?

9
LaurenceAugust 2nd 2007 @ 01:02PM

Hello all,

@ salvis - you kind of answered your own question. The Druplicon which Garland uses is a png but does not, as you said, contain any transparency. Each time you change the Garland color scheme it regenerates all the images necessary for the theme, including the logo. The image for the logo is then just positioned so as to appear like there is transparency, when really there isn't. If, for example, you were to reposition the logo you would see that the logo graphic doesn't line up with the header background gradient behind it.

@ Jonathan - the simplest answer in your case might well be to achieve the effect you need by just cutting the logo image so that it also contains any background, and then position it, like the Garland logo, so that it appears as if the transparency exists when actually it does not.

@Tom - if only a lot of people still didn't use IE6....one day perhaps.

10
Chris HerberteAugust 8th 2007 @ 10:46PM

Saving the PNG as Indexed color will give a transparency which works with IE although anti-alias and color pallet are some things which you'll need to work around. It's not an ideal solution -- quick and dirty. Works with IE 5.x.

11
YouSeptember 9th 2007 @ 10:45PM

For repeating backgrounds I think you need to specify either a width or a height argument.

(height:100%; is normally best as... well... it does not seem to mean anything at all.)

12
tomSeptember 19th 2007 @ 09:04PM

IE6 must die!

13
Spinn3r Web Crawler BoyNovember 18th 2007 @ 04:37AM

Good god do I hate IE 6.0... thanks for this tutorial.

IE 6.0 is the new Netscape btw.

14
palyacoDecember 25th 2007 @ 07:04PM

i agree with tom. IE must die. Exactly we must kill IE.

15
ArtikelverzeichnisJanuary 14th 2008 @ 07:35AM

The ideal way would be to cut a small image (maybe 1px high) and then repeat it vertically. My solution was to just create a larger/extended .png file which covers the whole repeating area.

16
IvantJanuary 30th 2008 @ 07:48AM

How about an src image rather than background? I do hate IE6 but theres no other option to not deal with it. So lets fix it guys...

17
DekorationFebruary 21st 2008 @ 07:09PM

Thanks for this really useful article.Great cheat sheet, I appreciate it very much.

18
TobiFebruary 27th 2008 @ 04:28AM

Nice and interesting article, thank u for the tipp!
btw- nice designed blog :-)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options