Responsive Images: srcset and sizes Explained

Speed & Performance2 min readBy the Monopage team

A photo that looks sharp on a large desktop monitor is far bigger than a phone needs. If you send the same large file to everyone, phone users download a lot of pixels they cannot even display. Responsive images let the browser choose from several versions of an image.

The basic idea

You provide the same image in several widths and describe how wide the image will appear on the page. The browser then chooses the best file for the device's screen size, pixel density, and connection.

srcset with width descriptors

The srcset attribute lists candidate files with their actual widths:

<img src="photo-800.jpg"
     srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
     sizes="(min-width: 900px) 800px, 100vw"
     width="800" height="533" alt="A team working at a table">

Each entry says "this file is 400 pixels wide", and so on.

The sizes attribute

The sizes attribute tells the browser how wide the image will be displayed at different screen widths. In the example, on screens at least 900 pixels wide the image displays at 800 pixels, and otherwise it fills the viewport width. The browser reads the page's layout before the images load, so it needs sizes to know what to pick. If you leave it out, the browser assumes the image is as wide as the whole viewport and may download more than needed.

The picture element

Use picture when you want to offer different formats or different crops:

Always set width and height

Include width and height attributes, or use CSS aspect ratio, so the browser reserves space and the page does not jump as the image loads. See how to fix Cumulative Layout Shift.

How many sizes do you need?

Three to five widths usually cover most needs. Pick sizes that match your layout, such as 400, 800, 1200, and 1600 pixels. Do not generate dozens.

Combine with compression and lazy loading

Choose the right dimensions first, then compress. Lazy load images below the first screen, but not the main image at the top. See how to compress images for the web and lazy loading images explained.

Let your platform help

Many content management systems and image services generate multiple sizes and the srcset automatically. Check whether yours does before writing it by hand, and confirm the output is sensible.

Test it

Open developer tools, resize the window, and watch which file the browser requests. On a phone-sized window, you should see smaller files. See how to find what is slowing down your page.

Common mistakes

  • Missing sizes, so the browser guesses too large
  • Offering only one huge file
  • Skipping width and height
  • Lazy loading the main hero image

Images are only part of the picture; video can be much heavier. Read how to optimize background videos and hero media.