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 wide
  • images/tomato-soup.jpg — 1200 pixels wide
  • images/tomato-soup.webp — 1200 pixels wide, in the smaller WebP format

Several sizes: srcset and sizes

html
<!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.

The page "Tomato soup" with the drawing of the bowl of soup. It looks the same as the plain image; only the downloaded file differs.

Read it like this:

  • srcset lists the files and their real widths: 600w means "this file is 600 pixels wide".
  • sizes tells 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.
  • src is 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:

html
<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

  1. Select the srcset example and choose Try it.
  2. Remove sizes. Without it, the browser assumes the image fills the whole screen width.
  3. Wrap the <img> in <picture> with a WebP <source>, as in the second example.

Common mistakes

  • Forgetting sizes when you use w values in srcset. The browser may pick a file that is too large. Fix: add sizes.
  • Wrong widths in srcset, such as 800w for 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 alt on <source>. It is ignored. Fix: alt belongs on the <img>.

Check yourself

  1. In srcset, what does 1200w mean?
  2. Inside <picture>, which element holds alt, width and height?
  3. Is this lesson required for a working page?

Answers

  1. The file is 1200 pixels wide.
  2. The <img>.
  3. 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.

Practice project

a two-page personal site

Goal: build a small website of two pages that link to each other, with an image, using correct relative paths.

Your website after this module

Outcome: your pages become a website: a home page with a photo, and links that take a visitor to every page and back.

Your my-website folder: index.html (your new home page), about.html (your page from module 1), recipe.html and an images folder.

Own topic? Use a photo that fits it. To go further, also link recipe.html from your home page, so a visitor can reach all three pages.

Warm up first (optional): Profile Card with a Photo and Links and Table of Contents with Jump Links in Work Space, if it is in your menu. Work Space checks your code at once, so you know you are ready before you build.

What to do

In your my-website folder:

  1. Make sure you have an images folder with at least one image in it (for example the sample tomato-soup.jpg, or a photo of your own, renamed in lower case with no spaces).
  2. index.html — your home page. It needs:
    • the full skeleton, a clear <title>, one <h1>;
    • one image from images/, with a good alt, a width and a height;
    • a link to about.html with clear link text (not "click here");
    • a link to one other website, with the full https:// address.
  3. about.html — a page about you. It needs:
    • the full skeleton and its own <title>;
    • at least two sections with <h2> headings, each with an id;
    • at the top, a short "Jump to" line that links to both sections;
    • a link back to index.html.
  4. Open index.html in your browser from your folder and click every link. The image must show, and both pages must open.

Paste the code of both pages into the box below, one after the other. Put a comment before each, like <!-- index.html --> and <!-- about.html -->, then submit.

Checklist before you submit

  • No path starts with /.
  • All file and folder names are lower case, with no spaces.
  • The image shows, and its alt describes it.
  • Every link works when you open the pages from your folder.
  • Each id is unique on its page, and the jump links work.

Your mentor reviews every submission. The exercise counts as done when it is marked Passed.

Starter code

html
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>YOUR NAME — Home</title>
</head>
<body>
  <h1>YOUR HEADING</h1>
</body>
</html>

<!-- about.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>About YOUR NAME</title>
</head>
<body>
  <h1>About me</h1>
</body>
</html>

Build it in your own folder, on your computer. ZAM students hand this project in on their dashboard, and their mentor reviews it.

Chat with us on WhatsApp