Magnifier lens
Also called image magnifier, zoom lens, hover zoom, or Lens
A round lens that follows the pointer over an image and shows a zoomed-in view of whatever is under it, so people can inspect details in place.
import { useState, type PointerEvent } from 'react';
type MagnifierProps = {
src: string;
/** A larger copy of the same photo, so the zoomed view stays sharp. */
zoomSrc: string;
alt: string;
zoom?: number;
lensSize?: number;
};
type Lens = { x: number; y: number; width: number; height: number };
export function Magnifier({
src,
zoomSrc,
alt,
zoom = 2.5,
lensSize = 160
}: MagnifierProps) {
const [lens, setLens] = useState<Lens | null>(null);
const half = lensSize / 2;
function onPointerMove(event: PointerEvent<HTMLDivElement>) {
const rect = event.currentTarget.getBoundingClientRect();
setLens({
x: event.clientX - rect.left,
y: event.clientY - rect.top,
width: rect.width,
height: rect.height
});
}
return (
<div
onPointerMove={onPointerMove}
onPointerLeave={() => setLens(null)}
// touch-none lets a finger drag the lens instead of scrolling the page.
className='relative aspect-[4/3] w-full max-w-lg touch-none overflow-hidden rounded-2xl'
>
<img src={src} alt={alt} className='size-full object-cover' />
<div
aria-hidden
className={`pointer-events-none absolute top-0 left-0 overflow-hidden rounded-full shadow-xl ring-3 ring-white transition-opacity duration-150 motion-reduce:transition-none ${lens ? 'opacity-100' : 'opacity-0'}`}
style={{
width: lensSize,
height: lensSize,
transform: lens
? `translate(${lens.x - half}px, ${lens.y - half}px)`
: undefined
}}
>
{lens ? (
// The photo at `zoom` times its size, shifted the other way so the
// point under the pointer stays in the middle of the lens.
<img
src={zoomSrc}
alt=''
className='absolute top-0 left-0 max-w-none object-cover'
style={{
width: lens.width * zoom,
height: lens.height * zoom,
transform: `translate(${half - lens.x * zoom}px, ${half - lens.y * zoom}px)`
}}
/>
) : null}
</div>
</div>
);
}Add a magnifier lens to the product photo: on pointermove, show a 160px circular lens centered on the pointer with a 3px white ring and a soft shadow. Inside it, render the same photo from a 2x larger source at 2.5× zoom, offset so the point under the pointer stays centered. Fade the lens in over 150ms on enter and out on leave, position it with transforms, let a finger drag it on touch screens, and skip the fade when prefers-reduced-motion is set.
Paste into Layerwise or any AI coding tool.
What is Magnifier lens?
A magnifier lens puts a circular window over an image at the pointer. Inside the window, a second copy of the image is scaled up, usually 2 to 3 times, and shifted the opposite way, so the spot under the pointer stays in the center. It feels like sliding a jeweler’s loupe across a print.
Use it on product photos, maps, artwork, floor plans, and detailed screenshots, where people want a closer look without opening a lightbox. Load a larger file for the zoomed copy, or the magnified view turns soft: at 2.5× zoom, a photo shown 400px wide needs a source about 1000px wide.
Move the lens with transform so it follows the pointer without layout work, and hide it when the pointer leaves. Touch screens have no hover, so show the lens while a finger drags, or offer pinch-to-zoom or a full-size view on tap. The base image keeps its alt text; the zoomed copy is decoration and gets alt="".
Related terms
All termsBefore/after slider
A comparison control that stacks two versions of an image and lets people drag a divider to reveal more of one or the other.
Custom cursor
A designed shape, such as a dot or a ring, that replaces or trails the mouse pointer and reacts to what it passes over.
Spotlight card
A card whose border and background light up with a soft radial glow that follows the mouse pointer, often across a whole grid of cards.
Scale
A CSS transform that draws an element larger or smaller around its transform origin, without pushing neighboring elements.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.
