Skip to content

Image Hover Swap with CSS

By , Head of MarketingUpdated

Open this page in a new chat

Short answer

Put both images in the same container and stack the second one on top with opacity: 0. On hover, set it to opacity: 1 and add a short transition for a fade. No JavaScript is needed. Add :focus-within for keyboard users, and @media (hover: hover) if you want to switch the effect off on touch screens.

After reading this you will know

  • The CSS and HTML for an image rollover, ready to copy
  • How to lazy load both images with Sirv
  • How to handle keyboards, touch screens and reduced motion

The best way to swap images on hover is to place both images in the same container, with the “rollover” image transparent by default.

When the user hovers over the container, the “rollover” image becomes opaque. It’s fast loading and easy to implement.

Hover over the headphones (or tab to them with your keyboard) to see the rollover image fade in:

Bose 700 headphones, front view

That on-hover rollover effect was achieved purely with CSS and HTML. The hover image loads with the page, so there is no flicker on the first hover.

Add this CSS once. It sets up the container, hides the rollover image and fades it in on hover. The :focus-within rule gives keyboard users the same swap when the image sits inside a link.

/* Rollover image styles */
.figure {
position: relative;
width: 360px; /* can be omitted for a regular non-lazy image */
max-width: 100%;
}
.figure img {
display: block;
width: 100%;
height: auto;
}
.figure img.image-hover {
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
height: 100%;
object-fit: contain;
opacity: 0;
transition: opacity .2s;
}
.figure:hover img.image-hover,
.figure:focus-within img.image-hover {
opacity: 1;
}
/* Respect visitors who ask for less motion */
@media (prefers-reduced-motion: reduce) {
.figure img.image-hover { transition: none; }
}

These rules match both Sirv lazy loaded images and plain images, so one block covers every example below.

Next, write the HTML. There are two HTML methods for an image hover effect. Either use lazy loading, automatically scaled images or use fixed size images that will be scaled by the browser.

Recommended for most scenarios, this method will lazy load the images and scale them to the appropriate dimensions (responsive), to suit every user’s device:

<script src="https://scripts.sirv.com/sirvjs/v3/sirv.js"></script>
<div class="figure">
<img class="Sirv image-main" src="https://demo.sirv.com/hc/Bose-700-a.jpg?w=10&colorize.color=efefef" data-src="https://demo.sirv.com/hc/Bose-700-a.jpg" width="1000" height="852" alt="Bose 700 headphones, front view" />
<img class="Sirv image-hover" data-src="https://demo.sirv.com/hc/Bose-700-b.jpg" alt="" />
</div>

The tiny ?w=10&colorize.color=efefef image is a placeholder that holds the space until the real image loads. The width and height give the browser the aspect ratio, so the page doesn’t jump.

The hover image has an empty alt because screen readers only need to hear about the product once. If the second image shows something new, like the back of the product, describe it instead.

This example links the image to another page on click:

<script src="https://scripts.sirv.com/sirvjs/v3/sirv.js"></script>
<div class="figure">
<a href="https://www.example.com/">
<img class="Sirv image-main" src="https://demo.sirv.com/hc/Bose-700-a.jpg?w=10&colorize.color=efefef" data-src="https://demo.sirv.com/hc/Bose-700-a.jpg" width="1000" height="852" alt="Bose 700 headphones" />
<img class="Sirv image-hover" data-src="https://demo.sirv.com/hc/Bose-700-b.jpg" alt="" />
</a>
</div>

Because the link can take keyboard focus, the :focus-within rule shows the second image when someone tabs to it.

Alternatively, you can specify a variety of differently sized images using srcset. This tells the browser which image to load, depending on the screen size.

The chosen image will be determined by the images you define in the srcset. Images will not be as well scaled but they might load faster because sirv.js is not needed.

This is the method used in the demo above:

<div class="figure">
<img class="image-main" src="https://demo.sirv.com/hc/Bose-700-a.jpg?w=360" srcset="https://demo.sirv.com/hc/Bose-700-a.jpg?w=720 2x" width="360" height="307" alt="Bose 700 headphones, front view" />
<img class="image-hover" src="https://demo.sirv.com/hc/Bose-700-b.jpg?w=360" srcset="https://demo.sirv.com/hc/Bose-700-b.jpg?w=720 2x" width="360" height="307" alt="" />
</div>

The ?w=360 and ?w=720 parameters are Sirv dynamic imaging, so you only upload one master image.

If your images are organized into folders, you can reference the first two images in a folder, without knowing their file names (thanks to Sirv’s smart gallery feature).

Simply append .view?thumb=1 to the folder URL, where 1 is the first image in the folder, alphabetically.

For example, the following URLs will return the 1st and 2nd images from the /hc folder:

https://demo.sirv.com/hc.view?thumb=1 https://demo.sirv.com/hc.view?thumb=2

The full code for creating an image rollover effect with smart gallery image URLs is as follows:

<script src="https://scripts.sirv.com/sirvjs/v3/sirv.js"></script>
<div class="figure">
<a href="https://www.example.com/">
<img class="Sirv image-main" src="https://demo.sirv.com/hc.view?thumb=1&w=500" data-src="https://demo.sirv.com/hc.view?thumb=1" alt="Product photo" />
<img class="Sirv image-hover" data-src="https://demo.sirv.com/hc.view?thumb=2" alt="" />
</a>
</div>

Use it with the same CSS block from above. Add a new product folder and the rollover picks up its first two images automatically.

Touch screens don’t have a real hover, so make sure the first image works on its own.

If you’d prefer to switch the effect off on touch devices, split the hover rule and wrap the :hover part in a media query:

.figure:focus-within img.image-hover {
opacity: 1;
}
@media (hover: hover) {
.figure:hover img.image-hover { opacity: 1; }
}

The :focus-within rule stays outside that query so keyboard users still get the swap.

You can swap a CSS background-image on :hover. It’s fine for decorative graphics, but product photos are better as <img> tags.

They get alt text, they can be indexed by image search and they can lazy load and use srcset.

A background image swap also downloads the second file on the first hover, so it can flicker. Stacking two images avoids that.

A rollover is great for two photos, like the front and back of a product. If you want shoppers to see every angle, use a 360 product viewer instead.

They can drag to rotate the product and zoom in. See how to create a 360 spin.

No. Both are normal <img> tags, so search engines can find them. Give the main image descriptive alt text and lazy load images below the first screen to keep the page fast.

Back to top