Skip to main content

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 sizes matters 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.

index.html
<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">

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:

  1. Work out the slot. The sizes attribute tells the browser how wide the image will be on the page, in CSS pixels.
  2. Multiply by the screen density. A 400 px slot on a 2x screen needs about 800 pixels of image.
  3. Pick a candidate. The browser generally picks the smallest file that is at least as wide as it needs.
  4. 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​

ProblemWhat happensThe fix
One huge file for everyoneA phone downloads a desktop imageAdd a srcset with several widths
srcset with no sizesThe browser assumes 100vwAdd a sizes that matches the layout
A sizes that does not match the layoutThe wrong file is chosenUpdate 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 describeHow wide each file isHow sharp each file is
Needs sizesYesNo
Best forImages whose size changes with the layoutImages with a fixed display size, such as a logo or an avatar
Examplecard-400.jpg 400w, card-800.jpg 800wlogo.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.

FormatGood forNotes
JPEGPhotosWorks everywhere, and a good fallback
PNGSharp graphics, transparencyLarge for photos
WebPPhotos and graphicsOften noticeably smaller than JPEG, and widely supported
AVIFPhotosOften smaller still, but slower to create, and support is not as universal
SVGLogos, icons, simple drawingsScales 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 width and height, 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 imagesrcset and imagesizes picks the same file as the img would.

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.

  1. Open DevTools, then Network, filter by Img, and reload. Check the size of each file.
  2. Right-click an image and open it, or check the Elements panel: the currentSrc property shows which file was chosen.
  3. 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");
}
});
  1. Resize the browser window, and reload at several widths. The chosen file should change.
  2. Use Lighthouse, whose suggestions include properly sizing images and using modern formats.
  3. Test with a throttled network, and look at your LCP before and after.

Best Practices​

  1. Always offer several widths for any image bigger than a small icon.
  2. Write sizes from your real layout, and update it when the layout changes.
  3. Use width descriptors for fluid images, and density descriptors for fixed ones.
  4. Never mix the two kinds in one srcset.
  5. Offer a modern format with a safe fallback.
  6. Compress sensibly, and test the result by eye.
  7. Give every image width and height.
  8. Treat the main image with care: early, sized, and not lazy.
  9. Automate the resizing with a build tool or an image service.

Quick Checklist​

  • Every large image has a srcset with several widths
  • Every srcset with w descriptors has a matching sizes
  • sizes matches the current layout
  • No srcset mixes w and x
  • The img has a src as a fallback
  • Modern formats are offered with type
  • Every image has width and height
  • 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 img tag 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 alt text in the page's language, for every image that carries meaning.

Common Mistakes to Avoid​

  1. One huge file for every screen.
  2. A srcset with w descriptors and no sizes.
  3. A sizes that was never updated after a layout change.
  4. Mixing w and x descriptors.
  5. A srcset with a single choice.
  6. Forgetting type on format sources in a picture element.
  7. No src on the img.
  8. Lazy-loading the main image.
  9. No width and height.
  10. Never checking which file the browser actually chose.

Practice Time​

  1. In the Selection Lab, set the screen to 1200 px and the density to 2x. How much is wasted with no sizes?
  2. Switch to the matching sizes. How much is saved?
  3. Try a 3x screen at a small width. Which file is chosen?
  4. Load the messy images into the checker and fix every problem it reports.
  5. Open a page you built, and run the Console snippet. Which image is the most oversized?
  6. Add a srcset and sizes to it, and reload at two window widths.
  7. Check currentSrc at each width.
  8. 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.
  • sizes tells the browser how big the image will be, and wrong or missing values waste data.
  • Use w descriptors for fluid images, x for 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.