Styling

Use CSS for the image layout and captions. Open the example to see a gradient zoom background supplied through a CSS variable.

captioned-photo.tsx
Last light over the ocean.
Press Enter or Space to zoom. Click, press Escape, or scroll to close.

Image layout

In JavaScript, apply styles to the original image:

html
<img
  class="article-photo"
  src="/photo.jpg"
  alt="A mountain at sunrise"
  data-zoomable
/>
css
.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:

css
: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:

js
import mediumZoom from 'medium-zoom'

mediumZoom('[data-zoomable]', {
  background: 'var(--photo-zoom-background)',
  margin: 32,
})

In React, pass the same options to <ImageZoom>:

tsx
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:

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