Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: image link, change on hover

Tags:

html

css

I have an image that is a link. I want to show a different image when the user hovers over the link.

Currently I'm using this code:

<a href="http://twitter.com/me" title="Twitter link"> <div id="twitterbird" class="sidebar-poster"></div></a>  div.sidebar-poster { margin-bottom: 10px; background-position: center top; background-repeat: no-repeat; width: 160px; } #twitterbird { background-image: url('twitterbird.png'); } #twitterbird:hover { background-image: url('twitterbird_hover.png'); } 

But I'm having loads of problems: the div isn't picking up the CSS rules (the element just isn't showing the related CSS rules when I view it in Firebug).

Perhaps this is because (as I know) this is invalid HTML: you can't put an <a> around a <div>. However, if I switch to <span> then it seems I get bigger problems, because you can't set a height and width on a span reliably.

Help! How can I do this better?

like image 834
AP257 Avatar asked Jan 17 '11 19:01

AP257


People also ask

How can I change image on hover?

Answer: Use the CSS background-image property You can simply use the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover.

How do I change the click image in CSS?

A Pure CSS SolutionUtilize the :checked pseudo class - attach it to a pseudo element of a checkbox (since you can't really affect the background of the input itself), and change its background accordingly.


1 Answers

 <a href="http://twitter.com/me" class="twitterbird" title="Twitter link"></a> 

use a class for the link itself and forget the div

.twitterbird {  margin-bottom: 10px;  width: 160px;  height:160px;  display:block;  background:transparent url('twitterbird.png') center top no-repeat; }  .twitterbird:hover {    background-image: url('twitterbird_hover.png'); } 
like image 68
Caspar Kleijne Avatar answered Nov 08 '22 19:11

Caspar Kleijne