How should the sizes attribute match the actual image layout?

The sizes attribute should describe the image’s rendered slot width at the same viewport conditions used by the CSS layout. It does not set the visual width. Sizes is an early loading hint, so an inaccurate value causes the browser to choose an inefficient candidate.
If a card is full width on mobile, half width on tablets and one-third width on desktop, sizes should approximate those slots after subtracting gutters. List conditions from first match to fallback and use the actual content breakpoints. Generic device widths may not match the layout.
<img
src="card-960.jpg"
srcset="card-400.jpg 400w, card-800.jpg 800w, card-1200.jpg 1200w"
sizes="(max-width: 40rem) calc(100vw – 2rem), (max-width: 64rem) 50vw, 33vw"
alt="Product shown from the front">
Account for maximum container widths. An image in a centered 1200-pixel layout should stop growing after the container reaches its maximum, even on a 2500-pixel screen. A final fixed or calc-based slot can prevent unnecessarily large downloads at ultrawide viewport sizes.
Compare sizes with the computed image width at representative viewports, then inspect currentSrc in a fresh session. Update the attribute when CSS Grid tracks, gutters or breakpoints change. Keep the logic near the component so layout and loading hints do not drift in separate templates. Add this case to responsive regression tests with long content, browser zoom and at least one narrow embedded container. Record the expected wrapping and focus behavior so later component changes can be checked against a known result.