Web development

Serve responsive images with srcset and sizes

Use width descriptors in srcset and a matching sizes attribute to help the browser choose an image suited to the rendered slot and device pixel density.

Worked example

HTML example · save as example.html and open in a browser

<img src="photo-640.jpg"
  srcset="photo-320.jpg 320w, photo-640.jpg 640w, photo-1280.jpg 1280w"
  sizes="(max-width: 700px) 100vw, 640px"
  width="1280" height="720" alt="A desk with a laptop and notebook."
  style="max-width:100%;height:auto">

How it works

The srcset numbers describe each file’s intrinsic width. Sizes describes the expected layout slot, not the image file itself. The browser combines that information with device conditions to choose a candidate; CSS still controls how the image is displayed.

Limits and mistakes

Provide actual assets whose dimensions match the descriptors. An inaccurate sizes value can waste bandwidth or produce a soft image. Responsive selection does not replace compression, meaningful alternative text, or layout testing.

Check the result

Use developer tools to inspect currentSrc at different viewport widths and pixel densities. Disable cache during controlled comparisons, and confirm the displayed image stays sharp without exceeding the layout.

Sources

Reference documentation for the behavior used in this guide: