A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.
It's fast and free.
processing...Retrieve Sign In
Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.
processing...Welcome to Webmonkey
- edit articles
- add to the code library
- design and write a tutorial
- comment on any Webmonkey article
Sign In Information Sent
Use Transparent PNGs in IE6
/skill level/
/viewed/
Contents |
Introduction
You don't need fancy javascript libraries to make a transparent PNG file work correctly in Internet Explorer 6. All you need is to follow a few simple rules in your CSS.
What you'll need
Just your favorite text editor!
Code and Explanation
First of all, put the regular transparent PNG image in your HTML:
<div id="png-holder"><img src="/images/transparent.png" alt="" /></div>
That will work perfectly in Firefox, Safari, and IE7. To make it work in IE6, add the following to your IE6 stylesheet:
#png-holder {
width: 300px;
height: 150px;
text-indent: -9999px;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/transparent.png', sizingMethod='crop');
}
That filter rule is a proprietary Microsoft code that will force IE6 to render the alpha transparency in the PNG - but it does it by drawing another box on top of everything on the page and sticking the png in there, which leads to some odd behavior. Here's the breakdown:
- You must use the filter on a containing element for the PNG, not the image itself. For this reason, it's easiest to wrap the image in a
DIVorPtag. - You must declare a width and height for the container matching the width and height of the image.
- You have to hide the original image - hence the
text-indent: -9999px;rule. You can do the same thing to a transparent PNG that you've set as a background on a div by usingbackground: none;instead. - You must use an absolute path to the image. As far as I can tell, this is just a restriction of the filter.
- You can set
sizingMethodtocrop, which means if the image is larger than the dimensions of your container, the overflow will be hidden. Alternately, you can set it toscale, which will stretch the image to fit. - If you absolutely position the PNG over other content on your page, the user will not be able to click on any links under the image. That's because the filter is drawing a new box on top of everything, completely overriding your z-index rules.
Suggested readings
- Microsoft support article on PNG transparency in IE6
- Justin Koivisto's article on the filter: code
- 24 Ways article on PNG transparency in IE6
/related_articles/
Special Offer For Webmonkey Users
WIRED magazine:
The first word on how technology is changing our world.
