Image guides

How to Choose Responsive Image Widths for srcset and sizes

Published

Measure how wide the image displays in each layout, then use those CSS widths in sizes. Export copies of the same image at file widths that cover those slots at the pixel densities you care about, and list each file in srcset with its actual width. The browser uses both attributes to choose a file. Confirm which file it requests on a fresh page load with the cache disabled.

Measure the image slot across your layout

Inspect the image in your browser’s developer tools and record its rendered width in CSS pixels. Check narrow and wide viewports, both sides of each breakpoint, and the point where a container reaches its maximum width. In a grid, measure the image inside its own card. The card width may change when another column appears even as the viewport gets wider.

Write sizes from the same rules that set those widths in CSS. The browser uses the first matching condition, so put the most restrictive minimum-width condition first when several could match. End with an unconditional length for the remaining viewports. Include gutters in the calculation for a fluid slot.

The browser ignores everything after the first matching condition, so be careful how you order the media conditions.
MDN — Responsive images (sizes)
An improvement would be to display a cropped version of the image which displays the important details of the image when the site is viewed on a narrow screen.
MDN — Responsive images (art direction)
The spec requires that the sizes attribute only be present when srcset uses width descriptors.
MDN — img element
When it first opens, the Network Monitor does not show request information.
MDN — Network Monitor
Cropping or modifying images for different media conditions (for example, loading a simpler version of an image which has too many details, on smaller displays).
MDN — picture element
This helps the browser select the most appropriate image from the srcset attribute, which lists images with their intrinsic widths.
MDN — picture element (srcset)

Establishes the order in which the browser evaluates the sizes list. [1]

Explains when a separate crop helps preserve important details on a narrow screen. The linked section shows how to use picture for the change. [2]

Establishes the relationship between sizes and the width descriptors used in the example. [3]

Explains why the panel can be empty when first opened. All four excerpts were supplied with the brief. Live page verification was unavailable. [4]

Verbatim excerpt from the source main content. [5]

Verbatim excerpt from the source main content. [6]

Choose export widths for the slots you measured

Start with the slot width in CSS pixels. For an estimate on a display with a device pixel ratio of 2, double that width. Group nearby estimates so multiple layouts can share the same file. Export each copy from the same master with the same framing and aspect ratio, in any image editor that can save at an exact pixel width. Give each file a name that makes its width easy to verify.

Consider candidates wider than the largest CSS slot. A 560 CSS pixel image may benefit from a 1120-pixel file on a high-density screen. Compare the extra transfer size with the visible improvement at its displayed size. If the master has limited detail, enlarging it adds pixels without recovering detail. Cap the export widths where the sharper result stops justifying the extra bytes.

Work through a fluid mobile slot and two desktop widths

Suppose your layout gives an image 16 CSS pixels of space on each side below a 768 CSS pixel viewport. At 768 CSS pixels the image moves into a 360 CSS pixel column, and at 1200 CSS pixels it moves into a 560 CSS pixel column. Set sizes to (min-width: 1200px) 560px, (min-width: 768px) 360px, calc(100vw - 32px). The order matters because both minimum-width conditions match at 1200 CSS pixels.

At a 390 CSS pixel viewport, the example slot is 358 CSS pixels wide. Doubling that gives a target of about 716 file pixels. Just below 768 CSS pixels, the fluid slot can approach 736 CSS pixels, so its doubled target can exceed the wide desktop column’s target. A starting srcset for this layout is trail-400.jpg 400w, trail-720.jpg 720w, trail-1120.jpg 1120w, trail-1480.jpg 1480w. Each w descriptor must match the file’s actual pixel width. These are example exports derived from the layout, and the browser makes the final selection. Add an intermediate copy if testing shows a recurring jump that costs noticeably more bytes.

Use picture when the crop needs to change

If the main subject gets too small in the wide composition on a phone, export a tighter crop from the master. Write a picture element that lists a source with a media condition matching the narrow layout before the final img element, give that source its own srcset and sizes for the tighter crop, and keep the wide crop’s srcset, its sizes, the fallback src, and the alt text on the img element. Without a sizes attribute on the img element the browser assumes the image spans the full viewport width and may fetch an oversized file for the desktop columns. A media condition that applies below 768 CSS pixels can select the tighter crop for the example layout, and the wide crop applies from that width upward. Check that the image description fits both crops.

Check the downloaded file with the cache disabled

Open the Network panel in the browser developer tools, disable the cache, and reload the page at each viewport you want to check. Find the image request and confirm that the requested file is the one you expect for that slot. Use a fresh reload for each check, because an already loaded page can keep a larger image in use after the layout shrinks.

If a narrow card repeatedly fetches an oversized file, compare the measured width with the first matching sizes entry and check the spacing between candidates. If the image looks soft on the page, confirm that the downloaded file has enough detail for that slot before exporting another width. Repeat the check for each crop if you use picture.

Sources

  1. MDN — Responsive images (sizes)
  2. MDN — Responsive images (art direction)
  3. MDN — img element
  4. MDN — Network Monitor
  5. MDN — picture element
  6. MDN — picture element (srcset)