JavaScript quickstart

Install medium-zoom, select an image, and click it to zoom.

Using React? Follow the React quickstart. For Angular, Vue, Svelte, Solid, Astro, or VitePress, see the integrations.

Install the package

Package manager
npm install medium-zoompnpm add medium-zoomyarn add medium-zoombun add medium-zoom

The standard import includes zoom styles and TypeScript definitions.

Add an image

Add data-zoomable to an image and describe it with alt:

html
<img
  src="/assets/images/ocean-sunset.jpg"
  alt="Ocean sunset"
  data-zoomable
/>

Replace the image URL and description with your own.

Initialize the zoom

After the image is in the document, call mediumZoom():

js
import mediumZoom from 'medium-zoom'

mediumZoom('[data-zoomable]')

Click the image to enlarge it:

Press Enter or Space to zoom. Click, press Escape, or scroll to close.

Click the enlarged image or its background, press Esc, or scroll to close the zoom.

The JavaScript library attaches click behavior to the image. For keyboard activation, provide a button that calls open(), as in the video guide. The React component includes a button.

Set the image size

Once the zoom works, add width and height using the image file’s pixel dimensions. Find them in the file’s properties, or let your framework or CMS supply them. These attributes let the browser reserve space before the image loads:

html
<img
  src="/assets/images/ocean-sunset.jpg"
  alt="Ocean sunset"
  width="1920"
  height="1280"
  data-zoomable
/>

If your site doesn’t already make images responsive, add this CSS:

css
img[data-zoomable] {
  max-width: 100%;
  height: auto;
}

max-width keeps the image within its container. height: auto preserves its proportions when it shrinks.

Other installation options

Separate stylesheet

To load the stylesheet separately, import the pure entry point and CSS:

js
import mediumZoom from 'medium-zoom/dist/pure'
import 'medium-zoom/dist/style.css'

mediumZoom('[data-zoomable]')

Include the stylesheet once in your application.

Script tag

For a page without a bundler, load the UMD script after the image markup:

html
<img src="/photo.jpg" alt="A mountain at sunrise" data-zoomable />
<script src="https://cdn.jsdelivr.net/npm/medium-zoom@1.1.2/dist/medium-zoom.min.js"></script>
<script>
  mediumZoom('[data-zoomable]')
</script>

You can also download the release files (opens in a new tab) and serve the script from your own site.

Browser support

The JavaScript library retains compatibility with these browser versions:

Chrome
36+
Firefox
34+
Safari
9+
Edge
12+
Internet Explorer
10+

Use the script-tag build for browsers without module support.

Custom templates are an advanced option for building your own zoom layout. If you use them in Internet Explorer or older Edge versions, add a <template> polyfill (opens in a new tab). The default zoom needs no template or polyfill.

Next steps