Responsive Image Optimisation: srcset, sizes, WebP and AVIF, Lazy Loading and LCP Priority

Images are usually the heaviest part of a web page and often the element that determines how fast it looks loaded. A phone with a 360 px wide screen does not need a 4,000 px wide photo, yet many sites send the same giant file to every device. The result is slow loading, wasted mobile data, poor Core Web Vitals and visitors who leave. Responsive image optimisation fixes this by sending each device an image of the right size and format, and by loading the important image first and the rest later.

This guide covers the HTML and the reasoning behind it: how to calculate the sizes you need, how srcset and sizes work, when to use the <picture> element, which formats to choose, how to avoid layout shift, how to load the hero image fast and how to test. It ties in with Core Web Vitals on Mobile, the On-Page SEO Checklist and the Responsive Web Design Testing Checklist.

Step 1: Work out the size you actually need

The pixel width you need equals the CSS width the image is displayed at, multiplied by the device pixel ratio (DPR). Most modern phones have a DPR of 2 or 3.

needed pixels = displayed CSS width × device pixel ratio

Example. A hero image fills the full width of a 360 px phone. At DPR 3 it needs 1,080 px. On a 1,440 px laptop at DPR 1 with the image at 800 px wide inside a column, it needs 800 px. On a 1,440 px display at DPR 2, it needs 1,600 px. Sending anything much larger than these numbers wastes bytes.

The cost is not only download size. A 4,000 × 3,000 photo has 12 million pixels, and once decoded it takes about 12,000,000 × 4 bytes = 48 MB of memory. An 800 × 533 version is 426,400 pixels, about 1.7 MB decoded, or only 3.6% of the pixels. Decoding large images also takes CPU time on low-end phones.

Step 2: Provide several widths with srcset and sizes

<img
  src="/img/shop-800.jpg"
  srcset="/img/shop-400.jpg 400w,
          /img/shop-800.jpg 800w,
          /img/shop-1200.jpg 1200w,
          /img/shop-1600.jpg 1600w"
  sizes="(max-width: 600px) 100vw, (max-width: 1024px) 50vw, 600px"
  width="800" height="533"
  alt="Storefront of a neighbourhood sweet shop">
  • srcset with w descriptors lists the available files and their real pixel widths.
  • sizes tells the browser how wide the image will be displayed at each viewport width, so it can pick a file before the CSS has loaded. In the example, the image is full width up to 600 px, half width up to 1,024 px and 600 px wide on larger screens.
  • The browser combines sizes, the screen’s DPR and sometimes network conditions to choose a candidate. You hint; it decides.
  • src is the fallback for browsers that do not understand srcset.

The most common mistake is a wrong or missing sizes. Without it, the browser assumes the image is 100vw wide and may download a larger file than needed. Make sizes match your CSS layout. Use breakpoints that follow your design, as covered in CSS Breakpoints, Media Queries and Container Queries.

For fixed-size images, such as a logo that is always 120 px wide, use density descriptors instead:

<img src="/img/logo-120.png"
     srcset="/img/logo-120.png 1x, /img/logo-240.png 2x, /img/logo-360.png 3x"
     width="120" height="40" alt="My Advisers logo">

Step 3: Choose the right format

FormatBest forNotes
AVIFPhotos where you want the smallest filesOften smallest, supported in current major browsers, slower to encode
WebPPhotos and graphicsWidely supported, a good default, supports transparency
JPEGPhotos, universal fallbackCompatible everywhere
PNGSharp graphics needing lossless transparencyLarge for photos
SVGLogos, icons, simple illustrationsScales infinitely, tiny, can be styled with CSS

Savings depend heavily on the image and settings, so measure rather than trust a headline percentage. Check current browser support on a compatibility table before dropping fallbacks.

Step 4: Offer modern formats with the picture element

<picture>
  <source type="image/avif"
          srcset="/img/shop-400.avif 400w, /img/shop-800.avif 800w, /img/shop-1200.avif 1200w"
          sizes="(max-width: 600px) 100vw, 600px">
  <source type="image/webp"
          srcset="/img/shop-400.webp 400w, /img/shop-800.webp 800w, /img/shop-1200.webp 1200w"
          sizes="(max-width: 600px) 100vw, 600px">
  <img src="/img/shop-800.jpg"
       srcset="/img/shop-400.jpg 400w, /img/shop-800.jpg 800w, /img/shop-1200.jpg 1200w"
       sizes="(max-width: 600px) 100vw, 600px"
       width="800" height="533"
       alt="Storefront of a neighbourhood sweet shop">
</picture>

The browser uses the first <source> it supports and falls back to the <img>. Put the alt, width, height and loading attributes on the <img>. If your hosting or CDN can convert formats automatically, you may not need to write this by hand.

Step 5: Art direction

Sometimes a smaller screen needs a different crop, not just a smaller copy. A wide banner showing a shop and its surroundings becomes unreadable on a phone; a tighter crop on the subject works better. Use media on the <source> elements:

<picture>
  <source media="(max-width: 600px)" srcset="/img/hero-portrait-800.webp">
  <img src="/img/hero-wide-1600.webp" width="1600" height="700" alt="Customers at the counter">
</picture>

Step 6: Reserve space to prevent layout shift

Always include width and height attributes that match the image’s intrinsic aspect ratio. Modern browsers use them to reserve space before the image loads, which prevents Cumulative Layout Shift (CLS). Then make the image responsive in CSS:

img { max-width: 100%; height: auto; }

/* Or reserve the ratio explicitly for containers */
.card-media { aspect-ratio: 3 / 2; }
.card-media img { width: 100%; height: 100%; object-fit: cover; }

Step 7: Load the right images at the right time

  • Lazy-load below-the-fold images with loading="lazy", so they download only when the user scrolls near them.
  • Do not lazy-load the main above-the-fold image. It is often the Largest Contentful Paint element, and delaying it hurts LCP.
  • Prioritise the LCP image with fetchpriority="high" on that single image so the browser requests it early.
  • Use decoding="async" on non-critical images so decoding does not block rendering.
  • Preload only when needed. If the LCP image is set in CSS or discovered late, a <link rel="preload" as="image"> can help, but a normal <img> in the HTML is usually discovered early enough.
<!-- Hero (LCP) image: eager and high priority -->
<img src="/img/hero-800.webp" srcset="..." sizes="100vw"
     width="1200" height="630" fetchpriority="high" alt="...">

<!-- Below the fold -->
<img src="/img/gallery-1.webp" srcset="..." sizes="(max-width: 600px) 100vw, 33vw"
     width="600" height="400" loading="lazy" decoding="async" alt="...">

Step 8: Compress sensibly

  • Resize first, then compress. Compressing a 4,000 px image and scaling it in CSS still sends a huge file.
  • Pick a quality setting visually. For photos, lossy quality around the middle-to-high range is often indistinguishable from the original at a fraction of the size. Check a few samples on a phone.
  • Strip metadata such as camera data and location when you do not need it. This also protects privacy.
  • Avoid text in images. Real text is smaller, searchable, accessible and translates.
  • Use SVG for icons and logos, and consider CSS for simple gradients and shapes.

Step 9: Alt text, file names and SEO

Every meaningful image needs descriptive alt text, and decorative images should have alt="". Use readable file names such as sweet-shop-storefront.webp. Images that are indexable and relevant can bring traffic from image search, and they help the page’s context. See the On-Page SEO Checklist for Developers for details.

Step 10: Background images and CSS

CSS background images cannot use srcset. Options include using image-set() for density and format variants, or switching images in media queries. For content images that matter to the page, prefer a real <img> so you get srcset, lazy loading, alt text and early discovery.

.hero {
  background-image: url("/img/hero-800.jpg");
}
@media (min-width: 900px) {
  .hero { background-image: url("/img/hero-1600.jpg"); }
}

Step 11: A CDN and caching

An image CDN can resize and convert on the fly using URL parameters, and serve images from locations close to your visitors. Set long cache lifetimes for static images and use fingerprinted file names so updates are picked up. If you run WordPress, check what your theme and hosting generate by default: WordPress creates several sizes and adds srcset automatically for images inserted through the media library, but custom theme code can bypass it, and the sizes value may need adjusting.

How to test

  1. Open the page at 360, 768 and 1,280 px widths and check which file loads in the browser’s network panel.
  2. Confirm that no image is more than about 1.5 to 2 times larger than its needed pixel size.
  3. Run Lighthouse or PageSpeed Insights and review the “properly size images”, “efficiently encode images” and “defer offscreen images” items.
  4. Check LCP and CLS in field data where available.
  5. Throttle the network to a slow connection and look at the loading order.
  6. View the page on a real phone, because emulators do not reproduce real decode and memory behaviour.

Dev Browser lets you load a page on your own phone at phone, tablet and desktop widths, which is a quick way to confirm that images look sharp and fit properly at each layout. Install it from Google Play, read the Dev Browser guide and see the products page.

Common mistakes

  • Uploading original camera photos straight to the page.
  • Adding srcset but omitting or mis-stating sizes.
  • Lazy-loading the hero image, which delays LCP.
  • Leaving out width and height, causing layout shift.
  • Serving AVIF or WebP without a fallback for browsers that need one.
  • Using images for text or critical UI.
  • Using one fetchpriority="high" on many images, which cancels its purpose.

Frequently asked questions

What is the difference between srcset and sizes?

srcset lists the available files and their widths. sizes tells the browser how wide the image will display, so it can choose the best file.

Should I use WebP or AVIF?

WebP is a safe default with broad support. AVIF can be smaller but takes longer to encode. Offering both with a JPEG fallback gives the best coverage.

Should every image be lazy-loaded?

No. Lazy-load images below the fold, and load the main visible image eagerly, with high priority.

How many sizes should I generate?

Three to five widths, for example 400, 800, 1,200 and 1,600, usually cover most layouts without creating a maintenance burden.

Conclusion

Work out the pixels each layout needs, provide a handful of widths, describe the layout with sizes, offer modern formats with fallbacks, reserve space with width and height, and load the hero first and everything else later. Then verify on a real phone with Dev Browser.

Disclaimer: Browser support and best practice for image formats and loading hints evolve. Verify current support and measure results on your own pages.

Leave a Reply

Share Your Love