Chapter 11 of 15

Responsive Images: srcset, picture and Fast Loading

Big images are the main reason websites load slowly. Let us send the right size image to every device.

The basic rule

CSS
img {
  max-width: 100%;
  height: auto;
}

This stops images from being wider than the screen. Put it in every stylesheet.

Different sizes with srcset

A phone does not need a 2000px-wide photo. Make the same image in a few sizes and let the browser pick.

Three sizes of the same photo; the small one goes to a phone, the medium one to a laptop
With srcset a phone gets the small file and a laptop gets the bigger one.
HTML
<img
  src="lake-800.jpg"
  srcset="lake-400.jpg 400w,
          lake-800.jpg 800w,
          lake-1600.jpg 1600w"
  sizes="(min-width: 900px) 800px, 100vw"
  alt="Blue lake with mountains behind"
  width="800" height="533">
  • srcset lists the files and their real width (w).
  • sizes tells the browser how wide the image will show: "800px on big screens, full screen width on small screens".
  • The browser then downloads only the best file. This saves mobile data.

Modern formats with picture

HTML
<picture>
  <source srcset="lake.avif" type="image/avif">
  <source srcset="lake.webp" type="image/webp">
  <img src="lake.jpg" alt="Blue lake with mountains behind" width="800" height="533">
</picture>

The browser uses the first format it understands. Old browsers fall back to the JPG. AVIF and WebP files are often half the size of JPG.

Three boxes avif, webp, jpg with arrows showing the browser falling back from one to the next
<picture> lets the browser pick the best format it understands.

A different crop for phones

A wide landscape photo can look tiny on a narrow phone. With <picture> and media, you can send a closer, taller crop to small screens:

HTML
<picture>
  <source media="(max-width: 600px)" srcset="team-square.jpg">
  <img src="team-wide.jpg" alt="Our team of five in the office" width="1200" height="500">
</picture>

This is called art direction. Use it for big banner images where the important part, like a face, would become too small.

Lazy loading

HTML
<img src="photo.jpg" alt="..." loading="lazy" width="600" height="400">

loading="lazy" loads the image only when the user scrolls near it. Use it for images lower on the page. Do not use it for the big image at the top, because that one should load at once.

A long page: images on screen are loaded, images further down show not loaded yet
Lazy images below the screen wait until the visitor scrolls down. The page opens faster.

Crop images neatly with object-fit

The same picture with object-fit fill (stretched), contain (with gaps) and cover (cropped)
The same wide photo in a tall box. cover is usually what you want for cards and avatars.
CSS
.avatar {
  width: 120px;
  height: 120px;
  object-fit: cover;     /* fill the box, crop extra */
  border-radius: 50%;    /* round */
}
Speed helps SEOGoogle measures page speed (Core Web Vitals). Small images, width/height attributes and lazy loading all help your ranking.

Keep the shape with aspect-ratio

CSS
.thumb {
  width: 100%;
  aspect-ratio: 16 / 9;   /* always a video shape */
  object-fit: cover;
}

aspect-ratio keeps a box the same shape at any width. Photos of different sizes now line up neatly in a grid. It also reserves the space before the image loads, so the page does not jump.

Background images

CSS
.banner {
  background-image: url("banner.webp");
  background-size: cover;
  background-position: center;
  min-height: 300px;
}

Use background images only for decoration. Google and screen readers cannot see them, and you cannot add alt text. If the picture means something, use a real <img>.

How to make the different sizes

You need the same photo in a few widths, for example 400px, 800px and 1600px. Free ways to make them:

  • Squoosh (squoosh.app): open the photo, set the width, choose WebP, download. Repeat for each size.
  • Most image editors have Export or Save for web with a size option.
  • Some hosting services and image CDNs can make sizes automatically from one upload.

Name the files clearly with the width: lake-400.webp, lake-800.webp.

Mini project: a fast product image

HTML
<picture>
  <source srcset="shoe-400.webp 400w, shoe-800.webp 800w" type="image/webp">
  <img
    src="shoe-800.jpg"
    srcset="shoe-400.jpg 400w, shoe-800.jpg 800w"
    sizes="(min-width: 700px) 400px, 100vw"
    alt="Blue running shoe, side view"
    width="800" height="600"
    loading="lazy">
</picture>

This one image uses everything from the chapter: modern format, the right size for each screen, real width and height, lazy loading and clear alt text. Copy this pattern for every important image.

Image checklist

  • ☐ The file is resized and compressed (most photos under 200 KB)
  • ☐ WebP or AVIF is offered, with JPG as backup
  • ☐ width and height are set
  • ☐ alt text describes the image (or is empty for pure decoration)
  • ☐ loading="lazy" on images below the first screen, not on the top image
  • ☐ The file name describes the picture: blue-running-shoe.jpg
Image SEOGoogle Images is a big source of visitors. Clear file names, good alt text and text near the image that talks about it all help your photos appear in image search.

Practice time

  1. Add max-width: 100% for all images.
  2. Make 3 sizes of one photo and use srcset.
  3. Add loading="lazy" to images below the first screen.
  4. Make a 400px and 800px version of one photo and use them with srcset.
  5. Use aspect-ratio to make a row of photos all the same shape.

Quick summary

  • max-width: 100% keeps images inside the screen.
  • srcset + sizes send the right size file.
  • <picture> serves modern formats; loading="lazy" saves data.