- Published on
- 2 min read
Serve the right image for the space it occupies
- Authors
- Name
- Ritenta
A phone does not need to download a desktop-sized photograph merely to display it at 320 CSS pixels. Yet choosing a small file for every device can look soft on a dense screen. Responsive images let the browser choose an appropriate resource while the page keeps one meaningful image. MDN's responsive-images guide explains the selection rules.
First decide what changes
If the same crop appears at different sizes, provide width candidates with srcset and tell the browser the image's expected display width with sizes:
<img
src="lesson-960.webp"
srcset="lesson-480.webp 480w, lesson-960.webp 960w, lesson-1440.webp 1440w"
sizes="(max-width: 640px) calc(100vw - 32px), 720px"
width="960" height="540"
alt="A learner comparing two layout options">
The w values describe the source files' intrinsic widths. sizes describes the slot in the layout, not the file size. The browser combines that slot with device pixel density and may choose a candidate you did not expect from viewport width alone. Keep src as a fallback and include intrinsic dimensions to reserve space.
If the composition changes, such as switching from a wide group photo to a portrait crop that keeps the subject visible, use <picture> with media-specific sources and a final <img>. That is art direction, a different problem from choosing resolution.
Verify the actual request
Open DevTools Network at several viewport widths and pixel densities. Check the downloaded image URL and byte size, then inspect whether the crop and alt text still communicate the intended content. Do not mark an important first-screen image as lazy solely because it is large; delaying discovery can worsen its appearance time. Optimize the asset and its loading priority according to measurements.