Measure the displayed image first
Open the page at a narrow and a wide viewport. Inspect the image and note its rendered width in each layout. A card in a three-column grid can be much narrower than the viewport, even if it fills the card.
Export a few useful widths
For a card photo, start with 400, 800, and 1200 pixel variants of the same crop. List them in srcset as photo-400.jpg 400w, photo-800.jpg 800w, and photo-1200.jpg 1200w. Use the actual pixel width in each descriptor. Keep one src URL as a fallback.
Describe the image slot
If the card image is roughly 360 CSS pixels wide from a viewport of 900 pixels onward and spans the viewport below that, a starting sizes value is (min-width: 900px) 360px, 100vw. Adjust that value for the padding and gaps in your real layout. The browser uses the slot estimate along with the screen density to select a candidate.
Verify the download and the layout
Use the Network panel with caching disabled and reload at two viewport widths. Check the downloaded filename and compare transfer sizes. Set width and height attributes that represent the image ratio so the browser can reserve space before the image loads.
Factor in screen density
A card image occupying 360 CSS pixels on a screen with a device pixel ratio of two can benefit from an image about 720 physical pixels wide. With 400w, 800w, and 1200w candidates, 800w is a reasonable candidate to expect. Check the actual selection in the browser because its choice can depend on additional conditions.
Read currentSrc and measure the transfer
Select the image element in your browser developer tools and inspect its currentSrc property to see the selected resource. In the Network panel, compare its transferred bytes with the original image. Start each lab run at the intended viewport and density with cache disabled so an earlier download cannot skew your comparison.
Use picture when the composition changes
Use srcset and sizes for different resolutions of the same crop. When a subject needs a tighter crop on small screens, add a picture element with a media-conditioned source for that crop and an img fallback. Check that the subject remains visible at the breakpoint and that the fallback alt text fits both compositions.
Comments