2.7 Serve images at display size

Do not send desktop pixels to a phone.

Ensure to serve images that are close to your display size

A phone with a 400px-wide viewport has no use for a 2000px-wide image. Sending one wastes bandwidth, decode time and memory — and on a slow connection it can be the single thing delaying LCP.

srcset and sizes

srcset lists the available widths; sizes tells the browser how wide the image will actually render, so it can choose before layout is computed:

<img
  src="/photo-800.jpg"
  srcset="/photo-400.jpg 400w, /photo-800.jpg 800w, /photo-1600.jpg 1600w"
  sizes="(max-width: 600px) 100vw, 50vw"
  alt="A harbour at dusk"
  width="800"
  height="600"
/>

The browser accounts for device pixel ratio itself — a 2× phone at 400 CSS pixels picks the 800w file. Do not handle that manually.

sizes is the part people get wrong. If it does not match the CSS, the browser picks badly. Wrong in the generous direction just means a larger file; wrong the other way means a blurry image.

Art direction

srcset serves the same image at different sizes. When you need a different crop — a wide hero on desktop, square on mobile — use <picture> with media:

<picture>
  <source media="(max-width: 600px)" srcset="/hero-square.jpg" />
  <img src="/hero-wide.jpg" alt="" width="1600" height="600" />
</picture>

Let a tool do it

Generating four sizes of every image by hand is not sustainable. Framework image components and image CDNs generate the whole srcset from one source file. This is the main reason to use one.

On this page