Responsive Images and Performance
Images are usually the heaviest part of a page, and the easiest place to waste a visitor's data. The most common waste is simple: sending a huge picture to a small screen. A phone that needs 400 pixels of image gets 2400, and downloads six times more pixels than it can show.
Responsive images are the fix. You offer the browser several versions of the same picture and tell it how big the picture will be on the page, and it picks the one that fits. The basics of srcset, sizes, and picture are in the earlier Responsive Images and Picture Element lessons. This lesson takes the performance view: how the choice is made, where it goes wrong, and how to check it.
What You Will Learnβ
- How the browser chooses a file from a
srcset - Why
sizesmatters more than most people think - The three common ways images waste data
- When to use width descriptors, and when to use density descriptors
- How formats and compression save bytes
- How to treat the main (LCP) image
- How to audit a page, in markup and in DevTools
The Big Ideaβ
You provide the choices. The browser knows the screen, so it makes the decision.
- HTML Code
- Selection Lab
- Image Checker
<img
src="/img/hero-1200.jpg"
srcset="/img/hero-800.jpg 800w,
/img/hero-1200.jpg 1200w,
/img/hero-1600.jpg 1600w,
/img/hero-2400.jpg 2400w"
sizes="(min-width: 800px) 50vw, 100vw"
alt="A golden loaf on a wooden board"
width="1600" height="800"
fetchpriority="high">
The page shows this image full width on small screens, and in a two-column layout (half the screen) from 800 px up. The file comes in 400, 800, 1200, 1600, and 2400 px widths.
- The browser believes the image is 1200 px wide, so it needs about 2400 px of image.
- It downloads the 2400w file, about 900 KB.
- The best file for the real layout is 1200w, about 225 KB.
Wasted: about 675 KB (75% of this download).
File sizes are illustrative (they grow with the square of the width). Real browsers also consider things like their cache, so they may choose a file you did not expect. Always check what was actually downloaded.
Loading the checker...
In the Selection Lab, set a wide screen and choose "No sizes attribute". The browser assumes the image fills the whole screen, though the layout shows it at half width. Now switch to the matching sizes and see how much is saved.
How the Browser Choosesβ
The choice follows a few steps:
- Work out the slot. The
sizesattribute tells the browser how wide the image will be on the page, in CSS pixels. - Multiply by the screen density. A 400 px slot on a 2x screen needs about 800 pixels of image.
- Pick a candidate. The browser generally picks the smallest file that is at least as wide as it needs.
- Download only that one. The others are never fetched.
This happens before the page layout is known, because the browser wants to start the download early. That is why it cannot measure the image itself, and why it relies on sizes. Browsers are also free to use their own judgement, for example by reusing a larger image they already have cached, so you cannot rely on one exact result.
sizes Is Where Things Go Wrongβ
If you give a srcset with width descriptors (800w) and no sizes, the browser assumes the image will fill the whole width of the screen (100vw). For a picture that really sits in a half-width column, that means it picks a file about twice as wide as it should, and downloads roughly four times the pixels.
<!-- Wrong: the layout shows this in a narrow column, but sizes is missing -->
<img src="/img/card-800.jpg" srcset="/img/card-400.jpg 400w, /img/card-800.jpg 800w" alt="Rye loaves">
<!-- Better: sizes matches the real layout -->
<img src="/img/card-800.jpg"
srcset="/img/card-400.jpg 400w, /img/card-800.jpg 800w"
sizes="(min-width: 800px) 33vw, 100vw"
alt="Rye loaves" width="800" height="533">
Write sizes from your layout: look at how wide the image is at each breakpoint, and describe that. List the conditions from the widest to the narrowest, and end with a default.
Three Ways Images Waste Dataβ
| Problem | What happens | The fix |
|---|---|---|
| One huge file for everyone | A phone downloads a desktop image | Add a srcset with several widths |
srcset with no sizes | The browser assumes 100vw | Add a sizes that matches the layout |
A sizes that does not match the layout | The wrong file is chosen | Update sizes when the layout changes |
The third one is the sneakiest. A designer changes a three-column grid to two columns, nobody updates sizes, and the page quietly downloads a bigger image than it needs for months.
Width Descriptors or Density Descriptors?β
Width descriptors (800w) | Density descriptors (2x) | |
|---|---|---|
| You describe | How wide each file is | How sharp each file is |
Needs sizes | Yes | No |
| Best for | Images whose size changes with the layout | Images with a fixed display size, such as a logo or an avatar |
| Example | card-400.jpg 400w, card-800.jpg 800w | logo.png 1x, logo-2x.png 2x |
Use one kind per image, never both. Fluid, full-width, or column images almost always want width descriptors, while a logo with a fixed size is a good fit for densities.
How Many Sizes?β
Offer enough choices that no screen is far from a good match, but not so many that you create dozens of files to build, store, and cache. A handful (for example 400, 800, 1200, 1600) is often plenty, and spacing them by roughly a quarter to a half is a common rule of thumb. Beyond that, extra files add work for little gain. An image service or build tool can generate the sizes for you.
Formats and Compressionβ
Pixels are one lever, and the way they are stored is another.
| Format | Good for | Notes |
|---|---|---|
| JPEG | Photos | Works everywhere, and a good fallback |
| PNG | Sharp graphics, transparency | Large for photos |
| WebP | Photos and graphics | Often noticeably smaller than JPEG, and widely supported |
| AVIF | Photos | Often smaller still, but slower to create, and support is not as universal |
| SVG | Logos, icons, simple drawings | Scales without blur, and is tiny for simple shapes |
Savings depend on the picture, so test your own images. More on formats is in the Image Formats lesson.
To offer a modern format with a safe fallback, use picture with a type on each source:
<picture>
<source type="image/avif" srcset="/img/banner-800.avif 800w, /img/banner-1600.avif 1600w" sizes="100vw">
<source type="image/webp" srcset="/img/banner-800.webp 800w, /img/banner-1600.webp 1600w" sizes="100vw">
<img src="/img/banner-1600.jpg" alt="Our shop at sunrise" width="1600" height="533" loading="lazy">
</picture>
The browser takes the first source whose type it supports, and falls back to the img. Compression quality matters too. Many sites find that a moderate quality setting (often somewhere around 75 to 85 for JPEG) looks fine and cuts size a lot, but check your own pictures. Also remove extra metadata when you do not need it.
The Main Image Is Specialβ
The largest image on the first screen is often your Largest Contentful Paint. Treat it with care:
- Never lazy-load it. See Lazy Loading.
- Give it
fetchpriority="high"so the browser starts it early. Check MDN for current support. - Give it
widthandheight, so no layout jumps. - Pick its sizes carefully. It is the file most worth getting right.
- Preload it only if it is found late. If it is set in CSS, a preload with
imagesrcsetandimagesizespicks the same file as theimgwould.
Check Your Own Imagesβ
First, try the checker from the earlier tab with your own markup. Then confirm in the browser, because only the browser knows what was really chosen.
- Open DevTools, then Network, filter by Img, and reload. Check the size of each file.
- Right-click an image and open it, or check the Elements panel: the
currentSrcproperty shows which file was chosen. - Run this in the Console to find images that are much bigger than needed:
document.querySelectorAll("img").forEach(function (img) {
const ratio = img.naturalWidth / (img.clientWidth * devicePixelRatio);
if (ratio > 1.5) {
console.log("Oversized:", img.currentSrc, Math.round(ratio * 10) / 10 + "x");
}
});
- Resize the browser window, and reload at several widths. The chosen file should change.
- Use Lighthouse, whose suggestions include properly sizing images and using modern formats.
- Test with a throttled network, and look at your LCP before and after.
Best Practicesβ
- Always offer several widths for any image bigger than a small icon.
- Write
sizesfrom your real layout, and update it when the layout changes. - Use width descriptors for fluid images, and density descriptors for fixed ones.
- Never mix the two kinds in one
srcset. - Offer a modern format with a safe fallback.
- Compress sensibly, and test the result by eye.
- Give every image
widthandheight. - Treat the main image with care: early, sized, and not lazy.
- Automate the resizing with a build tool or an image service.
Quick Checklistβ
- Every large image has a
srcsetwith several widths - Every
srcsetwithwdescriptors has a matchingsizes -
sizesmatches the current layout - No
srcsetmixeswandx - The
imghas asrcas a fallback - Modern formats are offered with
type - Every image has
widthandheight - The main image is not lazy, and is prioritised
- DevTools shows no image far bigger than it needs to be
Security and Privacy Notesβ
- Remove location data from photos. Photos from phones can carry hidden metadata, including the place they were taken. Strip it before you publish, unless you want it shown.
- Be careful with image resizing services. If your server or CDN resizes images from a URL, let it produce only a fixed list of sizes, or sign the URLs. Otherwise anyone can ask for huge or endless variations, which costs you money and can overload the service.
- Treat uploaded SVG files as code. An SVG can contain scripts. Never embed untrusted SVG inline, and serve or sanitise user uploads carefully. SVG used through an
imgtag does not run scripts, but inline SVG can. - Images from other hosts share visitor details. Loading an image from a third party tells that host about the visitor. Use hosts you trust, and consider hosting your own.
- Do not put private information in image URLs, because they appear in the page source and may be logged.
Global Audience Notesβ
- Many visitors use limited data. Right-sized images are one of the kindest things you can do for them. A phone on a slow plan benefits most.
- High-density screens are common, but a 3x screen does not always need a 3x image on a slow connection. A well-compressed 2x image often looks good enough, so test.
- Slow networks make large images painful. Smaller files, modern formats, and lazy loading all help.
- Avoid putting text inside images. It cannot be translated, resized, or read by a screen reader. Use real text on top of, or beside, the picture, and keep the image itself light.
- Write good
alttext in the page's language, for every image that carries meaning.
Common Mistakes to Avoidβ
- One huge file for every screen.
- A
srcsetwithwdescriptors and nosizes. - A
sizesthat was never updated after a layout change. - Mixing
wandxdescriptors. - A
srcsetwith a single choice. - Forgetting
typeon format sources in apictureelement. - No
srcon theimg. - Lazy-loading the main image.
- No
widthandheight. - Never checking which file the browser actually chose.
Practice Timeβ
- In the Selection Lab, set the screen to 1200 px and the density to 2x. How much is wasted with no
sizes? - Switch to the matching
sizes. How much is saved? - Try a 3x screen at a small width. Which file is chosen?
- Load the messy images into the checker and fix every problem it reports.
- Open a page you built, and run the Console snippet. Which image is the most oversized?
- Add a
srcsetandsizesto it, and reload at two window widths. - Check
currentSrcat each width. - Bonus: convert one photo to WebP or AVIF and compare the file size.
Quick Recapβ
Click a question to check your answer.
1. How does the browser choose an image?
It works out the slot width from sizes, multiplies by the screen density, and picks the smallest candidate that is at least that wide. It then downloads only that file.
2. Why is sizes important?
The browser chooses before the layout is known, so it relies on sizes for the image's width. Without it, it assumes the image fills the whole screen, and may download far too much.
3. When should you use w descriptors, and when x?
Use w descriptors for images whose size changes with the layout, together with sizes. Use x descriptors for fixed-size images such as logos. Never mix them in one srcset.
4. How do you offer a modern format safely?
Use a picture element with a source for each format, each with a type, and a normal img inside as the fallback.
5. How should you treat the main image?
Do not lazy-load it, give it width and height, consider fetchpriority="high", and choose its sizes carefully, because it is often the Largest Contentful Paint.
6. How can you see which file was chosen?
Use the Network panel, or read the image's currentSrc in the Console. Resize the window and reload to see it change.
Key Takeawaysβ
- Send each screen the size it needs, not more.
sizestells the browser how big the image will be, and wrong or missing values waste data.- Use
wdescriptors for fluid images,xfor fixed ones, and never both. - Modern formats and sensible compression cut bytes further.
- The main image gets special care: early, sized, and not lazy.
- Check in DevTools. The browser's choice is the truth.
- Strip hidden metadata, and protect any image resizing service.
Next up: Critical Resources, where you will learn how to find the few files that decide how fast your page feels.