Lesson 21 of 51 · Module 3, lesson 6 of 6 · 4 min
Responsive images (optional)
Offer smaller image files to phones and modern formats with a safe fallback.
What you'll learn
- How to offer the same image in several sizes with
srcset- How to offer a modern image format with a safe fallback, using
<picture>- When this is worth doing
This lesson is optional. Your pages work without it. Come back to it when your site has large photos.
Why this matters
A photo made for a large desktop screen can be ten times bigger than a phone needs. On a slow or expensive mobile connection, that is a real cost for your visitor. HTML lets you offer several files and let the browser choose.
The sample files
This lesson uses three sample files. In the Try code playground they are already available:
images/tomato-soup-small.jpg— 600 pixels wideimages/tomato-soup.jpg— 1200 pixels wideimages/tomato-soup.webp— 1200 pixels wide, in the smaller WebP format
Several sizes: srcset and sizes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Soup photo</title>
</head>
<body>
<h1>Tomato soup</h1>
<img
src="images/tomato-soup.jpg"
srcset="images/tomato-soup-small.jpg 600w,
images/tomato-soup.jpg 1200w"
sizes="(max-width: 300px) 100vw, 300px"
alt="A white bowl of hot tomato soup"
width="300" height="200">
</body>
</html>
What you'll see: the same picture as before. The difference is invisible — it is which file your browser downloads.
Read it like this:
srcsetlists the files and their real widths:600wmeans "this file is 600 pixels wide".sizestells the browser how wide the image will be on the page: "on screens up to 300 pixels wide, the full screen width (100vw); otherwise 300 pixels".- The browser does the maths, including the screen's sharpness, and downloads one file. A sharp phone screen shows 300 pixels with two or three real dots each, so it may choose the 600- or 1200-pixel file.
srcis the fallback for very old browsers.
A modern format with a fallback: <picture>
WebP files are smaller than JPEG. Every current browser supports them, but <picture> lets you offer them safely:
<picture>
<source srcset="images/tomato-soup.webp" type="image/webp">
<img src="images/tomato-soup.jpg" alt="A white bowl of hot tomato soup" width="300" height="200">
</picture>
The browser reads the <source> elements in order and uses the first one it supports. If none fits, it uses the <img>. The <img> must always be there — the alt, width and height stay on it.
<picture> can also show a different crop on small screens (for example, a close-up instead of a wide photo), using a media attribute on <source>. You will rarely need that.
How to see which file loaded
Open your page in Chrome or Edge, open the developer tools, and choose the Network tab. Refresh the page. The list shows the image file that was downloaded. Now use the device toolbar (the phone icon at the top left of the developer tools) to pretend to be a phone, refresh, and check again.
Try it yourself
- Select the
srcsetexample and choose Try it. - Remove
sizes. Without it, the browser assumes the image fills the whole screen width. - Wrap the
<img>in<picture>with a WebP<source>, as in the second example.
Common mistakes
- Forgetting
sizeswhen you usewvalues insrcset. The browser may pick a file that is too large. Fix: addsizes. - Wrong widths in
srcset, such as800wfor a 600-pixel file. The browser picks badly. Fix: write each file's real width. - No
<img>inside<picture>. Nothing shows at all. Fix:<img>is always the last child. - Putting
alton<source>. It is ignored. Fix:altbelongs on the<img>.
Check yourself
- In
srcset, what does1200wmean? - Inside
<picture>, which element holdsalt,widthandheight? - Is this lesson required for a working page?
Answers
- The file is 1200 pixels wide.
- The
<img>. - No. It makes pages lighter on phones, but a plain
<img>works everywhere.
Next: that completes links and images. Build a small two-page website in the practice exercise.
a two-page personal site
Starter code
Build it in your own folder, on your computer. ZAM students hand this project in on their dashboard, and their mentor reviews it.

