Performance · SEO · Frontend · Core Web Vitals

Image Optimization for the Web: AVIF, WebP, srcset and Lazy Loading

Making website images fast: choosing a format (AVIF, WebP, JPEG), sizing, srcset and sizes, lazy loading, prioritising the LCP image and preventing layout shift.

Sander Yazıcıoğlu Published: 2 min read Türkçe oku

Image Optimization for the Web: AVIF, WebP, srcset and Lazy Loading

On the sites I built for health-tourism, logistics and corporate clients, the biggest performance win almost always came from the same place: images. Images are usually more than half of a page's weight, and the Largest Contentful Paint (LCP) element is very often a hero image.

1. The right format

FormatWhen
AVIFBest compression for photos; supported by all modern browsers
WebPWide support, clearly smaller than JPEG
JPEGFallback for old browsers
PNGSimple graphics needing transparency (WebP is often better)
SVGLogos, icons, diagrams

Use <picture> to let the browser pick the best format it supports:

<picture>
  <source srcset="/img/clinic.avif" type="image/avif" />
  <source srcset="/img/clinic.webp" type="image/webp" />
  <img src="/img/clinic.jpg" alt="Waiting room of the clinic in Antalya" width="1200" height="800" />
</picture>

2. The right size

Showing a 3000-pixel photo in a 400-pixel card is the most common waste. Give the browser options with srcset and sizes:

<img
  src="/img/card-800.webp"
  srcset="/img/card-400.webp 400w, /img/card-800.webp 800w, /img/card-1200.webp 1200w"
  sizes="(max-width: 760px) 100vw, 380px"
  alt="Team meeting"
  width="800" height="450"
  loading="lazy" decoding="async" />

sizes tells the browser how wide the image will render; the browser factors in screen density and downloads the best-fitting file.

3. Lazy loading, but not for the LCP image

loading="lazy" keeps off-screen images from downloading until the user gets close. The main image on the first screen needs the opposite:

<img src="/img/hero.avif" alt="..." width="1600" height="900" fetchpriority="high" />

Lazy-loading the LCP image is the mistake I see most often, and it delays LCP by seconds. On this blog, article covers load with fetchpriority="high" while thumbnails in lists use loading="lazy".

4. Prevent CLS: declare dimensions

Give every image a width and height. The browser reserves space using that ratio, so text doesn't jump when the image arrives. width: 100%; height: auto; in CSS keeps it responsive. I cover the other Core Web Vitals techniques in the Core Web Vitals article.

5. Compression quality

You don't need quality 100. For photos, roughly 50–60 for AVIF and 75–80 for WebP is usually indistinguishable to the eye and halves the file. Use sharp, squoosh or your build tool's image plugins for batch conversion, and wire it into the build; manual optimisation gets forgotten.

6. Caching and CDNs

Put a content hash in the file name (hero.3f9a1c.avif) and serve it with a long immutable cache. A CDN serves images close to users, and some can convert format and size on the fly.

7. Images for SEO

  • Meaningful alt text: describe what the image shows; don't stuff keywords. Use alt="" for decorative images.
  • Meaningful file names: antalya-dental-clinic.jpg instead of IMG_4821.jpg.
  • Image sitemaps and an Open Graph image: for visibility in shares and Google Images.

Quick checklist

  • Hero image in AVIF/WebP, correctly sized, fetchpriority="high"
  • Other images loading="lazy"
  • width/height on every <img>
  • Responsive sizes with srcset + sizes
  • Long-lived caching and a CDN
  • Meaningful alt text

Conclusion

Image optimisation gives the biggest speed win for the least effort on most websites. The right format, the right size, the right loading priority and fixed dimensions: these four rules noticeably cut LCP and page weight on most sites.

All articles →