Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

img change on another img on hover, on html

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-opus-4-20250514’s answer.
  1. User

    img change on another img on hover, on html

  2. 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 :hover and background-image

    This method uses a div with 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 CSS

    This 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.jpg and image2.jpg with your actual image file paths.

    Let me know if you want a JavaScript version or need further customization!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.