tags:

views:

51

answers:

5

Hi, i want my icon

<img class="addFriend" src="images/addFriend.png" style="padding-right: 55px;">

to change to

when mouse over.. like you can do with a:hover, how can i do this?

I tried

addFriend img:hover{
    background: url(images/addFriend_hover.png);
}
A: 

You have to use a container with a background image, e.g. a div with a specified height and width. Then you can use the background image and a :hover-pseudo-class.

0x90
:hover pseudo-class is not supported in IE6 for DIV tags which is why he'd need to use anchor tags.
Mark
Good point Mark. I don't think about that nowdays because I have the option of refusing to support IE<7
Stephen P
+4  A: 

Ditch the img tag, and do it without javascript

(off the top of my head, untested)

HTML

<div class="addFriend"><a href="addfriend.html">Add Friend</a></div>

CSS

.addFriend { background: url(images/addFriend.png); }
.addFriend:hover { background: url(images/addFriend_hover.png); }
Stephen P
This example uses more mark-up then is needed. Also, unless the font-size is big enough, the image will be cut-off because you aren't setting a fixed width and height of the image. The image will also be overlapped by the text Add Friend.
Mark
+2  A: 

You could also use jQuery.

$('img.addFriend').hover(function() {
$(this).attr('src','images/addFriend_hover.png');
}, function() {
$(this).attr('src','images/addFriend.png');
});

Edit You could also do non-jQuery, I assume you don't want a background-image or an href

<img src='images/addFriend.png' onmouseover='this.src="images/addFriend_hover.png"' onmouseout='this.src="images/addFriend.png"' />

Robert
+1  A: 

For it to work in all browsers with the least amount of code you can do it with CSS.

<a href="#" class="icon">Add A Friend</a>



.icon {
  display: block;
  width: (width of image);
  height: (height of image);
  text-indent: -9999px; /* hides the text 'Add A Friend' */
  background: url(url of image) no-repeat center center;
  padding-right: 55px;
}

.icon:hover {
  background: url(url of new image) no-repeat center center;
}
Mark
A: 

This tutorial might be useful: http://www.w3schools.com/js/js_animation.asp To see if it does what you want, scroll down to "The Entire Code" and click "Try it yourself".

scott77777