#logo
{
position: relative;
width: 100px;
height: 18px;
float: right;
background-image: url('../images/logo_def.png');
background-repeat: no-repeat;
background-position: 50% 50%;
}
#logo a:hover
{
background-image: url(../images/logo_h.png);
}
It works, but bg-image doesn't change when mouse is over it, why?
The background-image is originally assigned to #logo. On a:hover the ANCHOR'S background-image is changed. Thus the following would work:
You've applied the hover image to an
<a>
element. There is no<a>
element in your inside yourdiv#logo
.Wouldn't it be simpler to use onMouseOver and onMouseOut javascript functions?
I used background colors, but the same should work with images.
It should be noted that you shouldn't put block level elements inside an anchor, which is an inline element that you can set to display block. The correct hierarchy should be something like
div#footer > a > span#logo
with the corresponding css.Well, the immediate problem is that there's no
<a>
element inside#logo
. Changing your second rule to target the correct element will "fix" this problem:Edit:
As was pointed out in the comments:
This is indeed the better fix for your problem, as it will reduce clutter and help prevent further headaches in the future. (thanks to @Julian for the clarification)
You could style your div to have an anchor element and then use
a:hover
:Then your CSS