Forum Moderators: not2easy

Message Too Old, No Replies

Link surrounding transparent.png with background image

...flashes in IE on hover

         

SilverLining

10:42 am on May 17, 2007 (gmt 0)

10+ Year Member



I have a div with a transparent.png image in the foreground and an image in the background. IE makes use of the
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader
for this to work. Looks great in both IE and FF.

The next step was to add a link around the image. The link is only displayed in FF, because IE stylesheet is set to place the image left, for the transparent img to be displayed correctly (

padding-left: 247px;
).

So for this to work in IE, the link was placed around the surrounding div. I seem to recall that this is not good practice? Now the link works in IE too, but the image flashes for a split second on hover over the transparent.png.

Has anyone seen this before and now of a quick fix?

SilverLining

3:44 pm on May 17, 2007 (gmt 0)

10+ Year Member



Here's some example code:

HTML


<a href="index.htm">
<div class="bgimage" style="background: url('/img/blah.gif') transparent 0% 0% no-repeat;">
<img class="transimage" src="/img/transparent.png" width="200" height="150" border="0" />
</div>
</a>

CSS


.bgimage,
img.transimage {width: 200px; height: 150px; float: left;}

IE CSS


.bgimage {width: 200px; height: 150px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='transparent.png'); padding-left: 335px;}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Any ideas are greatly appreciated.

tomda

4:06 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I don't want to look bad but to me, this is not XHTML Transitional valid... I know it is not your main issue here, but still it is one major issue.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head>
<title>First COokie</title>
</head>
<body>
<a href="index.htm"><div class="bgimage" style="background: url('/img/blah.gif') transparent 0% 0% no-repeat;"><img class="transimage" src="/img/transparent.png" width="200" height="150" border="0" alt="" /></div></a>
</body>
</html>
W3C returns a nested error for the above.

Rgds

Fotiman

4:33 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



tomda is correct. It's not valid for an <a> element to contain block level items (like <div>).

Try something like this:

<div class="bgimage" style="background: url('/img/blah.gif') transparent 0% 0% no-repeat;">
<a href="index.htm"><img class="transimage" src="/img/transparent.png" width="200" height="150" border="0" /></a>
</div>

Then style your <a> like this:

.bgimage a {
display: block;
width: 200px;
height: 150px;
}

You mmay need to add some other styles (overflow?) to get it to line up correctly (I haven't tried it), but the goal would essentially be to tell the <a> element to fill the contents of the containing div.

[edited by: Fotiman at 4:39 pm (utc) on May 17, 2007]

SilverLining

5:10 pm on May 17, 2007 (gmt 0)

10+ Year Member



Thank you for your comments. Points taken :)

I tried adding a class to the

a
tag this afternoon, because there are other links inside the same div, but still couldn't get that to work.

So now I've added similar code to what Fotiman suggested:


.bgimage a.test {
display: block;
width: 200px;
height: 150px;
}

and also floated it left, because when I inspect the HTML in IE with the DOM inspector, the Div is aligned to the right of the background-image which is displayed.

Something for me to dream about tonight.

tomda

5:20 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried adding a class to the a tag this afternoon, because there are other links inside the same div, but still couldn't get that to work.

What about using

.bgimage a:link.test, .bgimage a:visited.test, .bgimage a:hover.test, .bgimage a:active.test {

instead of

.bgimage a.test {

Fotiman

6:32 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




and also floated it left, because when I inspect the HTML in IE with the DOM inspector, the Div is aligned to the right of the background-image which is displayed.

Floating it will essentially take it out of the flow, which may or may not be desirable (not in most cases). You might also be able to do something like:

position: relative;
top: 0;
left: 0;

instead of floating it. Just a thought.


What about using

.bgimage a:link.test, .bgimage a:visited.test, .bgimage a:hover.test, .bgimage a:active.test {

instead of

.bgimage a.test {

That shouldn't matter (unless you have another rule with equal or greater specificity overwriting your rule).

[edited by: Fotiman at 6:33 pm (utc) on May 17, 2007]

Xapti

7:02 am on May 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fotiman, why style the link display: block? A,><img/></a> is fine.
The reason to make A a block element would be if you wanted to keep the same markup: <a><div><img/></div></a>

SilverLining

11:33 am on May 18, 2007 (gmt 0)

10+ Year Member



Okay, i've tried adding this to the IE specific stylesheet:


position: relative;
top: 0;
left: 0;
margin-left: -200px; /* To move the link over the bg image */

but this sort of defeats the whole object, because now the transparency is not implemented. Please note that the padding-left in IE (listed above) should be 200px (same as width of the image). I can place the A tag precisely on top of the image, but as far as I understand in IE one needs to offset the transparent image for it to be displayed correctly. By moving the A tag over the background image, the transparency is lost and a white and light blue image is displayed on top of the img.

Ideally the div structure should be:

<div>
<a href="#"><img /></a>
</div>

I can't think of a better way of explaining myself. Otherwise it just wasn't meant to be.

[edited by: SilverLining at 11:40 am (utc) on May 18, 2007]

Fotiman

3:25 pm on May 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Fotiman, why style the link display: block? A,><img/></a> is fine.
The reason to make A a block element would be if you wanted to keep the same markup: <a><div><img/></div></a>

No, actually, that would still be invalid.

The reason I added display block was so that you could set explicit width and height dimensions on the <a> to make sure it correctly surrounded the <img>. It would be invalid, though, to assume that you could then wrap a <div> with the <a>.

Fotiman

4:43 pm on May 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok, I think I've got a solution. It required adding another wrapper and also required some absolute and relative positioning (so I'm not sure how well that will tie into your current layout, or if it will cause any problems). Here's a working example:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title></title>
<style type="text/css">
.container {
background-image: url(test.png);
position: absolute;
width: 200px; /* width of test.png */
height: 150px; /* height of test.png */
}
.innerWrap {
width: 200px; /* width of test.png */
height: 150px; /* height of test.png */
}
</style>
<!--[if lt IE 7]>
<style>
.innerWrap {
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader (src='test.png', sizingMethod='crop');
}
.container {
background-image: none;
}
a {
position: relative;
z-index: 1;
}
.transimage {
visibility: hidden;
}
</style>
<![endif]-->
</head>
<body>
<div class="container">
<div class="innerWrap">
<a href="index.htm"><img class="transimage" src="test.png" border="0" /></a>
</div>
</div>
</body>
</html>