Responsive images: srcset, sizes, picture and lazy loading
Serve the right image to every screen with srcset, sizes, the picture element, modern formats and lazy loading.
In this lesson you will
- Prevent layout shift with width and height
- Write srcset and sizes
- Use picture for crops and formats
- Lazy-load the right images
About 14 minutes
One image, a hundred screens
Priya’s homepage photo is a beautiful 3000 pixel wide picture of 2 MB. On her big monitor it looks stunning. On a visitor’s phone on a slow connection, the page crawls, because the phone downloads the whole giant file only to display it 390 pixels wide. The fix is to give the browser a menu of image sizes and let it choose. That is what responsive images do.
Always start with width and height
<img src="images/loaf-800.jpg" alt="Sourdough loaf on a board" width="800" height="500">
Adding the real width and height lets the browser calculate the shape before the image loads, so nothing jumps around when it arrives. This avoids layout shift, which annoys visitors and counts against your performance scores. In CSS, the usual companion is img { max-width: 100%; height: auto; }.
srcset and sizes
<img src="images/loaf-800.jpg"
srcset="images/loaf-400.jpg 400w,
images/loaf-800.jpg 800w,
images/loaf-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, 800px"
alt="Sourdough loaf on a wooden board"
width="800" height="500">
srcsetlists the files and their real widths in pixels, usingwunits.sizestells the browser how wide the image will be shown at different screen widths. Here: full width on screens up to 600 px, and 800 px otherwise.srcis the fallback for old browsers.
The browser combines the display size, the screen’s pixel density and the file list, then downloads only the best file. A phone gets the 400 pixel file, which is a fraction of the size. You wrote the menu; the browser orders from it.
A sharp retina phone screen packs two or three real pixels into every CSS pixel, so a 400 px wide display may fetch the 800 px file. That is intended. It is the reason to offer several sizes.
Pixel density with x descriptors
For images that always display at the same size, such as a logo, a simpler form works.
<img src="logo.png" srcset="logo.png 1x, logo@2x.png 2x" alt="Priya's Bakery" width="160" height="48">
The picture element: different crops and formats
Sometimes you want a different image, not just a smaller one. A wide photo of a bakery counter may need a tighter crop on a phone so the subject stays visible. This is called art direction, and the <picture> element handles it.
<picture>
<source media="(max-width: 600px)" srcset="images/hero-square.jpg">
<source media="(max-width: 1200px)" srcset="images/hero-medium.jpg">
<img src="images/hero-wide.jpg" alt="Priya at the bakery counter" width="1600" height="700">
</picture>
The browser reads the sources from the top and uses the first whose condition matches. The final img is both the fallback and the element that actually displays, so it must carry the alt text.
Modern formats with a safety net
Formats such as WebP and AVIF compress far better than JPG, often saving a third to a half of the weight at the same quality. The picture element lets you offer them safely.
<picture>
<source type="image/avif" srcset="images/loaf.avif">
<source type="image/webp" srcset="images/loaf.webp">
<img src="images/loaf.jpg" alt="Sourdough loaf" width="800" height="500">
</picture>
Add width and height attributes, and a loading attribute of lazy, to this image. Use width 600 and height 400.
<img src="https://picsum.photos/600/400" alt="Sample landscape">
Add width=”600″ height=”400″ loading=”lazy” inside the img tag.
<img src="https://picsum.photos/600/400" alt="Sample landscape" width="600" height="400" loading="lazy">
Lazy loading and priorities
loading="lazy"tells the browser to fetch an image only when it is close to the screen. Use it on images below the fold.- Do not lazy-load the main image at the top of the page. That one should arrive first. For it, use
fetchpriority="high". decoding="async"lets the browser prepare the image without blocking other work.
Lazy loading your hero image makes the most important picture show up later. Only lazy-load what is out of sight at first.
A simple workflow
- Choose the largest size you will ever need, for example 1600 pixels wide.
- Export three or four smaller copies, such as 400, 800 and 1200.
- Compress them. Aim for under 150 KB for large photos.
- Write the srcset, sizes, width, height and alt.
- Test in the browser’s Network tab at a narrow width to confirm the small file is the one loading.
Write a picture element: an avif source, a webp source, and a jpg img fallback for hero.jpg, with alt text.
<!-- write the picture element -->
Put sources first, with type and srcset, and the img last.
<picture>
<source type="image/avif" srcset="hero.avif">
<source type="image/webp" srcset="hero.webp">
<img src="hero.jpg" alt="Bakery counter" width="800" height="450">
</picture>
A realistic before and after
Here is what the effort buys you on Priya’s homepage. Before: one 3000 pixel JPG weighing 2.1 MB, sent to every visitor. After: four versions of the picture in WebP, 400, 800, 1200 and 1600 pixels wide, weighing 28 KB, 70 KB, 120 KB and 190 KB. A phone fetches the 28 KB or 70 KB file, a laptop the 120 KB file, and only a large retina monitor ever needs the biggest. Total weight for most visitors drops by more than 90 percent, the page appears in a fraction of the time, and the picture looks just as sharp on every screen.
That kind of result is rare in web development: a change that makes the site faster, cheaper to host and better ranked, using nothing more than a few lines of HTML and an hour with an image tool.
Tools that do the resizing for you
- Free web tools such as Squoosh let you resize and convert to WebP or AVIF right in the browser.
- WordPress creates several sizes of every upload automatically and writes the srcset for you.
- Build tools and image services can generate all sizes during publishing, so you only keep one original.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Why add width and height to an img?
- It makes the file smaller
- The browser can reserve space and avoid layout shift
- It enables lazy loading
- It adds a border
Known dimensions stop the page jumping as images load.
What does srcset do?
- Lists image files so the browser can choose one
- Sets the alt text
- Blocks downloads
- Adds captions
The browser picks the best file for the screen.
When is the picture element the right tool?
- For tables
- For different crops or formats of an image
- For videos
- For links
picture supports art direction and format fallbacks.
Which image should NOT be lazy-loaded?
- The footer logo
- The large image at the very top
- A gallery below the fold
- An image in a long article
The top image is needed immediately.
Which format usually gives the smallest file at similar quality?
- BMP
- AVIF or WebP
- TIFF
- Uncompressed PNG
AVIF and WebP compress much better than JPG or PNG.
Summary
- Give every image width and height.
- srcset and sizes let the browser pick the right file.
- picture handles art direction and modern formats.
- Lazy-load below the fold, prioritise the top image.
Questions
How many sizes should I make?
Three or four is plenty, for example 400, 800, 1200 and 1600 pixels wide.
Is srcset enough without picture?
For most photos, yes. Use picture only when you need a different crop or format fallback.
Does WordPress do this automatically?
Yes, WordPress adds srcset and lazy loading to images in posts. Knowing the HTML helps you check and customise the result.
