Choose responsive image sizes for website cards

Match width-based srcset candidates to a promo card’s actual CSS slot, then check the browser’s choice.

Asset preparation4 min read

A promo card can be nearly the width of a phone and only a third of a desktop layout. Sending the same large image to both wastes bytes; picking by viewport width alone misses the actual card width. Describe that width in sizes and offer the browser several versions of the same image in srcset.

Start with the card’s real layout

This example has 16 px outer margins, 24 px gaps and a content area capped at 1200 px. It shows one card per row below 600 px, two from 600 px and three from 1000 px. The image fills the card, with a separate live heading below it.

For a standalone test file, include <meta name="viewport" content="width=device-width, initial-scale=1"> in the head. The grid supplies the outer margins, so remove the default body margin.

CSS
body { margin: 0; }
.promo-grid {
  width: min(1200px, calc(100% - 32px));
  margin-inline: auto;
  display: grid;
  grid-template-columns: 1fr;
  gap: 24px;
}
@media (min-width: 600px) {
  .promo-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (min-width: 1000px) {
  .promo-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
.promo-card img {
  display: block;
  width: 100%;
  height: auto;
}

The grid must sit directly in a full-width parent for these sizes values to match. If it sits in a sidebar or a padded container, measure that layout and recalculate. The CSS controls the rendered width; sizes is a hint for choosing a file, not a layout rule.

Describe the slot and provide candidates

Export the same composition at 400, 640, 800 and 1200 pixels wide. The w number must equal each file’s actual intrinsic width. Substitute your own files and meaningful alt text; an image that only repeats the adjacent heading can use alt="".

To run a copy with the sample bike, download the four images: 400 px · 640 px · 800 px · 1200 px. Save them as promo-400.webp, promo-640.webp, promo-800.webp and promo-1200.webp in an images folder next to the HTML file.

HTML
<div class="promo-grid">
  <article class="promo-card">
    <img
      src="images/promo-800.webp"
      srcset="images/promo-400.webp 400w,
              images/promo-640.webp 640w,
              images/promo-800.webp 800w,
              images/promo-1200.webp 1200w"
      sizes="(min-width: 1232px) 384px,
             (min-width: 1000px) calc((100vw - 80px) / 3),
             (min-width: 600px) calc((100vw - 56px) / 2),
             calc(100vw - 32px)"
      width="1200" height="750"
      alt="Side view of a road bike with drop bars">
    <h2>Road bike</h2>
  </article>
</div>

The conditions are read in order; the first match wins. At 1232 px the content reaches its 1200 px cap, so each of three cards is (1200 − 2 × 24) ÷ 3 = 384 px. Below that cap, the three-column expression subtracts 32 px of margins and 48 px of gaps. The two-column expression subtracts 32 px of margins and one 24 px gap. The last expression is the one-column fallback. width and height describe the image’s aspect ratio so space can be reserved before it loads; they do not replace sizes.

Work through a viewport and density

At a 390 CSS-pixel viewport, the one-column slot is 390 − 32 = 358 CSS px. At device pixel ratio (DPR) 2, a useful target is about 358 × 2 = 716 image pixels, so 800w is a plausible choice. At 800 CSS px, the two-column slot is (800 − 56) ÷ 2 = 372 CSS px; at DPR 2 the target is 744 px, again near 800w. At 1440 CSS px the capped slot is 384 CSS px; at DPR 1 the 400w candidate is close.

These are estimates, not promises. The HTML Standard leaves the browser room to select a resource: density, zoom and an already available image can affect the result. If the largest candidate is smaller than the target, the browser can still use it; inspect whether the resulting detail is acceptable. Add widths where measurements reveal a useful gap, rather than assuming every device needs an exact match.

Check the file the browser chose

Download the card exercise PDF and save its attached ZIP using a PDF viewer that supports attachments. Extract the files together and open responsive-image-sizes-sample.html in a browser to see the current slot width, density and selected file. To check your own page, select the image in DevTools and run:

JavaScript
({ slot: $0.getBoundingClientRect().width,
   dpr: window.devicePixelRatio,
   selected: $0.currentSrc })

$0 refers to the currently selected element in DevTools. Compare slot with the matching sizes expression, then inspect selected and the image’s transferred bytes in the Network panel. currentSrc reports the selected URL, not whether the download succeeded. If the slot differs, fix the expression or its assumptions before adding more files.

Test a fresh navigation with the Network panel’s cache disabled as well as a normal repeat visit. A browser may reuse a larger cached resource after resizing, so resizing alone is a poor test of what a fresh visitor receives. Check the image at the actual displayed size for softness or compression artifacts. The MDN responsive images guide covers the selection syntax and cache caveat.

This technique switches resolution for the same composition. If a narrow card needs a genuinely different crop or subject placement, use art direction with picture and source, and check each crop separately.

Image tools
All guides