Sub topic 11
Responsive images with srcset and sizes
This chapter assumes you know basic HTML, the img element, and the difference between CSS pixels and device pixels from the viewport chapter. It also assumes you know that fluid layouts let an image's displayed width change with the container.
Why one image file is not enough
A fluid layout changes how wide an image is displayed. A hero image might fill a 360 px phone screen and sit in a 1200 px column on a desktop. If you serve the 1200 px file to everyone, phone users download far more data than they can show. If you serve the 480 px file to everyone, desktop users see a blurry image.
The fix is to offer the browser several files and let it choose. The browser knows the screen's pixel density, the layout width, and often the network conditions. You do not know these when you write the HTML.
Giving the browser choices with srcset
The srcset attribute lists candidate files. Each entry has a URL and a width descriptor, written as a number followed by w. The descriptor is the file's real width in pixels, not the width it is displayed at.
The sizes attribute tells the browser how wide the image will be displayed. The browser needs this because it picks an image before the CSS has finished loading, so it cannot measure the image itself.
<img
src="hero-800.jpg"
srcset="hero-480.jpg 480w, hero-800.jpg 800w, hero-1200.jpg 1200w"
sizes="(min-width: 80em) 1200px, 100vw"
alt="A team reviewing a layout on three screen sizes">Read sizes from left to right. The browser uses the first condition that matches. Here, at a viewport of 80em or wider, the image is displayed 1200 px wide. Otherwise it fills 100% of the viewport width (100vw). The last entry has no condition and works as the default.
The src attribute stays as a fallback for browsers that do not support srcset. The alt text is still required.
How the browser chooses
The browser multiplies the displayed width from sizes by the device pixel ratio, then picks the smallest file that is at least that wide.
| Viewport | Pixel ratio | Displayed width | Needed pixels | File chosen |
|---|---|---|---|---|
| 360 px | 1 | 360 px | 360 | hero-480.jpg |
| 360 px | 3 | 360 px | 1080 | hero-1200.jpg |
| 1400 px | 1 | 1200 px | 1200 | hero-1200.jpg |
The second row shows why a small phone can still download the large file. Its screen is narrow in CSS pixels but dense in device pixels.
Matching sizes to your layout
The sizes value must describe the real layout. If it is wrong, the browser picks the wrong file, and nothing warns you.
Suppose a card image fills one column of a three-column grid at desktop widths, with 1 rem gaps. The image takes about a third of the page. You could write:
<img
src="card-400.jpg"
srcset="card-400.jpg 400w, card-800.jpg 800w"
sizes="(min-width: 80em) 400px, (min-width: 48em) 50vw, 100vw"
alt="Product photo">Use the same breakpoints in sizes that your CSS uses. When you change a breakpoint, update sizes too.
Common errors
- Omitting
sizes. The browser then assumes100vw, which over-downloads for any image smaller than the full width. - Using
xandwdescriptors together. Pick one style persrcset. Width descriptors withsizessuit fluid images. Density descriptors such as2xsuit fixed-size images like logos. - Wrong descriptor values. The number must match the file's actual pixel width.
- Layout shift. Add
widthandheightattributes, or a CSSaspect-ratio, so the page reserves space before the image loads.
Exercise
A card image is displayed 50% of the viewport width on screens 48em and wider, and full width below that. Write the sizes attribute. Then say which file a 600 px wide viewport with a pixel ratio of 2 needs if the files are 400w, 800w, and 1200w.
Show answer
sizes="(min-width: 48em) 50vw, 100vw". At 600 px (under 48em, assuming 48em is about 768 px) the image is 100vw, so 600 CSS pixels. Multiplied by a ratio of 2, the browser needs 1200 pixels, so it picks the 1200w file.