Ch. 20 · Optimizing Web Images and Animations

한국어日本語
8 min

picture and srcset: The Right File for the Right Screen

Files in different formats are chosen with the source type in picture; files at different resolutions are chosen with srcset and sizes. Setting width and height and adding lazy loading also reduces layout shift and unnecessary downloads.

Even if you compress a file well, serving a file that doesn't fit the screen is wasteful. There's no reason to send a 2000px-wide photo to a phone that's 360px wide. The web solves this along two axes. Format is <picture> + type; resolution is srcset + sizes.

<source type=avif>Chosen if decodable <source type=webp>Next if the above fails <img src=jpg>Last-resort fallback Within the chosen source, srcset and sizes pick the resolution
The order in which the browser chooses. For format, the first one it can interpret from the top; for resolution, the srcset candidate that fits the screen

Choosing resolution: srcset and sizes

In srcset, list several sizes of the same picture in the form width + w, and in sizes, write the width this image will take up on screen. The browser looks at both together with the screen's pixel ratio (DPR) and picks a suitable candidate. Which candidate to pick is up to the browser and is not a rule guaranteed by the spec, so don't use it to force an exact choice.

The most basic form, choosing resolution only
<img
  src="photo-800.jpg"
  srcset="photo-480.jpg 480w, photo-800.jpg 800w, photo-1600.jpg 1600w"
  sizes="(max-width: 600px) 100vw, 800px"
  width="800" height="533"
  alt="Riverside scenery"
  loading="lazy" decoding="async">

Choosing format too: picture

Falling back AVIF → WebP → JPEG while also choosing resolution
<picture>
  <source type="image/avif"
          srcset="photo-480.avif 480w, photo-800.avif 800w, photo-1600.avif 1600w"
          sizes="(max-width: 600px) 100vw, 800px">
  <source type="image/webp"
          srcset="photo-480.webp 480w, photo-800.webp 800w, photo-1600.webp 1600w"
          sizes="(max-width: 600px) 100vw, 800px">
  <img src="photo-800.jpg"
       srcset="photo-480.jpg 480w, photo-800.jpg 800w, photo-1600.jpg 1600w"
       sizes="(max-width: 600px) 100vw, 800px"
       width="800" height="533" alt="Riverside scenery"
       loading="lazy" decoding="async">
</picture>
  • `<img>` is required and goes last. Attributes like style, alt, width/height, and loading go on the <img>. <picture> only handles the selection rules.
  • Write `type` exactly. AVIF is image/avif, WebP is image/webp, and JPEG XL is image/jxl. The browser skips a source whose type it can't interpret.
  • Set `width` and `height`. They reserve space before the image loads and are used to reduce the shifting of text after loading (layout shift).
  • Use `loading="lazy"` only for images below the first screen. The usual recommendation is not to lazy-load the hero image at the very top of the screen.
  • Write `alt`. It's a description for accessibility and search; for decorative images, use an empty alt="".
In the field: how many sizes should you make?

Slicing sizes too finely increases the number of files to produce and cache. First check the maximum width that actually appears on screen. If the content width is 800px, 1600px is for high-resolution (2x) screens and almost nothing beyond that is needed. Starting with three or four representative widths is easier to manage. Tools like the Next.js image component can do this for you, but you still have to check for yourself which attributes get output.

srcsetsrcset

An img/source attribute that lists several resolution (or pixel-ratio) candidates for the same image.

sizessizes

An attribute that tells the browser the width the image will take up on screen. Used to choose among srcset's w candidates.

Layout shiftLayout shift

Surrounding content getting pushed around because an image loads late. Reduced by reserving space with width and height.

Check yourself

Which describes the role of <img> inside <picture>?

Check yourself

What is the main reason to set the width and height attributes on <img>?