Productivedreams.com ProductiveDreams - Everything that web designers need, owned by Gopal Raju

A blog to keep you updated on the latest design trends.
RSS

IE CSS hack without using any hacks!

Filed Under

A few days back while working for a MAJOR client (sorry, can’t mention their name), I discovered something new (I hope it’s new ) – A pure CSS based image replacement solution for IE. This method is helpful especially to solve the PNG issue in IE6. I know there are many CSS hacks available out there to solve the PNG transparency issue, but this method doesn’t need any hacks!! Interesting isn’t it?

Step-1: Let’s review the HTML part first.

HTML
01
02
<html>
03
 
04
<head>
05
<title>ProductiveDreams</title>
06
<link href=”style.css” rel=”stylesheet” type=”text/css”/>
07
</head>
08
 
09
<body>
10
<div class=”bg-image”></div>
11
</body>
12
 
13
</html>
14
The following are the images that I've used for this tutorial. The image without saturation is a .gif and the colored one is a .png. You may right click and save the image.

PNG Image

Colorful PNG Image



Desaturated GIF Image

Desaturated GIF Image


Step-2: The stylesheet

I included the following code in the stylesheet
CSS
01
body{
02
background:#000;
03
text-align:center;
04
}
05
 
06
div.bg-image{
07
background:transparent url(images/pd.png) no-repeat center !important;
08
width:134px;
09
height:134px;
10
}
Let me walk you through the CSS. The first part simply applies a background color to the body and center aligns the div element. In the second part, I've specified the width and height of the div (134px) based on the image dimensions and have given the .png as the background image. Everything works fine?

Step-3: Solution for IE

This the most interesting part that does the magic.

I added another line of CSS code just below
background:transparent url(images/pd.png) no-repeat center !important;
and the following is the code
background-image:url(images/pd.gif);

Check out the result in IE! Awesome.... isn't it? In all other browsers except IE you will see the colorful png image.

Consolidating the entire thing

So. the final css would look like:
CSS
01
body{
02
background:#000;
03
text-align:center;
04
}
05
 
06
div.bg-image{
07
background:transparent url(images/pd.png) no-repeat center !important;
08
background-image:url(images/pd.gif);
09
width:134px;
10
height:134px;
11
margin:0px auto;
12
}

For your ease, I have attached the sample files as a zip. Feel free to download it :) and do let me know your feedbacks.

Download CSS Solution for image replacement

23 Comments

Leave Your Reply

  1. DesignFellow says: I hate IE6…
    but what to do. Still it is alive.. :(
    Thanks for the info dude :)
  2. pradeep says: bhai,

    this is the same method which we using as min-height hack. IE will negate the !important line. But nice post.
  3. sunil says: still we need to support the crap ie6?
  4. Praveen Vijayan says: Title is bit confusing. We have to use lot of ‘!important’ especially if site have plenty of png images. Personally I’m bit against using lot of ‘!important’ in css. But its cool tip and new addition to my css knowledge base. Thanks for sharing your invention :)
  5. DevLano says: very quick and clean fix if you ask me. I’ve seen this done with much more lengthy attempts, but this is a very native use for css. how css should work in some respect. great find. :-þ
  6. admin says: @Mariusz: Thanks for your comment :) This is just an image replacement technique, without using CSS hacks.
  7. Mariusz says: Unfortunately, that doesn’t fix the PNG transparency issue. Anyway, thanks for posting
  8. admin says: @Steven: Thanks for your comment. I agree with your suggestion on adding conditional comments for IE. But in that case you would need a separate css for IE in which you can use hacks as well. In my opinion, !important can be used as long as it doesn’t mess up with the W3C validator.

    Thanks again, Steven :)
  9. Steven Clark says: By the way, awesomely beautiful website you have Gopal.
  10. Steven Clark says: !important would not be an ideal solution IMO. But you could easily use an IE conditional comment to achieve this respectably, or provide more advanced selectors that IE doesn’t understand to deliver the PNG.

    !important is a very old school way of achieving this.
  11. Nacho says: It´s the same thing than using a star hack for the background image.
  12. okra says: its nor working properly… :( but nice try…
  13. Remiz says: Misleading title !
    Why do we need to care ie6 now?
    Only we (developers) can make IE6 out of the world. Ask more pay from client if they want to see their site in IE.
  14. Mike says: And what about big and high quality png’s? Transforming them to *.gif would mean image information loss. Also, if you do have many images, than with your method their count doubles.
    Anyway, this trick is nice for some not big images.
    But the truth remains the same: Someone, kill IE! Please.
  15. admin says: Will, thanks for your comment :) I guess you got confused with the term “PNG transparency issue”. You would have to use a gif for IE6 to replace the png image. That’s why I’ve used a png and a gif in this article… Please do let me know if you have any questions.
  16. Will Ayers says: I might be reading this article wrong, but I don’t believe this is not a fix for transparency but just away to have ie use a different background image instead of the png. For example creating a png version for everyone and gif version for ie6.

    Here is a test I did with your example above. http://iarewill.com/projects/ie/

    If I did something wrong or I am completely off then please let me know. Awesome site by the way, I really like the design.
  17. Navdeep says: That’s pretty amazing. So this is going to be W3C and better than defining under f***ie.css :)