Forum Moderators: not2easy

Message Too Old, No Replies

Hover woes - Mozilla vs. IE

Trying to have hover work in both

         

skube

9:03 pm on Nov 27, 2003 (gmt 0)

10+ Year Member



Can anyone explain why this doesn't work as one would expect in both IE and Mozilla?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
<title>Can't get this to work in Mozilla and IE6</title>

<style type="text/css">
img {display: block;}
a:hover img {
background-color: blue;
}
</style>

</head>
<body>

<div id="test"><a href="blank.html"><img src="blank.gif" width="30" height="30" alt="" /></a></div>

</body>
</html>

Reflection

9:24 pm on Nov 27, 2003 (gmt 0)

10+ Year Member



Probably because there is no background-color property for img.

skube

9:30 pm on Nov 27, 2003 (gmt 0)

10+ Year Member



Nope, changing the styles to:

<style type="text/css">
img {
display: block;
background-color: white;
}
a:hover img {
background-color: blue;
}
</style>

Has no effect other than (surprise!) changing the background color of the image...the hover effect still doesn't work in IE.

Reflection

9:33 pm on Nov 27, 2003 (gmt 0)

10+ Year Member



Why are you using an img at all? (assuming it is a blank img).

skube

9:48 pm on Nov 27, 2003 (gmt 0)

10+ Year Member



This is a simplified version of my problem. In the actual page, the image is a transparent gif which contains text, so that one as you hover over the image, it appears to change colour. Similar to an image swap, but only using one image.

skube

10:57 pm on Nov 27, 2003 (gmt 0)

10+ Year Member



Okay, this is really annoying...I can't understand why something so simple doesn't work...

Here's a modified verision, to basically demonstrate my initial problem. Using the <div> from above, and replacing the style with the following...

This will work in IE, but screws up in Moz:
img {
border: none;
color: black;
background-color: black;
}
a:hover { border: 2px solid red; }

This slight change will work in Moz, but craps out in IE.:
img {
border: none;
color: black;
background-color: black;
}
a:hover img { border: 2px solid red; }

Arg! Why isn't this issue posted anywhere?....which make me believe I'm missing something extremely basic.

Any suggestions are deeply appreciated...

Reflection

11:13 pm on Nov 27, 2003 (gmt 0)

10+ Year Member



This is a strange one...

How about trying this. Since the gif is transparent why not set the background color's on the <a> instead. Set the <a> to display block and set its width equal to the width of the img and then change the background-color on hover.

The other option is to use javascript and use onmouseover events to trigger the changes you want.

skube

11:26 pm on Nov 27, 2003 (gmt 0)

10+ Year Member



Holy crap Reflection! That did it! You are a GOD.

Simply adding the following style made it work:
a {
display: block;
width: 30px;
}

Thanks so much!

Reflection

11:33 pm on Nov 27, 2003 (gmt 0)

10+ Year Member



No prob, glad you got it to work... I wouldnt say Im a god, afterall I didnt even realize you could set the background color of an img :)

If you dont mind what was the final css you ended up with?

SuzyUK

11:47 pm on Nov 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes it's strange alright.. I set up a test and the funny thing is I can get the event to trigger in IE, but I can't even explain how or why

If you put a border onto a:hover border: none; will do then it works...

CSS:
#test {background: lime;}
#test a img{
background-color: black;
border: 2px solid yellow;
}
#test a:hover {border: none; }
#test a:hover img { border: 2px solid red; background: yellow;}

there is something (buggy) going on here as I discovered an interesting side effect during testing, but I need to run some tests to see if it's of any use

Suzy

skube

12:04 am on Nov 28, 2003 (gmt 0)

10+ Year Member



Well, after some further experimenting, it turns out you can't really set a background color on the image or anchor, but that's ok cause you can set it with the div...

Also, I noticed while testing you must specify a height in the a:hover for it to work properly in NS6.2. Kinda strange. Anyway, here's the final "test" page, which works in IE5.x, IE6, Gecko and NS6.2 (height is required for NS):

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

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
<title>This works in Mozilla and IE6!</title>

<style type="text/css">
div#testimage{width:30px;height:30px;background-color:black;color:black; }
img { border: none;}
a:hover {color: white; background-color:white;height: 30px; display:block; }
</style>

</head>
<body>

<div id="testimage"><a href="blank.html"><img src="some.gif" width="30" height="30" alt="" /></a></div>

</body>
</html>

p.s. - sorry for jumping back and forth from using border and background...it's sloppy, as I'm probably mixing two separate issues.

Reflection

12:29 am on Nov 28, 2003 (gmt 0)

10+ Year Member



it turns out you can't really set a background color on the image or anchor

Sure ya can, try this instead:

<style type="text/css">
div#testimage{width:30px;height:30px;}
img {border:none;}
a{display:block; background-color:black; width:100%; height:100%;}
a:hover {color: white; background-color:white;}
</style>

<edit>Removed height:30px;, you dont need to set the height on the <a> as the img will expand the height</edit>

<edit again>Ok so netscape 6.2 chokes without the height, so I put in height:100%;... perhaps its choking because I dont actually have an img, maybe it works if you have an actual blank.gif? </edit again>

HarryM

12:39 am on Nov 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't remember all the details, but I had similar problems trying to change the background color for a transparent gif on a:hover. I believe IE takes it's line height from the gif, and therefore it works, whereas NS6+ ignores the gif and takes its line height from whatever is set or inherited. Opera if I remember correctly was also different to IE.