Friday, August 11, 2006

IE6 bug with semitransparent png background and nested links not working

So I had a div that had a tall, wide semi-transparent png as the background image.

Inside this div I had an absolutely positioned link.

Since IE 6 cannot display semitransparent png's as background using the CSS "background-image" property, I had to use the IE-specific "filter:" property.

For those of you unaware, the style declaration looks like this:


.semitransparent {
position:relative;
background-image:url(transparent.png);
_background-image:none;
_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='transparent.png');
}

.semitransparent a
{
position:absolute;
left:20px;
top: 20px;
}


For a quick explanation, the background-image works fine in all other modern browsers with a semi-transparent png file - BUT NOT IE! - the underscore before the next line is a hack that only IE sees, so it renders the background-image:none in IE. "filter" is ignored by other browsers, but I hate this problem so much I don't even want to declare it correctly, so I used "_filter:..." for IE.

A side note is that apparently in IE 5.x if you don't comma-separate the values in parenthesis after "AlphaImageLoader" the background-image won't work. I don't normally care about IE 5.x since if people are still using those browsers the internet looks like crap to them anyhow. (I don't like IE by the way, in case you were wondering)

So after hacking all this together for IE, I went to my page in Firefox, clicked the link and everything looked great. I went to the page in IE and everything looked great until I tried to click the link. There was no cursor when I rolled over the link, and the link did nothing when I clicked it. I tried a few things (including adding cursor:pointer to the style) and nothing seemed to work.

After much Googling, the solution came from the comments on this page:
http://www.daltonlp.com/daltonlp.cgi?item_type=1&item_id=217.

Here's what I had to do: I changed the position to static on the div, then the link worked fine, but the positioning no longer worked. SO I added another div inside the first div, gave it a position of "relative" and everything worked fine.

Here's the CSS:

.semitransparent {
position:static;
background-image:url(transparent.png);
_background-image:none;
_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='transparent.png');
}

.semitransparent div
{
position:relative;
}

.semitransparent a
{
position:absolute;
left:20px;
top: 20px;
}


Here's the simplified HTML:


<div class="semitransparent">
<div>
<a href="#">Blah<a>
</div>
</div>


I hope this saves someone else from some grief.

** one last note is that some folks found that by changing the size of the background png file the links started working in IE (you can read that on the page I linked to above) - but in my case I had a large png to begin with, so this wasn't an option. I get so frustrated with IE...**

6 Comments:

Anonymous Anonymous said...

Hi John, great little artlicle & very useful!!!

I have been suffering with PNG in IE6 for a while, sometimes links work other times they didn't.

I adapted your code and now it works a treat. Here's what I did:

.hpgutter {
position:static;
background: url(../images/hp-links-bg.png) no-repeat bottom center;
_background-image:none;
_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader (enabled=true, sizingMethod=scale, src='../images/hp-links-bg.png',);
padding: 5px 15px;
}

& then for the links within the box:

.hpgutter li a {
_position:absolute;
padding: 0;
}

Without the, _ before position it knackered up the links as they were in a list.

I've tested the site now in IE6, IE7, FireFox and Opera and it works fine. So I'm well pleased, thanks for your help.

D

12:33 PM  
Anonymous Anonymous said...

Thanx! Solved my problem. Just a note: that _ before the filter is not optional (you said you used it because you didn't want to declare it correctly) but necessary, in fact ie7 without it loads both the .png background and the filter, so for a transparency of 50% you get a 75%. With the _ is perfect. Thanks again!

2:52 PM  
Anonymous Anonymous said...

Hey John,

Thanks for the useful tip. I've been struggling getting links to work on the transparent png and setting the containing div to static did the trick. God I wish Microsoft would clean up their software. It's not like they don't have the resources to!

Uzhiel

7:51 PM  
Anonymous Anonymous said...

Your article was a life saver. Thank you for writing a clear, concise explanation. I just knew this could be done without loading up someone's scripts!

1:03 PM  
Anonymous Anonymous said...

Its really amazing help

6:31 AM  
Blogger Unknown said...

This is all well and good but none of these solution validate (w3c) so it's best to use the '!if' argument within your header tag to detect browsers OR use a pngfix type JS.

1:00 AM  

Post a Comment

<< Home