Sub topic 13
Responsive images: srcset, sizes, and picture
This chapter assumes you know how to use img with src, alt, width, and height, and that you can read relative file paths. Responsive images let the browser download a file that fits the visitor's screen instead of one fixed file for everyone.
The problem with a single src
A single src forces every visitor to download the same file. A phone on a mobile connection gets the same 1600-pixel image as a large desktop monitor. That wastes data and slows the page. HTML gives you two tools to fix this:
srcsetandsizesoffer several sizes of the same image. The browser picks one.pictureoffers different formats or crops. The browser picks the first one it supports.
Resolution switching with srcset and sizes
Add srcset to list candidate files. Each entry has a URL and a width descriptor such as 400w, which is the file's real width in pixels. Add sizes to tell the browser how wide the image will be on the page.
<img
src="images/hero-800.jpg"
srcset="images/hero-400.jpg 400w, images/hero-800.jpg 800w, images/hero-1600.jpg 1600w"
sizes="(min-width: 900px) 800px, 100vw"
alt="Mountain lake at sunrise"
width="800"
height="450">Read sizes from left to right. The first condition that matches wins, and the last entry is the default. Here, on a viewport 900 pixels or wider, the image displays 800 pixels wide. Otherwise it fills the full viewport width (100vw).
The browser multiplies the displayed width by the screen's pixel density, then chooses the smallest file that is large enough. A 400-pixel-wide phone with a 2x screen needs about 800 pixels, so it picks hero-800.jpg.
The src attribute stays as the fallback for browsers that do not understand srcset. Keep alt, width, and height as usual, so the browser can reserve space before the file loads.
Format switching with picture
Use picture when you want to offer newer formats with a safe fallback. Each source has a type. The browser uses the first source whose type it supports. If none match, it uses the img element.
<picture>
<source srcset="images/photo.avif" type="image/avif">
<source srcset="images/photo.webp" type="image/webp">
<img src="images/photo.jpg" alt="Street market at dusk" width="800" height="533">
</picture>The img is required. It carries the alt text and the dimensions, and source elements do not replace it.
Common errors
- Writing
sizeswithoutsrcset. It has no effect alone. - Using
wdescriptors but omittingsizes. The browser then assumes the image is100vw, which can pick a file that is too large. - Placing
alton asourceelement. Put it on theimg. - Listing
sourceelements with the most widely supported format first. The browser stops at the first match, so put the newest format first. - Giving the
wdescriptor a value that does not match the file's real width.
Exercise
A page shows an image 600 pixels wide on screens 700 pixels or wider and full width on narrower screens. You have files at 300, 600, and 1200 pixels wide named card-300.jpg, card-600.jpg, and card-1200.jpg. Write the srcset and sizes attributes.
Show answer
srcset="card-300.jpg 300w, card-600.jpg 600w, card-1200.jpg 1200w" sizes="(min-width: 700px) 600px, 100vw"
Exercise
In a picture element, why must the AVIF source come before the JPEG img, and what happens if the browser supports neither AVIF nor WebP?
Show answer
The browser uses the first source it supports and ignores the rest, so the preferred format must come first. If it supports neither, it falls back to the img element's src, which is the JPEG.