Sub topic 15
Responsive images with srcset and picture
This chapter assumes you can export images at 1x and 2x, know the main formats (JPEG, PNG, WebP, AVIF), and can read basic HTML. It covers how the browser chooses among the files you export.
Why one file is not enough
A phone on a slow connection and a large monitor need different files. If you ship only the largest export, small screens download pixels they never show. If you ship only a small export, large and high-density screens look soft. The srcset attribute and the picture element let you offer several files and let the browser choose.
Density descriptors: same size, sharper pixels
Use density descriptors (1x, 2x) when an image has a fixed displayed size, such as a logo.
<img
src="logo.png"
srcset="logo.png 1x, [email protected] 2x"
width="120"
height="40"
alt="Acme logo">The width and height attributes give the displayed size in CSS pixels. The browser picks logo.png or the double-size [email protected] based on screen density. The src file is the fallback for browsers that ignore srcset.
Width descriptors: different sizes per layout
Use width descriptors (w) when the image changes size with the layout, such as a hero image.
<img
src="hero-800.jpg"
srcset="hero-400.jpg 400w, hero-800.jpg 800w, hero-1600.jpg 1600w"
sizes="(min-width: 960px) 800px, 100vw"
width="800"
height="450"
alt="Team working at a shared desk">Each w value is the file's real pixel width. The sizes attribute says how wide the image will be displayed. Here it is 800 CSS pixels on screens 960 pixels or wider, and the full viewport width otherwise. The browser combines that width with the screen density and picks the smallest file that still looks sharp. A 400-pixel-wide phone at 2x needs about 800 pixels, so it picks hero-800.jpg.
You cannot mix x and w descriptors in one srcset.
The picture element: choosing by format
Use picture to offer different formats. The browser uses the first source whose format it supports and falls back to the JPEG in the img element.
<picture>
<source type="image/avif" srcset="hero.avif">
<source type="image/webp" srcset="hero.webp">
<img src="hero.jpg" width="800" height="450" alt="Team working at a shared desk">
</picture>List the smallest, newest format first. The img element is required. It carries the alt text and the dimensions, and it is the fallback.
Common errors
- Missing
sizes. Without it, the browser assumes the image fills the viewport width and may download a file that is too large. - Wrong
wvalues. The number must match the file's actual pixel width, not the width you want it shown at. - No
widthandheight. The page can jump as the image loads. Set them to the image's aspect ratio. - Exporting only the largest file. Export every size you list in
srcset.
Exercise
A card image is displayed 300 CSS pixels wide on every screen. You have card-300.jpg (300 px wide) and card-600.jpg (600 px wide). Write the img attributes that let high-density screens get the sharper file, and say which descriptor type you used and why.
Show answer
<img src="card-300.jpg" srcset="card-300.jpg 1x, card-600.jpg 2x" width="300" height="200" alt="..."> (the height value should match your image's aspect ratio). Density descriptors (1x, 2x) fit because the displayed size is fixed, so only the screen density changes which file is best.
Exercise
Why must the img element stay inside picture even when every source is listed?
Show answer
The img element holds the fallback file, the alt text, and the width and height. Browsers that do not support picture, or any listed format, use it, and without it nothing is displayed.