# JavaScript quickstart

> Install medium-zoom and make your first image zoomable with HTML, JavaScript, and a working example.

Source: <https://medium-zoom.francoischalifour.com/docs/javascript/>

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

Using React? Follow the [React quickstart](https://medium-zoom.francoischalifour.com/docs/react/). For Angular, Vue, Svelte, Solid, Astro, or VitePress, see the [integrations](https://medium-zoom.francoischalifour.com/docs/#integrations).

## Install the package

```sh
npm install 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:

![Ocean sunset](https://medium-zoom.francoischalifour.com/assets/images/ocean-sunset.jpg)

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()`](https://medium-zoom.francoischalifour.com/docs/reference/javascript/methods/#open), as in the [video guide](https://medium-zoom.francoischalifour.com/docs/guides/video/#prepare-the-poster). The [React component](https://medium-zoom.francoischalifour.com/docs/react/) 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](https://github.com/francoischalifour/medium-zoom/releases) 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](#script-tag) 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](https://github.com/webcomponents/template). The default zoom needs no template or polyfill.

## Next steps

- [HD images](https://medium-zoom.francoischalifour.com/docs/guides/hd-images/): Load a larger image when zooming.
- [Multiple images](https://medium-zoom.francoischalifour.com/docs/guides/multiple-images/): Attach zoom behavior to several images.
- [Styling](https://medium-zoom.francoischalifour.com/docs/guides/styling/): Set the layout, captions, and zoom background.
- [mediumZoom()](https://medium-zoom.francoischalifour.com/docs/reference/javascript/medium-zoom/): Select images and create a zoom instance.
