How to Swap Images on Hover with CSS
To show a different image on hover, layer two equally sized images and reveal the one that comes later in the HTML. Use display for an instant switch or opacity and transition for a fade. These examples also respond to keyboard focus.
Run the two image swap examples
Save the HTML as index.html and the CSS as style.css in the same folder. Open the HTML in a browser. Both sample images are embedded in the HTML. Point to each link in turn or use Tab to focus them and compare the results.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS image swap example</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Compare two image swaps</h1>
<a class="swap swap--instant" href="#notes">
Read the instant swap notes
<span class="swap-art">
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20320%20240%22%20style%3D%22background%3A%23bae6fd%22%3E%3Ccircle%20cx%3D%22160%22%20cy%3D%22120%22%20r%3D%2270%22%20fill%3D%22%231e40af%22%2F%3E%3C%2Fsvg%3E" width="320" height="240" alt="">
<img class="alternate" src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20320%20240%22%20style%3D%22background%3A%23fed7aa%22%3E%3Cpath%20d%3D%22M160%2035%20245%20195H75Z%22%20fill%3D%22%239a3412%22%2F%3E%3C%2Fsvg%3E" width="320" height="240" alt="">
</span>
</a>
<a class="swap swap--fade" href="#notes">
Read the fade notes
<span class="swap-art">
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20320%20240%22%20style%3D%22background%3A%23bae6fd%22%3E%3Ccircle%20cx%3D%22160%22%20cy%3D%22120%22%20r%3D%2270%22%20fill%3D%22%231e40af%22%2F%3E%3C%2Fsvg%3E" width="320" height="240" alt="">
<img class="alternate" src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20320%20240%22%20style%3D%22background%3A%23fed7aa%22%3E%3Cpath%20d%3D%22M160%2035%20245%20195H75Z%22%20fill%3D%22%239a3412%22%2F%3E%3C%2Fsvg%3E" width="320" height="240" alt="">
</span>
</a>
<p id="notes">The images are decorative. The link label and destination stay the same when the image changes.</p>
</body>
</html>
* {
box-sizing: border-box;
}
body {
margin: 1rem;
font: 1rem/1.5 system-ui, sans-serif;
color: #172033;
}
.swap {
display: block;
max-inline-size: 20rem;
margin-block: 1.25rem;
color: #153770;
}
.swap:focus-visible {
--alternate-display: block;
--alternate-opacity: 1;
outline: 2px solid #153770;
outline-offset: 4px;
}
.swap-art {
display: grid;
aspect-ratio: 4 / 3;
margin-block-start: 0.35rem;
}
.swap-art img {
grid-area: 1 / 1;
display: block;
inline-size: 100%;
block-size: 100%;
object-fit: cover;
}
.swap--instant .alternate {
display: var(--alternate-display, none);
}
.swap--fade .alternate {
opacity: var(--alternate-opacity, 0);
transition: opacity 300ms ease;
}
@media (hover: hover) {
.swap:hover {
--alternate-display: block;
--alternate-opacity: 1;
}
}
@media (prefers-reduced-motion: reduce) {
.swap--fade .alternate {
transition: none;
}
}
Place both images in the same box
Place both images in a display: grid container and give them grid-area: 1 / 1. The later image is painted above the first. aspect-ratio: 4 / 3 preserves the shape of the box so the content below stays in place during a swap. CSS Grid stacking order
To use your own photos, replace the values in src with file paths. object-fit: cover fills the box without distorting the image, but may crop a photo with a different aspect ratio. Use contain when the entire photo matters and check the space around it. CSS image fitting rules
Switch instantly with display
The first link starts with the upper image set to display: none. Pointing to the link or focusing it sets --alternate-display to block, immediately revealing the upper image. The original comes back once both hover and focus are gone.
Fade in the alternate image with opacity
The upper image in the second link starts transparent with opacity: 0. It takes 300ms to reach full opacity and fades back when the state ends. The lower image stays in place so the box never goes blank. With reduced motion, the transition is disabled and the image appears instantly. Reduced motion media feature
Check keyboard, touch, and alternative text
The hover rule applies when the primary input can hover. On touch devices, the default image still identifies the link, and tapping follows it. If the alternate photo contains important information, such as a product detail, show it in the page or provide a separate view button. Hover capability of the primary input
The demo images decorate links whose visible labels already describe their purpose, so both use alt="". Use Tab and check for the new image and a focus outline together. Give meaningful photos suitable alternative text and present essential information regardless of where the pointer is. W3C guidance on decorative images




