# Multiple images

> Attach zoom behavior to multiple images with JavaScript or React while preserving their layout.

Source: <https://medium-zoom.francoischalifour.com/docs/guides/multiple-images/>

Make several images zoomable without changing the page’s layout. Each image opens independently.

```tsx title="multiple-images.tsx"
import { ImageZoom } from '@/components/ui/image-zoom'

import './multiple-images.css'

const photos = [
  {
    src: '/assets/images/ocean-sunset.jpg',
    alt: 'Ocean sunset',
    width: 1_920,
    height: 1_280,
  },
  {
    src: '/assets/images/denali.jpg',
    alt: 'Denali at sunrise',
    width: 2_400,
    height: 1_594,
  },
  {
    src: '/assets/images/yellowstone.jpg',
    alt: 'Yellowstone at sunrise',
    width: 2_400,
    height: 1_600,
  },
  {
    src: '/assets/images/aurora.jpg',
    alt: 'Aurora over Yellowstone',
    width: 2_400,
    height: 1_600,
  },
]

export function MultipleImages() {
  return (
    <div className="photo-gallery">
      {photos.map(photo => (
        <ImageZoom key={photo.src} {...photo} />
      ))}
    </div>
  )
}
```

```css title="multiple-images.css"
.photo-gallery {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  align-items: start;
  gap: 12px;
}

.photo-gallery img {
  width: 100%;
  height: auto;
}
```

## JavaScript

Attach several images with one instance:

```html
<div class="photo-gallery">
  <img src="/ocean.jpg" alt="Ocean sunset" data-zoomable />
  <img src="/mountain.jpg" alt="Denali at sunrise" data-zoomable />
  <img
    src="/yellowstone.jpg"
    alt="Yellowstone at sunrise"
    data-zoomable
  />
  <img src="/aurora.jpg" alt="Aurora over Yellowstone" data-zoomable />
</div>
```

```js
import mediumZoom from 'medium-zoom'

const zoom = mediumZoom('.photo-gallery [data-zoomable]')
```

After adding images to the page, attach them to the instance:

```js
zoom.attach('.photo-gallery [data-zoomable]')
```

An image is attached once, even when it matches the selector again. See [`attach()`](https://medium-zoom.francoischalifour.com/docs/reference/javascript/methods/#attach) and [`detach()`](https://medium-zoom.francoischalifour.com/docs/reference/javascript/methods/#detach) for image lifecycle handling.

## React

Render one `<ImageZoom>` per image. With composition, use a separate `<ImageZoom.Root>` for each image.

```tsx
import { ImageZoom } from '@/components/ui/image-zoom'

const photos = [
  {
    src: '/ocean.jpg',
    alt: 'Ocean sunset',
    width: 1_920,
    height: 1_280,
  },
  {
    src: '/mountain.jpg',
    alt: 'Denali at sunrise',
    width: 2_400,
    height: 1_594,
  },
]

export function MultipleImages() {
  return (
    <div className="photo-gallery">
      {photos.map(photo => (
        <ImageZoom key={photo.src} {...photo} />
      ))}
    </div>
  )
}
```

Set the grid layout in CSS. Close the current zoom before opening another image.

## Related pages

- [HD images](https://medium-zoom.francoischalifour.com/docs/guides/hd-images/): Load a larger image when zooming.
- [Styling](https://medium-zoom.francoischalifour.com/docs/guides/styling/): Set the layout, captions, and zoom background.
