srcset and sizes Explained: Responsive Images That Load Fast
How browsers choose an image from srcset, how to write a correct sizes value from your layout, how many widths to make, and when to use picture with AVIF.

The problem srcset solves
A photograph that looks crisp on a large desktop monitor might be 2400 pixels wide. On a phone the same image may occupy a slot 360 pixels wide. Sending the large file to the phone wastes several hundred kilobytes, slows the largest contentful paint and costs the visitor mobile data. Sending a small file to the desktop makes the image blurry. The browser could choose well, but it starts downloading images before the CSS that decides their size has arrived. srcset and sizes give it the information it needs in the HTML itself.
How the browser makes its choice
srcset lists the available files with their real pixel widths, written as 400w, 800w and so on. sizes describes how wide the image will be displayed at different viewport widths, as a list of media conditions and lengths, ending with a default. The browser evaluates sizes for the current viewport, multiplies by the device pixel ratio, and picks the smallest file that is at least that wide. An image shown at 361 CSS pixels on a 3x phone wants about 1083 pixels; with density capped at 2x in your width list it gets the file closest above 722.
Leaving out sizes is the most common mistake. The browser then assumes the image fills the whole viewport, so a small thumbnail column on desktop downloads the largest file. A sizes value that is too small causes the opposite problem: soft images on high-density screens.
Writing sizes from your layout
Work from your CSS breakpoints. For each range, ask how wide the image slot is. A full-width image inside page margins is calc(100vw - 32px). In a two-column grid it might be 50vw. On large screens the content often stops growing, so the slot becomes a fixed width such as 600px. Write the narrow rules first with max-width conditions and finish with the default. The value does not need to be pixel-perfect; within ten percent is fine, because the chosen file only changes at width steps.
Choosing how many widths
Each extra width is another file to generate, store and cache. A good method is to compute what common devices actually need and merge widths that are close together. The Responsive Image srcset and sizes Builder does this from your layout rules: it evaluates the slot on ten typical screens, multiplies by the density you serve, and merges values within a tolerance you choose. For many layouts three to five widths are enough. Capping density at 2x is a sensible default, since the difference at 3x is hard to see and the file is much larger.
Formats and the picture element
AVIF and WebP produce much smaller files than JPEG at similar quality. The picture element lets the browser pick the first format it supports: a source for AVIF, a source for WebP and an img with a JPEG fallback, all sharing the same sizes value. Always give the img width and height attributes so the browser reserves space and the layout does not jump. Mark images below the fold with loading lazy, and give the main hero image fetchpriority high, plus a preload link if it is discovered late.
Making the files and checking the result
If you have no image pipeline, the builder can resize your original in the browser into every width and format your browser can encode, packaged as a ZIP that follows your file pattern. For production sites a build step or image CDN is usually better, but the markup is the same.
Finally, test. Use the preview slider to see which file each screen should load, then open your page in browser developer tools with device emulation and check the network panel. If a phone downloads the largest file, your sizes value is too large or missing; if images look soft, raise the density or add a larger width.



