img change on another img on hover, on html
- User
img change on another img on hover, on html
Assistant
Here's how to change an image on hover using HTML and CSS:
Method 1: Using CSS background-image
<!DOCTYPE html> <html> <head> <style> .image-container { width: 300px; height: 200px; background-image: url('image1.jpg'); background-size: cover; background-position: center; } .image-container:hover { background-image: url('image2.jpg'); } </style> </head> <body> <div class="image-container"></div> </body> </html>Method 2: Using two img tags with CSS
<!DOCTYPE html> <html> <head> <style> .image-wrapper { position: relative; width: 300px; height: 200px; } .image-wrapper img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; } .hover-image { opacity: 0; transition: opacity 0.3s ease; } .image-wrapper:hover .hover-image { opacity: 1; } </style> </head> <body> <div class="image-wrapper"> <img src="image1.jpg" alt="Default image"> <img src="image2.jpg" alt="Hover image" class="hover-image"> </div> </body> </html>Method 3: Using JavaScript
<!DOCTYPE html> <html> <head> <style> .hover-image { width: 300px; height: 200px; cursor: pointer; } </style> </head> <body> <img src="image1.jpg" alt="Hover to change" class="hover-image" onmouseover="this.src='image2.jpg'" onmouseout="this.src='image1.jpg'"> </body> </html>The CSS methods (1 and 2) are generally preferred as they keep styling separate from HTML. Method 2 with the opacity transition provides a smooth fade effect between images. Choose the method that best fits your needs!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.