views:

2341

answers:

5

Hi, I have such code:

<div id="footer">
   <a href="http:/tra-ta-ta.com">
      <div id="logo"></div>
   </a>
</div>

#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?

+1  A: 

The background-image is originally assigned to #logo. On a:hover the ANCHOR'S background-image is changed. Thus the following would work:

#logo a
{
    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);
}
Dmitri Farkov
scooped again! I need to type faster.
dnagirl
+2  A: 

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:

#footer a:hover
{
    background-image: url(../images/logo_h.png);
}

Edit:
As was pointed out in the comments:

The style of the first rule should be applied to the A element and the #logo DIV should be removed since it serves no purpose whatsoever.

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)

Donut
Downvoters: Any particular reason? This allows the style to get applied to the `<a>` tag, without having to change the HTML.
Donut
Not the anonymous downvoter, but they might feel that the proposed solution doens't follow best practices. I'll +1 you just to encourage them to speak up next time. :)
Mayo
@Donut: Except the first background is applied to the div. This is horrible design and condoning it is bad advise. The style of the first rule should be applied to the A element and the #logo DIV should be removed since it serves no purpose whatsoever. I'm sorry to down vote but I'm really tired of quick and dirty solutions popping all around SO: they just promise more questions from the OPs in the future by not emphasing on the real problem (ie: unnecessary clutter in the DOM).
Julian Aubourg
@Julian Thanks for the clarification.
Donut
@Donut: removed the downvote ;)
Julian Aubourg
+1  A: 

You've applied the hover image to an <a> element. There is no <a> element in your inside your div#logo.

dnagirl
A: 

You could style your div to have an anchor element and then use a:hover:

Then your CSS

blah a:hover {

background-image: url(myImage.jpg); }

JonH
A: 

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.

jasonevers