Reinspire

You are viewing an archived page from the 1st version of my site, but I've started over. You can find the latest content and design at www.reinspire.net, or read all about the relaunch here.

Using Transparent PNGs in Internet Explorer

Up until a couple of weeks ago, I thought that there was no way to use transparent or semi-transparent PNG images in IE. I was wrong. My only excuse is a bad one too: I read it somewhere.

The truth is that there is a pretty good way to use transparent PNG images with CSS. It’s not without its drawbacks (I’ll get into those later) and it doesn’t use valid CSS, but while we wait for PNGs to be fully supported by all major browsers, it seems to me that it’s a fairly good solution.

It’s my opinion that the benefits of using alpha PNG images far outweigh the drawbacks of your CSS not validating, so without further ado, here’s how it’s done:

#example_image {
     filter: progid:DXImageTransform.Microsoft.AlphaImageLoader (src='your_image.png',sizingMethod='scale');
}

html>body #eample_image {
    background: url(your_image.png);
}

The trick is to use CSS’s filter attribute to call a proprietary Microsoft component that will handle the PNG image. Click here to see an example.

The basic technique is fairly simple, but there are a few things that need to be expanded upon. First of all, I’ve found that the src property doesn’t handle relative paths very well, so unless the image is in the same directory as the CSS that’s calling the AlphaImageLoader (or a subdirectory of that current folder), I would suggest using the absolute paths (or even the full URI) to reference the image file.

The second property in the function call is the sizingMethod, which takes 3 different values. Scale, which causes the image to scale to the dimensions of the object that’s using it, Crop, which clips the image to fit the dimensions of the object and Image, which causes the element’s borders to expand or contract to fit the actual dimensions of the image. The default value is the image value.

For more information on the AlphaImageLoader, see Microsoft’s documentation.

You’ll also notice that I didn’t set the background image in the same section as I’m calling the filter attribute. The reason for this is that IE still understands the background image attribute and would load the PNG image without the alpha channel support. So, I’ve used a child selector to set the background for all browsers that understand that syntax.

What about IE 7?

Internet Explorer 7 will support PNG images with the alpha channel, but as far as I know it’ll still handle the filter component as well - so what to do? Well, it’s considered a best practice to separate all IE specific hacks (or any browser specific tweaks for that matter) into a separate stylesheet anyways, so we’ll insert a conditional comment that will detect (without JavaScript) IE versions 6 and under.

<!--[if lte IE 6]>
<link rel="stylesheet" type="text/css" href="ie_style.css" />
<![endif]-->

If you’re familiar with programming decision structures, you’ll notice that it’s using an IF/EndIf statement. The ‘lte’ stands for ‘Less Than or Equal’ to IE 6, and will use the code contained in the If statement for any version of IE less than or equal to IE 6. Incidentally, IE5.5 also supports this PNG filter method.

There are drawbacks?

Sadly, yes. As with any proprietary way of doing things, this PNG filter method also has some drawbacks. I mentioned before that the CSS for this method is not valid, but that’s not really a big deal if you’re only serving it to IE 6 and under. The main drawback of this method is that if you want to have an anchor tag (hyperlink) on top of your PNG image, you’re sort of out of luck. The best way to get around this is to just set the PNG image as background for the anchor itself. It’s obviously not ideal, but it’s also not that big of a drawback because there are ways around it. It just may take a little more creativity when you’re slicing your images.

Check out this example that shows the differences between using an anchor tag on top of a PNG and an anchor tag where the PNG is set on the anchor tag itself. (Be sure to view the page in both Firefox and IE to see the differences. Make sure to actually click on the links and also make sure to take a look at the source code).

One final note

Just one final note: when using the PNG filter method on an anchor tag, in IE you’ll need to specify the CSS value for the cursor (the mouse pointer), otherwise, all you’ll get in IE is the default pointer (usually an arrow, unless you’ve got a custom icon set). Here’s an example:

#example_link a {
    filter: progid:DXImageTransform.Microsoft.AlphaImageLoader (src='your_image.png',sizingMethod='scale');
    cursor: pointer;
}

Putting it all together

Because this little ‘tutorial’ has been put together in bits and pieces with a lot of explanation, I thought I’d post all the code together in one spot to tie it all together.

XHTML:

<html>
<head>
<title>Your Title</title>
<link rel="stylesheet" type="text/css" href="main_style.css" />
<!--[if lte IE 6]>
<link rel="stylesheet" type="text/css" href="ie_style.css" />
<![endif]-->
</head>
<body>
<div id="Example">
    <a href="#"></a>
</div>
</body>
</html>

CSS - main_style.css:

#Example {
     width: 200px;
     height: 200px;
}

#Example a {
    display: block;
    width: 200px;
    height: 200px;
}

html>body #Example a {
    background: url(your_png_image.png) no-repeat;
}

CSS - ie_style.css:

#Example a {
     filter: progid:DXImageTransform.Microsoft.AlphaImageLoader (src='your_png_image.png',sizingMethod='scale');
     cursor: pointer;
}

So there you have it! There is a way after all to use transparent PNG images in IE. Feel free to post any questions or comments below.

Update: Jeff Croft has written a great article over at Digital Web Magazine that does a great job of outlining a lot of practical uses for using transparent PNG images. Be sure to check it out.

Posted in: Code, CSS, CSS Tips, Web Development

Comments

Globally Recognized Avatar1. Elliot Jay Stocks - September 29, 2006, 6:33 AM

Hey Jonathan,

Thanks for such an informative article. I have some good news: there’s a fix to get anchor tags working over the top of a PNG background.

In your CSS file, simply give all of your anchors relative positioning:

* a { position:relative }

I can’t take credit for this - I found it while researching Justin Koivisto’s PHP method for getting PNGs to work in IE, but he mentioned the ‘relative’ fix there (suggested by Holger RĂ¼prich) and I thought I’d combine it with yours… and it worked!

Best,

~ Elliot

Globally Recognized Avatar2. Jonathan Eckmier - September 29, 2006, 11:48 AM

Elliot: You’re welcome, and thank you for adding to it by posting your comment. This is something I hadn’t heard of yet, so thanks for making me aware of it. It’s always appreciated when others add their knowledge to the discussion.

Globally Recognized Avatar3. Timothy - December 20, 2006, 6:32 PM

Hi Jonathan,

Thanks for the helpful article!

I noticed that you stated that the AlphaImageLoader doesn’t handle relative paths very well, and I thought this too at first. However it turns out that the AlphaImageLoader uses the path of the html doc that calls it, not the path of the CSS document, as the base path.

So if your html document is in the base folder, your stylesheet is in a folder called css, and your image is in folder called img, the relative path to the image (as far as AlphaImageLoader is concerned) is ‘img/image.png’ and not ‘../img/image.png’.

Tim

Globally Recognized Avatar4. Mingyi - January 18, 2007, 11:07 PM

hi guys, not sure if you can help me with this. I used transparant PNG images in www.vivie.com.sg. Is there anyway to force user to click on the entire png area rather than the opaque text itself?

Globally Recognized Avatar5. Jonathan Eckmier - January 19, 2007, 12:36 AM

Hey Mingyi, this is one drawback to the IE PNG filter. Areas of the image that are 100% transparent for some reason aren’t clickable when used in a hyperlink.

The way I usually get around this is to add a full white layer behind the image and set it’s opacity to 1%. I guess IE just needs “something” to click on and I’ve always used a white semi-transparent background layer to get it to work. Don’t worry, you can’t ever notice the 1% opaque white on top your background, so that won’t be an issue.

Let me know how it goes and if you need some advice on how to accomplish this.

Globally Recognized Avatar6. endryou - February 7, 2007, 10:15 AM

Great article!

I didn’t want to use child selector so I added:

background: none;

before:

filter: progid:DXImageTransform.Microsoft.AlphaImageLoader()

All together:
- CSS for Internet Explorer (all versions lower than 7)


<head>
<style type="text/css">@import "my_css.css";</style>

<!--[if lt IE 7]>
<style type="text/css">
#png_image {
background: none;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader (src='../res/images/stand.png',sizingMethod='scale');
}
</style>
<![endif]-->

</head>

- CSS for rest (Firefox, Opera, etc.) - file my_css.css


#png_image {
position: absolute;
top: 167px;
right: 274px;
width: 164px;
height: 224px;
background: url(../res/images/stand.png) bottom right;
}

As you probably noticed relative path is also working for me :)
I hope it helps someone.

Globally Recognized Avatar7. Jeroen Haan - February 13, 2007, 5:05 PM

http://www.haan.net/test/png_fix.php

see source code

It is an all in once fix.

I did not exclude IE 7 yet.

Globally Recognized Avatar8. Dave Searles - February 14, 2007, 3:33 PM

Are you aware of this non-intrusive way to get PNG transparency using the CSS “behavior” extension of IE? It works great and requires MUCH less modification.

http://www.twinhelix.com/css/iepngfix/

Jonathan’s tip for clickability is still necessary, however…

Globally Recognized Avatar9. Jugular Bean - May 28, 2007, 4:28 PM

I can’t seem to figure out how to use a png as a repeating background for a div. Scale somehow doesn’t seem to be making it scale.

Here’s my code for ie (using conditional comments to load a stylesheet)

#page {
background-image: none;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/containerback.png',sizingMethod='scale');
height: 600px;
}

and for other browsers

#page {
background: url(images/containerback.png) repeat-y;
float: left;
width: 100%;
}

Globally Recognized Avatar10. Jonathan Eckmier - May 28, 2007, 10:40 PM

Jugular Bean: According to Microsoft’s documentation of the sizingMethod property, using the scale attribute “stretches or shrinks the image to fill the borders of the object.” As far as I’m aware, it’s not possible to really achieve the same sort of vertical (or horizontal) repeat of an image using the PNG Filter Method. The only thing you’re really able to do is stretch that background image to the same size as the element that’s using it (#page), but it will be both a vertical and horizontal stretch (which would be fine as long as “#page” was the same width as the PNG image, but in your example you have a width of 100%).

If the image you’re using should look fine when stretched vertically and the #page width of 100% is actually the same width as your image (ie, #page is contained inside an element with a width the same as your PNG image), then you should be OK to use the scale property, but if the image doesn’t look good all stretched out, you’re probably best to serve up a separate background image just for IE 6 users. I know that’s not the ideal solution, but when it comes to transparent PNG images (at least with CSS), IE 6 does have its limitations.

Hopefully this helps, but please let me know if you have any other questions or thoughts.

« March 2006 »

Sun Mon Tue Wed Thu Fri Sat
   1234
567891011
12131415161718
19202122232425
262728293031 

Search this site

Latest Entries

Hot Topics