Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing image on hover with CSS/HTML

I have this problem where I have set an image to display another image when the mouse hovers over, however the first image still appears and the new one doesn't change height and width and overlaps the other one. I'm still pretty new to HTML/CSS so I may have missed something simple. Here is the code:

<img src="LibraryTransparent.png" id="Library"> 
#Library {     height: 70px;     width: 120px; }  #Library:hover {     background-image: url('LibraryHoverTrans.png');     height: 70px;     width: 120px; } 
like image 941
user2704743 Avatar asked Sep 15 '13 14:09

user2704743


People also ask

How do you automatically change an image in HTML?

The following code will help to change the image automatically at a certain time interval. setInterval(function() { nextSlide() }, 5000); ➤ I have instructed that the condition of nextSlide should be effective every 5 seconds. That means the images will change every 5 seconds .

How do you change hover text in HTML?

Yes, you can use CSS content . To switch between the normal text and "Reply!", put the normal text in a span and hide that when hovering. button { width: 6em } button:hover span { display: none } button:hover:before { content: "Reply!" }


2 Answers

Another option is to use JS:

<img src='LibraryTransparent.png' onmouseover="this.src='LibraryHoverTrans.png';" onmouseout="this.src='LibraryTransparent.png';" /> 
like image 54
kurdtpage Avatar answered Nov 10 '22 01:11

kurdtpage


One solution is to use also the first image as a background image like this:

<div id="Library"></div> 
#Library {    background-image: url('LibraryTransparent.png');    height: 70px;    width: 120px; }  #Library:hover {    background-image: url('LibraryHoverTrans.png'); } 

If your hover image has a different size, you've got to set them like so:

#Library:hover {    background-image: url('LibraryHoverTrans.png');    width: [IMAGE_WIDTH_IN_PIXELS]px;    height: [IMAGE_HEIGHT_IN_PIXELS]px; } 
like image 41
Aurelio De Rosa Avatar answered Nov 10 '22 01:11

Aurelio De Rosa