All effectsHover & follow

Hover Image Swap

悬停切换图片

A second image replaces the default view inside the same frame.

Hover, focus, or tap the preview

Where to use it

Show bottled products from another angle or alternate between two portfolio views. Pair two views with the same crop or framing. This keeps the switch easy to interpret at a glance.

Practical example

On a backpack comparison page, hovering over a model's front photo swaps it for the rear view. Shoppers can see the straps and back padding within the same frame.

AI prompt

Add Hover Image Swap to [target]. Stack equal-size images in one frame. Transition opacity for the alternate image and expose a tap or keyboard control for alternate views. Keep the content readable in reduced-motion mode and preserve keyboard navigation.

Example code

Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.

View HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Hover Image Swap</title>
  <style>
    :root {
      color-scheme: dark;
      --demo-bg: #171629;
      --demo-ink: #f8f5ff;
      --demo-accent: #b9a6ff;
      --demo-muted: #bfbbd3;
      font-family: system-ui, sans-serif;
      color: var(--demo-ink);
      background: var(--demo-bg);
    }

    *, *::before, *::after { box-sizing: border-box; }
    body { margin: 0; min-width: 0; }
    button, input { font: inherit; }

    .demo {
      display: grid;
      place-items: center;
      align-content: center;
      min-height: 100vh;
      padding: 14px;
      overflow: hidden;
      background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
    }

    .demo-button {
      border: 1px solid #ffffff3d;
      border-radius: 7px;
      background: #ffffff12;
      color: var(--demo-ink);
      padding: 6px 11px;
      font-size: 12px;
      cursor: pointer;
    }

    :focus-visible {
      outline: 2px solid var(--demo-accent);
      outline-offset: 3px;
    }

    .target {
      position: relative;
      display: grid;
      place-items: center;
      width: min(100%, 208px);
      min-height: 84px;
      padding: 10px;
      color: #f5efff;
      border: 1px solid #aa91ee80;
      border-radius: 13px;
      background: #29213f;
      cursor: pointer;
      font: 680 15px system-ui, sans-serif;
    }
    .target[aria-pressed="true"] { outline: 2px solid #d9c9ff; outline-offset: 3px; }
    @media (prefers-reduced-motion: reduce) {
      .target, .target *, .target::before, .target::after, .target *::before, .target *::after {
        transition: none !important;
        animation: none !important;
      }
    }


    .picture {
      width: 100%; min-height: 106px; border-radius: 9px;
      background:
        radial-gradient(circle at 72% 29%, #ffe6af 0 12px, #ffd9a844 13px 18px, transparent 19px),
        linear-gradient(158deg, transparent 53%, #6652a6 53.5% 72%, transparent 72.5%),
        linear-gradient(45deg, #453676 26%, #705ab1 27% 45%, transparent 45.5%),
        linear-gradient(150deg, #574180, #a06fa6 63%, #da929a);
      transition: transform .35s ease, filter .35s ease, opacity .28s ease;
    }
    .target { padding: 5px; }
    .picture { position: relative; overflow: hidden; }
    .picture::after {
      content: ''; position: absolute; inset: 0; opacity: 0; transition: opacity .25s;
      background:
        radial-gradient(circle at 31% 25%, #e2dff8 0 10px, #c0a9ed35 11px 17px, transparent 18px),
        linear-gradient(143deg, transparent 52%, #3b376d 53% 70%, transparent 71%),
        linear-gradient(50deg, #241b48 36%, #38316f 37% 45%, transparent 46%),
        linear-gradient(145deg, #24284d, #514674);
    }
    .target:is(:hover, :focus-visible, [aria-pressed="true"]) .picture::after { opacity: 1; }
  </style>
</head>
<body>
  <main class="demo">
    <button class="target" type="button" aria-pressed="false" aria-label="Switch day and night views"><span class="picture"></span></button>
  </main>
  <script>
    const target = document.querySelector('.target');
    target.addEventListener('click', () => {
      target.setAttribute('aria-pressed', String(target.getAttribute('aria-pressed') !== 'true'));
    });
  </script>
</body>
</html>

Original example by UI Skills