Styling
Use CSS for the image layout and captions. Open the example to see a gradient zoom background supplied through a CSS variable.
import { ImageZoom } from '@/components/ui/image-zoom'
import './captioned-photo.css'
export function CaptionedPhoto() {
return (
<figure className="captioned-photo">
<ImageZoom
src="/assets/images/ocean-sunset.jpg"
alt="Ocean sunset"
width={1_920}
height={1_280}
options={{
margin: 32,
background: 'var(--photo-zoom-background)',
}}
/>
<figcaption>Last light over the ocean.</figcaption>
</figure>
)
}:root {
--photo-zoom-background: radial-gradient(ellipse at 50% 0%, #363044, #16171c 70%);
}
.captioned-photo {
max-width: 360px;
margin: 0 auto;
}
.captioned-photo button {
max-width: 100%;
border-radius: 8px;
}
.captioned-photo img {
display: block;
width: 100%;
max-width: 100%;
height: auto;
border-radius: 8px;
}
.captioned-photo figcaption {
margin-top: 16px;
color: var(--muted-foreground, #737373);
font-size: 0.875rem;
}Image layout
In JavaScript, apply styles to the original image:
<img
class="article-photo"
src="/photo.jpg"
alt="A mountain at sunrise"
data-zoomable
/>.article-photo {
display: block;
max-width: 100%;
height: auto;
}In React, native image props such as className and style apply to the image. Use composition to style the button separately.
Captions
Keep the caption outside the zoom trigger. The example uses <figure> and <figcaption>.
The caption remains in the page when you open the image.
Zoom background and margin
The background option accepts CSS backgrounds, including gradients and variables. Define the variable on :root so the overlay can use it:
:root {
--photo-zoom-background: radial-gradient(
ellipse at 50% 0%,
#363044,
#16171c 70%
);
}The overlay is appended to body. A variable defined only on the image or its wrapper is outside the overlay's scope.
In JavaScript, pass the variable as the background option:
import mediumZoom from 'medium-zoom'
mediumZoom('[data-zoomable]', {
background: 'var(--photo-zoom-background)',
margin: 32,
})In React, pass the same options to <ImageZoom>:
import { ImageZoom } from '@/components/ui/image-zoom'
export function Photo() {
return (
<ImageZoom
src="/photo.jpg"
alt="Ocean sunset"
options={{
background: 'var(--photo-zoom-background)',
margin: 32,
}}
/>
)
}Change the CSS variable to match your application's theme. Without an explicit background option, the React component reads the image's inherited --background color. See React defaults for details.
Layering
The JavaScript library leaves z-index unset. If another element covers the enlarged image or overlay, set their stacking order in your application:
.medium-zoom-overlay,
.medium-zoom-image--opened {
z-index: 999;
}Choose a value that fits your application's layers. The React component's stylesheet sets the overlay to 50 and the enlarged image to 51.