A single image file has to satisfy a phone on a slow connection and a high density desktop display at once, and it cannot do both. Either it is large enough for the desktop and wasteful on the phone, or small enough for the phone and blurry on the desktop. The srcset attribute lets you offer several files and hand the choice to the browser.
Width descriptors
The common form lists each file with its actual pixel width, marked with a lowercase w.
<img src="/img/photo-800.jpg" srcset="/img/photo-400.jpg 400w, /img/photo-800.jpg 800w, /img/photo-1600.jpg 1600w" sizes="(max-width: 600px) 100vw, 800px" alt="A harbour at dusk" width="800" height="450"> The descriptor is the intrinsic width of the file, not the size it will be displayed at. This trips people up constantly. You are describing the files you have, and the browser works out which is appropriate.
The plain source attribute remains as a fallback for anything that does not understand srcset, and it should point at a sensible middle option rather than the largest file.
Why sizes is required
Here is the part that is genuinely counterintuitive. The browser decides which image to download before it has applied any CSS, because images are requested during parsing while the stylesheet may still be loading. At that moment it has no idea how large the image will be displayed.
The sizes attribute supplies that information in advance. It is a list of media conditions with the width the image will occupy in each case, and the browser takes the first condition that matches.
In the example above, the image fills the full viewport width below six hundred pixels and is eight hundred pixels wide otherwise. Omit sizes and the browser assumes the image fills the entire viewport width, which usually means downloading a file far larger than necessary and undoing the point of the exercise.
Sizes must match your CSS
The catch is that this information duplicates what your stylesheet already knows, and nothing keeps the two in sync. Change a layout breakpoint and every sizes attribute describing that layout becomes wrong, silently, with the only symptom being images that are slightly too large or too small.
On a site with a consistent layout this is manageable. On one with many different image contexts it is a real maintenance burden, and it is the main reason people reach for a component or a template that generates the attribute from a single definition.
Pixel density descriptors
A simpler form exists for images with a fixed display size, using an x descriptor to indicate screen density.
<img src="/img/logo.png" srcset="/img/logo.png 1x, /img/logo@2x.png 2x" alt="Coding Tutorials" width="200" height="40"> No sizes attribute is needed here, because the display size does not vary. This is the right form for logos, avatars and icons that always render at the same dimensions, and the wrong form for anything whose width depends on the viewport.
The two descriptor types cannot be mixed within one srcset.
The browser has the final say
Srcset is a hint rather than an instruction. The browser may factor in connection speed, data saving preferences and whether it already has a larger version cached, in which case it will often use that rather than downloading a smaller one. This is deliberate and generally produces better outcomes than a rigid rule would.
How many sizes to generate
Three or four widths covers most cases without producing an unmanageable number of files. A common set doubles from around four hundred pixels up to sixteen hundred, which spans phones through to high density desktops.
Generating these by hand does not scale, so this is a job for a build step or an image service that produces the variants automatically from one original. Once that is in place the marginal cost of another size is nothing, and a template can emit the full attribute without anyone thinking about it.
Checking it is working
The network panel in your browser tools shows which candidate was actually requested, which is the only reliable way to confirm the attribute is doing what you intended. Resize the window, reload, and watch which file is fetched.
Common mistakes
Three errors account for most problems. Using the display width rather than the file width in a descriptor produces selections that are consistently wrong. Omitting the sizes attribute means the browser assumes full viewport width and downloads too much. And listing files that do not all share the same aspect ratio breaks the space reservation, because the width and height attributes describe only the fallback.
Do not forget dimensions
Width and height attributes still belong on the element, using the dimensions of the fallback file. The browser uses them to reserve space and prevent layout shift, and the aspect ratio holds regardless of which candidate is eventually chosen, provided all your variants share that ratio.