PERFORMANCE6 MIN READ

How to Use Srcset and Sizes for Responsive Images

Published

Using srcset and sizes for responsive images comes down to giving the browser a list of candidate files and telling it how wide the image will display, so it can pick the smallest file that still looks sharp on that device. The srcset attribute lists the files and their widths; sizes describes the layout slot the browser should match against.

Why should you use srcset and sizes for images?

Sending one large master image to every visitor wastes bandwidth on phones and small tablets, and it can slow down the moment the browser paints the largest visible element on the page. Smaller image bytes can help that metric, but they do not shorten server response time or guarantee any particular search ranking. Responsive markup lets you supply several versions of the same image and leave the selection to the browser, which already knows the viewport width and pixel density of the device it is running on.

How does the srcset attribute work?

The srcset attribute is a comma separated list of image files, each followed by its own width in pixels using a w descriptor, for example a 480w or 960w file. The browser matches these widths against the viewport and the screen's pixel density, then requests the file it judges wide enough to fill the slot without visible softness. This selection logic is already built into how browsers handle responsive images, so no extra script is needed to switch sources.

The width descriptor describes the real width of the image file in pixels, not how large the image appears on screen. A file labeled 1200w is 1200 pixels wide regardless of the CSS width applied to it later. Confusing the two is a common source of oversized downloads: pairing a correctly labeled large file with a sizes value that under-reports the display width still sends more data than the layout needs.

What is the role of the sizes attribute?

srcset alone only tells the browser the available widths; sizes tells it how wide the image slot will actually be once the page is laid out. Because the browser reads sizes before it has finished calculating layout, this attribute speeds up the file choice rather than just describing the final appearance.

sizes values must use absolute lengths or viewport-relative units such as vw; percentages are not valid in this attribute, even though percentages are common in CSS width rules. A typical value looks like 100vw for a full-width mobile image, or 50vw for a column that takes half the viewport on larger screens.

sizes can list several conditions, each paired with a width, read in order. The browser stops at the first condition that evaluates to true and ignores every condition after it, even if a later one would also match more precisely. List the narrowest or most specific condition first, then finish with a default width that carries no condition.

How to use srcset and sizes for responsive images

The src attribute still belongs on the img element because it is the mandatory fallback for browsers that do not support srcset and sizes. Without it, older browsers and some non-browser consumers of the markup have nothing to load.

Illustrative example, not a measured test: a card component spans the full viewport width on phones and one third of a three column grid on desktop. Candidate files are prepared at 320, 640, 960 and 1280 pixels wide. The sizes attribute reads: max-width 600px gives 100vw, max-width 1200px gives 50vw, otherwise 33vw. The fallback src points at the 960 pixel file, so non-supporting browsers still receive a reasonably sized image rather than the largest master file.

Deciding on the actual width candidates to generate is a separate step from writing the markup; it depends on the real column widths and gutters in your grid, not round numbers chosen for convenience.

Common mistakes when implementing responsive images

  • →Listing only one or two widths, which gives the browser nothing meaningful to choose between.
  • →Writing a sizes value that assumes full width on every breakpoint when the component is actually narrower, cancelling the benefit of the smaller candidates.
  • →Omitting the fallback src and leaving unsupported consumers without any image.
  • →Using percentage values in sizes, which the attribute does not accept.
  • →Placing a broad media condition before a narrower one, so the broad condition matches first and the narrower rule never runs.
  • →Lazy-loading the image that is also the largest contentful paint candidate, which delays its discovery instead of speeding it up.

These mistakes are easy to introduce when a sizes value is copied from one component to another without checking the new layout. Treat sizes as part of the component's own styling, not a one-time setting written once and forgotten.

How to test if your responsive images are working

Open the page in browser developer tools, select the image element, and read its currentSrc property in the console; this reports the exact file the browser chose, separate from the src attribute written in the markup. Resize the viewport and reload to see the selection change at different widths, then confirm in the network panel that the requested file size matches the chosen candidate.

Check the result at more than one pixel density if you can, since a high density display may request a numerically larger file for the same layout width. Also confirm the image occupies its reserved space before and after loading; cumulative layout shift needs that reserved space regardless of how well the source selection works.

This markup controls which file downloads; it does not control how the browser decodes or displays the format inside that file. A browser accepting a width candidate in a newer format does not confirm every tool in your pipeline can encode that format, and raster re-encoding during resizing typically drops metadata that a pass-through file would keep. Verify the chosen file visually and check its dimensions rather than assuming the markup alone guarantees a correct result.

Next step: pick one page with a large hero or card image, add two or three width candidates and a sizes value that matches its real column width, then check currentSrc at a phone-width viewport and a desktop viewport before moving on to other pages.

Ready to optimize your images?

TRY ZIPMYIMAGES FREE →

Related guides