0

I am trying to do a image change when the mouse rolls over the image, but I have to use CSS not java script. I got the image to show up, but it shows up behind the first image, it doesn't replace it.

<a href="http://google.com"><img id="rollover" src="cat.jpg" width="250" height="188" alt="Cat"></a>

#rollover:hover {background-image: url(dog.jpg);} 
img {padding: 5px;}

4 Answers4

1

Don't use the img tag. Use the a itself:

a
{
    display:inline-block;
    width: 250px;
    height: 188px;
    background: red;
} 
a:hover
{
    background: blue;
} 
<a href="http://google.com"></a>
Eun
  • 4,146
  • 5
  • 30
  • 51
0

Set the first image via CSS also.

<a href="http://google.com"><div id="rollover" width="250" height="188"></div></a>

#rollover {
  background-image: url(cat.jpg);
}

#rollover:hover {
  background-image: url(dog.jpg);
}
user1438038
  • 5,821
  • 6
  • 60
  • 94
0

You could have two images and switch display property example

    #dog{
        display: none;
        }
    #cambio:hover > #cat{
        display: none;
    }
    
    #cambio:hover > #dog{
        display:block;
        
    }
   <a href="#" id="cambio">
        <input type="button" id="dog" value="Perro" />
        <input type="button" id="cat" value="Gato" />
    </a>
chepe263
  • 2,774
  • 22
  • 38
0

You dont need anotehr element of A...

<a href="http://google.com" id="rollover"> </a>

#rollover {   background-image: url(cat.jpg); 
     width: 250px; height: 188px; 
     display: block;
}
#rollover:hover {   background-image: url(dog.jpg);  }