views:

621

answers:

2

I've got a simple html/css page, where I show a picture.

Now when the user moves the mouse on the picture, I'd like to swap out the picture to some other image.
Upon leaving the image area the old picture should be shown again.

How can I achieve this? A version which works without javascript would be best.

Thanks in advance!

+4  A: 

This seems fairly concise and well written (note: CSS rollover solutions will not work on IE6.)

Matt Briggs
This will only work in IE6 if you use :hover on anchor elements. IE6 doesn't support :hover on other elements.
klaaspieter
There is no way to do :hover in IE6 for anything but anchor tags, and no way to do rollovers in css without the :hover psudo-class.
Matt Briggs
+4  A: 

You can use CSS to change the background image of an element when you hover over it. The hover pseudo-class is supported on most elements in recent browsers, but to be backwards compatible (Internet Explorer 6), you need to use an anchor tag for hover to work.

The onclick event in the link is just so that nothing happens if you click it, it has nothing with the hovering effect to do. You can of course do something when the image is clicked, if you like.

HTML:

<a href="#" id="picture" onclick="return false;"></a>

CSS:

#picture {
   display: block;
   width: 200px;
   height: 200px;
   background-image: url(firstimage.gif);
   border: none;
   text-decoration: none;
}
#picture:hover {
   background-image: url(hoverimage.gif);
}
Guffa
I never thought of that. I think I would do a conditional <!––[if IE 6]> javascript solution to fail back on over using a semantically incorrect element in the markup, but that is just me.
Matt Briggs