Click to See Complete Forum and Search --> : Trouble when using Opacity in CSS with PNG in IE 7 & 8


cohq82
06-01-2009, 02:13 AM
I like to use Opacity to make some smooth fading effect for a DIV which contains several 24-PNG files that have transparency, It works fine in Firefox but not in IE 7 & 8. If I didn't use the Opacity in the first place, the transparency displays OK. But if I use Opacity, transparency for 24-PNG lose its effect.

How to fix this issue? I realized it may not be something new but I cannot find a simple solution yet.

coothead
06-01-2009, 03:18 AM
Hi there cohq82,

have a look at this example that may contain a possible fix to your problem...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<base href="http://www.coothead.co.uk/images/">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">

<title></title>

<style type="text/css">
h6 {
margin:0;
}
#compliant_browser img {
opacity:0.5;
}
#IE7_plus_browser img {
filter:alpha(opacity=50);
}
#IE7_plus_workaround {
position:relative;
width:300px;
}
#IE7_plus_workaround div {
position:absolute;
z-index:1;
width:300px;
height:300px;
opacity:0.5;
filter:alpha(opacity=50);
background-color:#fff;
}
</style>

</head>
<body>

<h6>compliant browser opacity</h6>
<div id="compliant_browser">
<img src="shadow_ball.png" alt="">
</div>

<h6>IE7+ browser opacity failure</h6>
<div id="IE7_plus_browser">
<img src="shadow_ball.png" alt="">
</div>

<h6>IE7+ opacity work around?</h6>
<div id="IE7_plus_workaround">
<div></div>
<img src="shadow_ball.png" alt="">
</div>

</body>
</html>

coothead

coothead
06-01-2009, 04:27 AM
Hi there cohq82,

I forgot to mention that if you required opacity:0.2 for the image then in the IE7+ opacity work around,
you would need to set the value as opacity:0.8 and filter:alpha(opacity=80).

coothead

cohq82
06-01-2009, 10:31 PM
I tried your example and it worked but I still don't quite understand the logic.

It seems that filter:alpha = 100 - opacity*100. Is it correct? Also the mechanism is to apply filter:alpha on some DIV layer just under the main DIV and this DIV layer must overlay the whole image. Right? Any logical thought to make sense how it works would help.

Thanks

deepu8
06-02-2009, 01:30 AM
Use that code.

<img src="abc.png" width="550" height="413" alt="abc"
style="opacity:0.4;filter:alpha(opacity=40)" />

Firefox uses the property opacity:x for transparency, while IE uses filter:alpha(opacity=x).

In Firefox (opacity:x) x can be a value from 0.0 - 1.0. A lower value makes the element more transparent.

In IE (filter:alpha(opacity=x)) x can be a value from 0 - 100. A lower value makes the element more transparent.

coothead
06-02-2009, 03:15 AM
Hi there cohq82,
Any logical thought to make sense how it works would help.
Well, 'logic' and 'IE' spoken in the same breath is an oxymoron. ;)

You have discovered that IE does not render filter:alpha(opacity=40) with transparent .png images.

Applying filter:alpha(opacity=60) to another element, with background-color set to white, positioned
above the image was found, by experimentation rather than logic, to resolve the problem. ;)

coothead

Delan
06-02-2009, 05:32 AM
By the way, you should do -moz-opacity and -khtml-opacity CSS properties too (with same 0-1 float value as opacity).

coothead
06-02-2009, 06:11 AM
Hi there Delan,

I cannot speak for Konqueror, but Firefox, Opera and Safari all render opacity now. ;)

coothead

cohq82
06-03-2009, 01:10 AM
By the way, I changed the background color of BODY and this seems not working right

<body style="background:#69C;">


I need it to be transparent in any background color. The example above assumes the background is WHITE.

Any other suggestion? Thanks

Guezt
09-05-2009, 01:55 PM
I have a set of png images with transparency. I try to make a nice cycle effect where each image fade into another. Sadly the transparency doesn't work in IE when using opacity etc. The transparent parts is turning white.

Someone know how to solve this?

Btw IE sucks!