Responsive images: get srcset and sizes working together

Abstract media systems diagram for Responsive images: get srcset and sizes working together

Give browsers the right candidates and a truthful display size.

Describe the actual slot

A width-descriptor srcset lists candidate image widths; sizes tells the browser how wide the image will render at each breakpoint. If sizes claims a full viewport while CSS shows a half-width card, the browser can choose an unnecessarily large file. Write sizes from the layout, not from the source dimensions.

Separate crop from resolution

Use srcset for the same composition at several resolutions. Use picture with media conditions when mobile needs a different crop or framing. Keep a fallback img with intrinsic width and height so layout space is reserved before the image loads.

Inspect the browser’s choice

Test a real mobile viewport and a high-density desktop display. Look at currentSrc in DevTools and compare the delivered pixels to the rendered slot. A candidate set is useful only if the transformation service produces those widths and the CDN caches them predictably.

A concrete acceptance test

Render the same card at narrow and wide widths, then inspect currentSrc and transferred bytes. The chosen resource should have enough pixels for the slot without sending a desktop hero to mobile. Run this test with a realistic asset and record the observed state, output URL, and failure reason.

Implementation check

  • Match sizes to the actual CSS slot width.
  • Use picture when the crop changes across layouts.
  • Inspect currentSrc on mobile and high-density screens.

Related guides

Primary reference: Official documentation.