gpt-4.1-2025-04-14· OpenAI
LMArena Human Preference 140K
img change on another img on hover, on html
- User
img change on another img on hover, on html
- Assistant
Certainly! To change an image to another image when you hover over it using only HTML and CSS, you can use the following approach:
Method 1: Using CSS
:hoverandbackground-imageThis method uses a
divwith a background image that changes on hover.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Image Hover Change</title> <style> .img-hover { width: 300px; /* Set your image width */ height: 200px; /* Set your image height */ background-image: url('image1.jpg'); background-size: cover; background-position: center; transition: background-image 0.3s; display: inline-block; } .img-hover:hover { background-image: url('image2.jpg'); } </style> </head> <body> <div class="img-hover"></div> </body> </html>
Method 2: Using Two
<img>Tags and CSSThis method uses two images stacked on top of each other, and the top one is hidden on hover.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Image Hover Change</title> <style> .img-container { position: relative; width: 300px; /* Set your image width */ height: 200px; /* Set your image height */ display: inline-block; } .img-container img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; transition: opacity 0.3s; } .img-container img.img-top { opacity: 1; } .img-container:hover img.img-top { opacity: 0; } </style> </head> <body> <div class="img-container"> <img src="image2.jpg" class="img-bottom" alt="Image 2"> <img src="image1.jpg" class="img-top" alt="Image 1"> </div> </body> </html>
Replace
image1.jpgandimage2.jpgwith your actual image file paths.Let me know if you want a JavaScript version or need further customization!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.