< Browse > Home / Browser Issues, CSS, Class Notes, PHP/MySQL & JavaScript / Blog article: Transparent PNG 24 Images with Internet Explorer 6 for Beginners

| Mobile | RSS

Transparent PNG 24 Images with Internet Explorer 6 for Beginners

December 23rd, 2009 | No Comments | Posted in Browser Issues, CSS, Class Notes, PHP/MySQL & JavaScript

PNG 24 display problem

A common question in class is “how do I overlap images and still see the background?” The answer, you use some type of transparent image. Traditionally we used a transparent GIF, but the quality isn’t so great. PNG has really become the preferred image format for transparency. PNG 8 gives you a transparent image, but you may see some white pixels around the edges of the image. PNG 24 transparent images are crisp and clean, but sometimes a bit bloated. Still PNG 24 now being widely used.

Unfortunately, it is well documented that transparent PNG 24 images will not display properly in Internet Explorer 6 without applying some type of fix. Instead of getting a truly transparent image you will see a blue background around the image like the image above. I’m not going to go into the technical reasoning for this problem, this is an article for beginners and there are numerous fixes and articles for this issue, Perishable Press has a great write up on the subject.

I’ve prepared a page that shows both of these problems; this page was created from the Six Revisions’ tutorials How to Create a Grunge Web Design Using Photoshop and How to Code a Grunge Web Design from Scratch – great tutorials, but I’ll write about that later. If you open my PNG transparency example page in Internet Explorer 6 you should see that the header image “Get Your Hands Dirty” has a blue background – not good. If you look at the heading “Featured Work”, a PNG 8 image, you should notice, albeit subtle, little white dots around the edges of the image – not good either. If you look at the heading “About this Website”, a transparent GIF, you will also see the little white dots around the edges of the image.

Now if you look at my example with PNG 24 images and a JavaScript fix applied in Internet Explorer 6 you will see that all of the images look great.

So how did I fix this? If you read the article PNG Fix for Internet Explorer from Perishable Press you will see that there is a general method for fixing PNG 24 images in IE 6. You’re going to need to create an IE specific CSS file, this file is linked in the head section of your HTML document after your regular CSS link within conditional comments.

<link href="styles.css" rel="stylesheet" type="text/css" />

<!--[if lt IE 7]> 

<link rel="stylesheet" type="text/css" href="ie-specific.css" /> 

< ![endif]-->

The conditional comment starts with

<!--[if lt IE 7]>

This tells the browser that if the visitor is using a browser version older than IE 7 then use this special CSS file in addition to the styles.css file. It doesn’t replace the styles.css file, it just allows you to send specific CSS rules to IE 6.

In the ie-specific.css file you will need one declaration for the transparency fix.


.png { behavior: url(png.htc); }

You created a class called png. Behavior is a CSS property that allows you to attach a script to an element, class, or id. The png.htc file contains a JavaScript that will fix the PNG 24 display issues in IE 6 and earlier versions. You can learn more about this property and htc files in the SitePoint Reference.

You also need a special JavaScript file, the htc file. You will find this file all over the Internet, but here is a zip of the png.htc file that I used. You will also need a transparent GIF file, this is a 1px x 1px transparent GIF that the HTC file uses.

To make this fix easier to use, the files ie-specific.css, png.htc, and transparent.gif should be placed in the same folder. If you place them in different folders you will need to edit the paths in the CSS file and the HTC file. This isn’t really a big deal, but sometimes it can cause problems for the beginner.

Ok, you have got your CSS file created and linked using conditional comments, your HTC file and transparent GIF files are in the same folder as your CSS and you preview your page in IE 6 and it still looks like crap, oh I mean bad. We’ve got a little more editing to do – remember that class we created in the ie-specific.css file? Ok, in case you forgot, this one


.png { behavior: url(png.htc); }

You now have to apply this class in your HTML file whenever you use a PNG 24 image. For example the “Get Your Hands Dirty” PNG 24 logo was applied as a background image in the main styles.css file with this code

h1 a {
display: block;
background: url(images/title.png) no-repeat left top;
float: left;
height: 92px;
width: 426px;
}

so the png class from the ie-specific.css file needed to be added to the HTML

<h1><a href="#" class="png">Get Your Hands Dirty</a></h1>

Here is another example of how it was added to the featured work image

<h2 class="featured-work png">Featured Work</h2>

In the above example two classes have been applied – featured-work is from the main styles.css file and png is from the ie-specific.css file.

The ie-specific.css file can also contain other declarations that are for Internet Explorer 6 only; you are not limited to using this file just for fixing transparent PNG files. Anytime you think about putting * html in your regular CSS file to fix an IE 6 issue you can put the declaration in the ie-specific.css file instead.

Again, there are numerous fixes for this issue and this may not be the perfect fix for you. Check out the great article at Perishable Press on the subject or remember that Google is your friend.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • StumbleUpon
  • Design Float
  • Reddit
  • Twitter
Leave a Reply 858 views, 6 so far today |

Comments are closed.

BubbleRelaxPlasmaBubbleDripPlaying with Water and LightPlaying with Water and LightPlaying with Water and LightPlaying with Water and LightPlaying with Water and LightwetJust Posing