# Image Hover Swap with CSS

## Pure CSS to swap image on hover

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](https://demo.sirv.com/hc/Bose-700-a.jpg?w=360)![](https://demo.sirv.com/hc/Bose-700-b.jpg?w=360)](#pure-css-to-swap-image-on-hover)

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.

Faster loading images and better SEO with Sirv

Deliver perfectly resized and optimized images with Sirv. Lightning fast global CDN. Serve images from your own domain. [Sign up free](https://app.sirv.com/signup)

## How to swap images with CSS

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.

```css
/* 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](/help/sirv-media-viewer/core-features/responsive-images/) or use fixed size images that will be scaled by the browser.

### Lazy loading, scaled images

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:

```html
<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:

```html
<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.

### Fixed size images

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:

```html
<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](/help/dynamic-imaging/overview/), so you only upload one master image.

## Swap images with a smart gallery

If your images are [organized into folders](/help/file-management/naming-folders-images/), you can reference the first two images in a folder, without knowing their file names (thanks to Sirv’s [smart gallery](/help/sirv-media-viewer/core-features/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=1) [https://demo.sirv.com/hc.view?thumb=2](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:

```html
<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.

## Does hover work on phones and tablets?

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:

```css
.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.

## Can I use background images instead?

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](/help/image-management/add-alt-tags-images-automatically-ai/), they can be indexed by image search and they can lazy load and use [srcset](/help/sirv-media-viewer/core-features/responsive-images/).

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

## What if I want more than two views?

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](https://sirv.com/features/360-product-viewer/) instead.

They can drag to rotate the product and zoom in. See how to [create a 360 spin](/help/sirv-media-viewer/media-types/360-spin/).

## Is it bad for SEO to have two images?

No. Both are normal `<img>` tags, so search engines can find them. Give the main image [descriptive alt text](/help/guides/best-image-seo/) and lazy load images below the first screen to [keep the page fast](/help/guides/faster-image-loading/).
