All guides
Guide

How to Serve Responsive Images with srcset, sizes and AVIF

Write a sizes attribute that matches your layout, pick widths from it, serve AVIF and WebP with a JPEG fallback, crop for phones, and set width, height, lazy loading and fetchpriority — with the markup and what each screen downloads.

Fastest way

Ask your AI to do it

Paste this into the AI agent you already use:

Help me use a Skill with another agent. Ask which agents and package I use, check supported locations and tools, identify any assumptions to change, and verify the workflow with a small example.

Quick answer

Give every content image a srcset of several widths and a sizes attribute that says how wide it is shown at each breakpoint; the browser then downloads the smallest file that is sharp on that screen. Wrap it in a picture element with AVIF and WebP sources and a JPEG img, set width and height so the page does not jump, lazy-load everything below the fold and give the hero fetchpriority="high".

Write sizes from your layout

sizes lists media conditions with the width the image takes, ending with a default. A three-column card grid that becomes two columns on tablets and one on phones might be (min-width: 1100px) 340px, (min-width: 700px) 45vw, 92vw. Leaving sizes out means 100vw, so browsers fetch files several times larger than a card needs.

<img src="card-800.jpg"
     srcset="card-400.jpg 400w, card-800.jpg 800w, card-1200.jpg 1200w"
     sizes="(min-width: 1100px) 340px, (min-width: 700px) 45vw, 92vw"
     width="1200" height="1200" alt="Freshly roasted coffee beans" loading="lazy" decoding="async">

Choose the widths

  • Multiply the slot width by the screen's pixel ratio for common screens: a 393 px phone at 3× needs about 1180 px for a full-width image.
  • Merge widths that are within about 25% of each other; each extra width is another file to store and cache.
  • Never upscale: if the source is smaller than the largest screen needs, those screens get a softer image.

AVIF, WebP and JPEG

AVIF is usually the smallest at the same visual quality, WebP is close behind, and JPEG is the fallback every browser shows. List sources best first; the browser takes the first type it supports. Grainy photos gain less than smooth ones, so compare sizes per image rather than assuming a fixed saving.

Crop for phones

A wide hero often loses its subject on a tall phone screen. Add a source with a media condition such as (max-width: 640px) that points to a taller crop kept on the subject; the browser switches crops by screen width.

The hero image and Core Web Vitals

  • Do not lazy-load the largest image above the fold; give it fetchpriority="high" and, if it is set in CSS or late in the HTML, a preload with imagesrcset and imagesizes.
  • width and height reserve the space before the file arrives, which prevents layout shift (CLS).
  • Strip EXIF metadata: phone photos can carry GPS location and camera serial numbers.

Do it with an AI agent

PicturePress works out the widths from your sizes attribute, crops for phones around a focus point, renders AVIF, WebP and JPEG with quality tuned per photo, strips metadata, and writes the picture markup for HTML and React with a page showing which file each screen downloads.

All guides