Compress

Compress JPGCompress PNGCompress WebPCompress AVIFCompress to 100 KBCompress to 200 KBCompress to 500 KB

Convert

Convert to JPGConvert to PNGConvert to WebPConvert to AVIFWebP to JPGPNG to JPGJPG to PNGWebP to PNG

Image tools

Crop ImageResize ImageImage to PDFImage to Base64

More tools

AI Prompt GeneratorColor PickerTime Zone ConverterQR Code GeneratorWord CounterPassword Generator
Web Performance

Responsive Images in HTML: srcset, sizes and the picture Element

Serve a small image to a phone and a large one to a 4K monitor, without writing any JavaScript.

The problem responsive images solve

A single large image wastes bandwidth on small screens, while a single small one looks soft on large, high-density displays. Responsive images give the browser a menu of options and enough information to choose. The browser knows the viewport width, the pixel density and often the network conditions, so it picks better than you can in advance.

srcset with width descriptors

List the available files and their widths in pixels, plus a sizes attribute describing how wide the image will be displayed:

<img
  src="/img/photo-800.jpg"
  srcset="/img/photo-400.jpg 400w,
          /img/photo-800.jpg 800w,
          /img/photo-1600.jpg 1600w"
  sizes="(min-width: 1000px) 800px, 100vw"
  width="800" height="533"
  alt="A red kayak on a calm lake"
  loading="lazy">

Here, 400w means the file is 400 pixels wide. sizes says the image fills the full viewport width on small screens and 800 CSS pixels when the viewport is at least 1000 pixels wide. The browser multiplies by device pixel density and picks the closest suitable file. Older browsers ignore srcset and use src.

Density descriptors (1x, 2x)

For images with a fixed display size, such as a logo, you can use density descriptors instead: srcset="logo.png 1x, logo@2x.png 2x". Do not mix width and density descriptors in one srcset.

The picture element: formats and art direction

Use picture when you want to offer alternative formats or different crops. The browser uses the first source it supports:

<picture>
  <source type="image/avif" srcset="/img/hero.avif">
  <source type="image/webp" srcset="/img/hero.webp">
  <img src="/img/hero.jpg" width="1200" height="630" alt="Mountain village at dawn">
</picture>

The inner img is required: it holds the alt text, dimensions and fallback. For art direction, add media conditions, for example a tighter crop for portrait phones. Create those crops with the crop tool.

Creating the image files

  1. Start from the original full-size image.
  2. Resize to each width you want to offer (for example 400, 800, 1600) with the image resizer, which can produce several sizes at once.
  3. Compress each (quality 75–85) and, if desired, convert to WebP.
  4. Name files clearly, such as photo-800.webp.

Common mistakes

  • Missing sizes: with width descriptors, the browser assumes the image is 100vw wide, so it may download a bigger file than needed.
  • No width and height: causes layout shift. Always set them.
  • Lazy-loading the hero image: delays Largest Contentful Paint. See image optimisation for Core Web Vitals.
  • Too many variants: three to five widths are usually enough.
  • Empty or keyword-stuffed alt text: write what the image shows.

A complete, copy-paste hero image

<picture>
  <source type="image/avif"
          srcset="/img/hero-800.avif 800w, /img/hero-1600.avif 1600w"
          sizes="(min-width: 1000px) 960px, 100vw">
  <source type="image/webp"
          srcset="/img/hero-800.webp 800w, /img/hero-1600.webp 1600w"
          sizes="(min-width: 1000px) 960px, 100vw">
  <img src="/img/hero-1600.jpg"
       srcset="/img/hero-800.jpg 800w, /img/hero-1600.jpg 1600w"
       sizes="(min-width: 1000px) 960px, 100vw"
       width="1600" height="900" alt="Describe the image"
       fetchpriority="high">
</picture>

This offers three formats at two widths. The browser picks the first format it supports and then the best width for the screen. Because this is the hero image, it is not lazy-loaded and has a high fetch priority. For images further down the page, add loading="lazy" and decoding="async" and remove fetchpriority.

Do you really need all this?

For a small site, the biggest wins are simple: resize images to the displayed width, compress them, and set dimensions. Add srcset and picture when you have large hero images or heavy traffic from mobile. CMS platforms such as WordPress generate srcset automatically, as covered in the WordPress guide.

Try it now: Resize Image

Free, private and runs in your browser. Nothing is uploaded.

Open Resize Image

Frequently asked questions

srcset on an img lets the browser choose between sizes of the same image. The picture element lets you offer different formats or different crops, and the browser picks the first source it can use.
It tells the browser how wide the image will be displayed before layout is complete, so it can choose a file from srcset. Without it, the browser assumes the image is as wide as the viewport.
The inner img element serves as the fallback for browsers that do not support the formats listed in the source elements. It is also required for the markup to be valid.
It helps speed, which supports Core Web Vitals, and Google recommends supported formats and responsive markup in its image guidance.

Sources and further reading

External links open the original source. We cite primary or official documentation for factual claims and re-check them when we update an article. See our editorial policy.

Keep reading