Cross-browser semi-transparent backgrounds
posted Dec 23, 2006

Semi-transparent backgrounds are nice. They would be more popular, but Internet Explorer doesn't support .png transparency. There are a few clunky workarounds. Here's another that's a little less clunky.

Update: Here is probably the easiest way. You can stop reading this page now.

Update: The kind folks on css-discuss point out some other methods. I believe most of them use javascript.

Update: An article from A List Apart has more techniques (some of which are downright painful to look at...)

Update: Workaround for link bug in IE 6.0

Update: Now works in IE 5.0 (Thanks MG)

Example:
I like flowers.
They smell nice.
How it works:

The ingredients are:

  • A background image: flowers.jpg
  • A small, semi-transparent .png image: 75p_honey.png
    (The "75p" means 75% opaque)
    These are a snap to create with The GIMP.
  • A small css hack to overcome IE's lack of support for semi-transparent .png images:
    .trans_box2 {
      font-family:verdana;
      font-weight:bold;
      padding:40px;
      margin:30px;
      border:solid 1px #555;
      /* Mozilla ignores crazy MS image filters, so it will skip the following */
        filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='/75p_honey.png');
    }
    /* IE ignores styles with [attributes], so it will skip the following. */
    .trans_box2[class] {
      background-image:url(/75p_honey.png);
    }
    
The html:
<div style="float:left;background-image:url(/flowers.jpg);border:solid 1px #000;padding:10px;">
<div class="trans_box1" style="float:left;">
I like flowers.
</div>
<div class="trans_box1" style="float:left;">
They smell nice.
</div>
<div class="trans_box2" style="float:left;">
<a href="http://www.google.com/search?q=flowers+bees+honey"&gt;When combined with bees, they make honey...</a>
</div>

More information Addendum: IE Link bug

Thanks to nitehood for pointing out a bug/problem in IE. Using alphaimagefilter on a div makes links inside the div quit working (they appear as links, but don't respond to mouse clicks or hovering).

Upon further research, this has been noticed before, here and here.

This points toward a possible cause. Apparently, the dimensions of the background image affects IE's display of links. That's a pretty ugly bug.

I tried various widths and heights for the background .png image. The results are in the table below (Y="links work in IE 6.0" N="links don't work in IE 6.0")


width
height
12345678910
1Y
Y
Y
Y






2Y
Y
Y
Y


Y


Y
3Y
N
N
N


N


N
4Y
N








5









6









7Y
N








8







N

9








N
10Y
N






N
N

Based on these results, the background image is now 2x10 pixels. Links now work in IE 6.0.

Comment by Mike D
posted Jun 03, 2007

You are correct Phil, after exhaustive testing in IE 6 the AlphaImageLoader only works on an element via a class, does not work via an ID. Nice work M$

Comment by Junjie
posted Jun 14, 2007

i realised even those widths and heights don't work for me. http://alistapart.com/d/pngopacity/examples/ex3.htm uses 1000x1 and that seems to work for me, even in ie5.5

Comment by Greg
posted Jul 07, 2007

Link bug in IE 6.0 solution: a {position: relative} for that affected link. :-)

Comment by Gab
posted Jul 23, 2007

Nice. Almost perfect. Only problem is that IE7 will load the background 2 times, as it doesn't ignore .trans_box2[class]. This is not a big deal in this case, but it was for me.

What I did was create a perfectly normal stylesheet with no hacks. I used ID selectors to apply my background image, like I normally would. IE6 and below won't display the PNGs properly using this stylesheet.

Next I linked a second stylesheet (containing the PNG fix codes) to my page. So that the browsers apply both stylesheets at the same time, we need to apply an identical title attribute to both these styles. Now since I only wanted IE6 and below to use the second stylesheet, I applied IE's conditional comments for the second stylesheet (http://www.quirksmode.org/css/condcom.html)

The last thing to do was add "background: none !important;" to the div containing the background image in the second stylesheet, so that the background image only shows for the class selector (fixed PNG) and not the ID selector.

So basically I can now style any div normally using IDs, and if it happens to contain a PNG background, I just add the class that will be used in the second stylesheet to it, and in the second stylesheet add the ID containing the PNG. A lot less complicated imo.

Comment by Anders
posted Jul 25, 2007

Gab: After many frustrating hours, I found the same solution as you regarding the IE7-problem, with one difference; You don't need "!important" behind "background:none;" as long as the IE6-specific stylesheet is included into the markup later than the general stylesheet. So that saves you another, er, 10 bytes. ;)

Comment by Gab
posted Jul 30, 2007

Hehe, I just put !important to be on the safe side :P

Comment by Pranjal
posted Jul 31, 2007

It is really great job. It has solve my big padding work. I already used this tag but not repeat the image. Thanks and Nice work :)

Comment by Frank
posted Aug 09, 2007

Thanks so much for the "IE Link bug" solution!!

Comment by Nicklay
posted Sep 05, 2007

Can you apply this to a href class?

Comment by Saz
posted Sep 11, 2007

Can't get the position:relative for the link bug working if the container with the background png is positioned either relatively or absolutely. If I take the positioning off, it works.

I tried cutting the png to the sizes in the table. That didn't work either.

Comment by Rob C
posted Sep 20, 2007

So I have to use a png to do this, while FF works by simply setting the background color and opacity values?

Comment by Serge
posted Oct 10, 2007

ok...if i need a vertical background not a horizontal (DIV gives only horizontal background), how shall I use this?

Comment by Bryan
posted Oct 14, 2007

This looks nice, but any links on the page will not work in IE. There needs to be another fix.

Comment by Sarah H
posted Oct 17, 2007

It seems like this is still a valid option for transparent backgrounds as Angus Turnbull's method that you linked to does not function with PNGs that are tiled.

Comment by Bruno Martins
posted Oct 17, 2007

Thanks, great help! The only size that worked for me was 1x1, with the other ones, there were some links that worked and others that didn't based on their positions.

Comment by zincou
posted Nov 06, 2007

http://www.cssplay.co.uk/opacity/png.html for the links, position:relative works but needed sometimes on the parent span or p , even a z-index sometimes. when images i use an _background instead of important

posted Nov 06, 2007

Thanks a lot! Realy good!

Comment by Stephanie
posted Nov 13, 2007

Hi, can somebody tell me exactly where to add that hack. Im really new at this and i get confused in the technical language.thanx

Comment by Mat Delamere
posted Nov 26, 2007

I don't do a lot of web design as i'm more program orientated but i'm a little confused as to how to get this to work. the css hack and html don't seem to ad together to me. can somone link me basicly where to use the code in a example situation so i can see the layout and syntax as it makes sense but i've tried lots and don't seem to be able to get it to work.

Comment by Matias
posted Nov 26, 2007

Hi, I have been trying for hours to get the solution posted by Angus Turnbull to work. the one where you have to add the htc file, the blank gif and the css code. I cant get it to work. I would be really thankful if someone could send me a complete working semitransparent web file so that i could open it and see for myself exectly how everything is done. I know it sounds like too much and maybe im not earning my stars but i just cannot put the things in the right order. thank you

Comment by Mark Abucayon
posted Dec 09, 2007

Nice tutorial, saves my day. cool

Comment by Maverynthia
posted Dec 09, 2007

I'm actually having this work in Firefox, while the Crazy image filter makes it work in IE, FF doesn't want to read the .stuff[class]{ bacgroundcssstuff} so if I get it working in one browser, ir creaks in the other...

Comment by Diego Martorelli
posted Jan 18, 2008

i think the best way arround this is to use 2 divs, one for bg and one for content, and then using JS resize the background div when changing the content, thats how i do it. not too hard and works on all browsers.

Comment by video izle
posted Feb 28, 2008

Thanks a lot for this information! good article!

Comment by varmısın yokmusun
posted Feb 28, 2008

perfect tutorial, saves for website. thanks

Comment by varmısın yokmusun
posted Feb 28, 2008

perfect tutorial, saves for website. thanks

posted Feb 28, 2008

perfect tutorial, saves for website. thanks

Comment by adet dönemi
posted Feb 28, 2008

Very useful, thank you very much for sharing your information!

Comment by genç kızlar
posted Feb 28, 2008

You can find some good tutorials here always.Great solution, thanks for share this!

Comment by iş ilanları
posted Feb 28, 2008

I agree with you.Links now work in IE 6.0.I like this work and ı look forward your other works.

Comment by Rick Stock
posted Mar 20, 2008

Thanks for the explanation of the IE filter/div/link bug - that was driving me mad! Great article...

Rick

Comment by kunter
posted Mar 27, 2008

You can replace the IMG tag with a DIV at which you can take the IE-only benefits of filters as a layer background:

if (navigator.platform == "Win32" &amp;amp;&amp;amp; navigator.appName == "Microsoft Internet Explorer" &amp;amp;&amp;amp; window.attachEvent) { document.writeln('<style type="text/css">img { visibility:hidden; } </style>'); window.attachEvent("onload", fnLoadPngs); }

function fnLoadPngs() { var rslt = navigator.appVersion.match(/MSIE (\d+.\d+)/, ''); var itsAllGood = (rslt != null &amp;amp;&amp;amp; Number(rslt[1]) >= 5.5);

for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
    if (itsAllGood &amp;amp;amp;&amp;amp;amp; img.src.match(/\.png$/i) != null) {
        var src = img.src;
        var div = document.createElement("DIV");
        div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizing='scale')"
        div.style.width = img.width + "px";
        div.style.height = img.height + "px";
        img.replaceNode(div);
    }
    img.style.visibility = "visible";
}

}

Apart from the subject, avoid using "click here" or "this" to highlight web links on web pages. This causes problems for users with a visual impairment along with the anti-semantical structure causing burden on search engines.

Jakob Nielsen, a leading web usability pundit, says, "Don't use 'click here' or other non-descriptive link text." According to web programmer Jutta Denenger, "If you owned a shop, you'd write 'Welcome' on the door, not 'Open this door to enter the shop.'"

best regards p.s. the a.m. solution is posted on http://www.youngpup.net/projects/sleight/demo.html you can check the demos; as for the below part, the sentences were taken from Wikipedia you can always check.

moreover; your site's commentary system not only promotes free speech, but also shows how it is being abused deliberately to cheat SE's. Check out your Database for URL's just like the entries with Feb.the 28th post date.

again, best regards.. good site you have; kunter ilalan, web designer

Comment by Joshua Ellis
posted Mar 27, 2008

This site is incredibly useful! Thank you very much... this is information I'll be using for years. Microsoft is good job security...

Comment by wombat
posted Mar 29, 2008

Hi all!

I have a problem with this transparency on this page: www.irorszaginfo.com. It doesn't work in IE6 under Win2000. The DIV that originally has a transparent .png background in my case is just a simple gray background. Could anybody help me?

Comment by jason millward
posted Apr 03, 2008

perfect. just what i was looking for thanks

Comment by andreas
posted Apr 16, 2008

Finally! After many hours of searching and tryouts... especially the Link Prob in IE6 BIG THANKS!!!!

Comment by Stylesheet
posted Apr 18, 2008

Thank you man, you have helped me alot.

Tried it several times in Firefox and IE 7 - smoothly. Also post it in my blog: http://www.rofaldez.com/webpage/transparent-png-in-html.html

Comment by 婚紗相
posted Apr 30, 2008

Link bug in IE 6.0 solution: a {position: relative} for that affected link. :-)