Srcset and Sizes: Serving the Right Image to the Right Screen

Srcset and sizes explained: x and w descriptors, how the browser picks an image, worked examples, writing accurate sizes, and debugging with currentSrc.

Executive Summary: srcset lists the versions of an image you have; sizes tells the browser how wide the image will be displayed. From those two facts and the screen’s pixel density, the browser downloads the smallest file that still looks sharp. Use x descriptors for images with a fixed display size, like logos and avatars, and w descriptors plus an accurate sizes for anything fluid. Without sizes, the browser assumes the image fills the whole viewport, and a thumbnail in a three-column grid downloads a full-width file.
Three image sizes of increasing width representing srcset candidates

A blog’s article thumbnails are displayed at about 360 pixels wide in a three-column grid. The source files are 2400 pixels wide, exported straight from the design tool, around 600 KB each. Twelve thumbnails on the home page add up to over 7 MB, on a phone, for images that never display larger than a business card. Every one of them could have been a 60 KB file.

Responsive images are images that come in several files, so each device downloads one suited to its screen. The srcset attribute on <img> lists the candidate files with a descriptor for each, and the sizes attribute describes how wide the image will be rendered in different layout conditions. This is resolution switching: the same picture at different sizes. When you need a different crop or composition for different screens, that is art direction, which needs the <picture> element instead.

How do x descriptors work for fixed-size images?

When an image always displays at the same CSS size, such as a 64-pixel avatar, a logo or an icon, you only need to account for pixel density:

<img src="/img/avatar-64.webp"
     srcset="/img/avatar-64.webp 1x, /img/avatar-128.webp 2x, /img/avatar-192.webp 3x"
     width="64" height="64" alt="Priya Nair">

A standard screen gets the 64-pixel file, a Retina laptop or most phones get the 128-pixel file, and high-density phones get 192. The src is the fallback for browsers that ignore srcset, which in practice is none of the current ones but it costs nothing to keep.

How do w descriptors and sizes work for fluid images?

Most content images are fluid: full width on a phone, half width on a tablet, a third of a container on a desktop. Here, describe each file by its actual pixel width with w, and tell the browser how wide the image will be displayed with sizes:

<img src="/img/post-800.webp"
     srcset="/img/post-400.webp 400w,
             /img/post-800.webp 800w,
             /img/post-1200.webp 1200w,
             /img/post-1600.webp 1600w"
     sizes="(min-width: 1200px) 360px,
            (min-width: 768px) calc(50vw - 48px),
            calc(100vw - 32px)"
     width="1600" height="900" alt="Kanban board with three columns" loading="lazy">

sizes is a list of media conditions and lengths. The browser uses the length from the first condition that matches, and the last entry, with no condition, is the default. Lengths can be px, vw, em or a calc() expression, but not percentages, because the browser evaluates sizes before it knows anything about your CSS.

How does the browser choose an image?

For a w descriptor srcset, the browser does three things:

  1. Evaluates sizes to get the slot width in CSS pixels.
  2. Multiplies by the device pixel ratio to get the pixels it needs.
  3. Picks a candidate, usually the smallest one at least that wide.
Device Viewport DPR Slot from sizes Pixels needed Likely choice
Phone 390px 3 100vw – 32px = 358px 1074 1200w
Small Android phone 360px 2 328px 656 800w
Tablet 820px 2 50vw – 48px = 362px 724 800w
Laptop 1440px 2 360px 720 800w
Desktop monitor 1920px 1 360px 360 400w

The specification deliberately leaves the final choice to the browser. It may pick a smaller file on a slow connection or with data saver turned on, and if a larger candidate is already in the cache it may reuse that instead of downloading a smaller one. That flexibility is the point: you describe the situation, the browser makes the decision with information you do not have.

Why does sizes matter so much?

If you omit sizes, the browser assumes 100vw. For the desktop monitor in the table, that means a 1920-pixel slot instead of 360, and the browser downloads the 1600w file, more than ten times the pixels it needs. Missing or inaccurate sizes is the single most common responsive image mistake, and it silently wipes out the benefit of generating multiple files.

Write sizes from your actual CSS layout. For a grid with a max-width container, the image width usually stops growing at the container’s maximum, so the largest breakpoint should be a fixed pixel value, as in the example above. It does not have to be exact; within 10 to 20 percent is fine, because the browser rounds up to the next candidate anyway. But it must reflect the layout, and it must be updated when the layout changes.

Checking your work

  • In the console, select the image and read $0.currentSrc to see which file the browser chose.
  • Compare the image’s rendered width in the Elements panel with the natural width of the downloaded file. A file more than about twice the needed pixels (after accounting for DPR) points to a wrong sizes.
  • Lighthouse’s “Properly size images” audit flags oversized downloads with the potential savings.
  • Test at several viewport widths with device emulation, and clear the cache between tests, since a cached larger file is reused.

How many image sizes should you generate?

More candidates mean less waste per device but more files to generate and cache. A practical approach is steps of about 300 to 400 pixels from the smallest useful width up to around twice your largest display width. For content images that max out at 800 CSS pixels, widths of 400, 800, 1200 and 1600 cover 1x and 2x screens well.

Above 2x density, the visible difference is small for photographs while the file size keeps growing. Many teams cap the largest candidate at twice the maximum display width even though some phones have a pixel ratio of 3. The table above shows the effect: the 390-pixel phone picks the 1200w file, and nobody can see the difference from a 1600w one.

You rarely need to generate these files by hand. WordPress has created multiple sizes and output srcset and sizes automatically since version 4.4, though its default sizes often assumes a wider slot than your theme’s layout and is worth overriding. Image CDNs such as Cloudinary, imgix and Cloudflare Images resize on the fly from URL parameters, and frameworks like Next.js and Astro generate srcset from a single source image.

Responsive images and other attributes

  • width and height still matter. Set them to the intrinsic size of any candidate (they only need the right aspect ratio) so the browser reserves space and avoids layout shift.
  • loading="lazy" works with srcset, and the rules from the native lazy loading guide apply: keep the hero image eager.
  • Formats are a separate concern. srcset chooses a size, not a format. To offer AVIF with a WebP or JPEG fallback, wrap the image in <picture> with typed <source> elements, each with its own srcset and the same sizes.

Frequently Asked Questions

What is the difference between srcset and sizes?

srcset lists the image files available and their widths or pixel densities. sizes tells the browser how wide the image will be displayed in different layout conditions. The browser combines both with the screen’s pixel density to choose which file to download.

When should I use x descriptors instead of w descriptors?

Use x descriptors, such as 1x and 2x, for images that always display at the same CSS size, like logos, icons and avatars. Use w descriptors with sizes for fluid images whose display width changes with the layout.

What happens if I leave out the sizes attribute?

With w descriptors and no sizes, the browser assumes the image is as wide as the viewport, 100vw. Images displayed smaller than that, such as grid thumbnails, then download much larger files than necessary.

How can I tell which image the browser downloaded?

Select the image in DevTools and check its currentSrc property in the console, or look at the Network panel filtered to images. Clear the cache when testing, because browsers may reuse a larger cached candidate.

Describe the layout, let the browser choose

Responsive images come down to two honest statements: here are the files I have, and here is how big the image will be on screen. Generate a sensible set of widths, write sizes from your real CSS, check currentSrc at a few viewports, and the browser takes care of every combination of screen size, density and network you will never be able to test by hand. For the full reference, see the MDN responsive images guide and the images section of the HTML Standard. Related reading on this site: the picture element and performance-safe CSS.

Originally posted on 13 February 2024 and post last modified on 13 February 2024

Front-End Development HTML5 Responsive Images Srcset Web Performance

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *