Tech

Image Optimization for the Modern Web

Suggested path: Performance Engineering · View path →

Your image strategy should start with dimensions, not formats.

Key takeaways

  • Start with dimensions, not formats — a correctly-sized image in an old format beats an oversized image in a new one.
  • AVIF wins on file size, WebP wins on compatibility and encode speed; both beat JPEG/PNG for photographic content.
  • Your single largest above-the-fold image is very likely your LCP element. Never lazy-load it.
  • srcset and sizes exist to stop the browser downloading a 2000px image for a 400px slot — the most common source of wasted image bytes.

Dimensions come before format

50%+of page weight is often images
2000pxtypical unnecessary hero width
~30–50%AVIF savings over JPEG

The single biggest image mistake is choosing a better format for an image that shouldn't have been that large in the first place. Serving a 2400×1600 image into a 600px-wide card wastes far more bytes than switching that same oversized image from JPEG to AVIF would ever save. Fix dimensions first; format second.

Uploading the camera-original file and letting CSS shrink it with width: 100% is the single most common image performance mistake. The browser still downloads every byte of the original resolution.

Choosing a format

FormatBest forTrade-off
AVIFPhotographs, complex images — smallest file sizeSlower to encode; slightly lower browser support than WebP
WebPGeneral-purpose replacement for JPEG/PNGLarger than AVIF, but near-universal support and fast encoding
JPEGFallback for the rare unsupported browserLargest file size of the three
SVGIcons, logos, illustrations with flat colorsNot suitable for photographs

The practical pattern is a <picture> element offering AVIF first, WebP second, and a JPEG fallback last — the browser picks the first format it supports:

<picture>
  <source srcset="hero.avif" type="image/avif">
  <source srcset="hero.webp" type="image/webp">
  <img src="hero.jpg" alt="Description" width="1200" height="630" fetchpriority="high">
</picture>

Responsive images: srcset and sizes

A single image file cannot be the right size for every viewport. srcset offers the browser several resolutions of the same image; sizes tells it how large the image will actually render at different breakpoints, so it can pick the smallest sufficient file.

<img
  srcset="card-400.webp 400w, card-800.webp 800w, card-1200.webp 1200w"
  sizes="(max-width: 600px) 100vw, 400px"
  src="card-800.webp"
  alt="Description" width="400" height="300" loading="lazy">

The sizes attribute is not a display size — it's a hint the browser uses before it knows the final CSS layout, at the point it must decide which file to start downloading. Get it wrong and the browser can pick a file that's too small (blurry) or too large (wasted bytes) despite srcset being correct.

Loading priority: the LCP decision that's easy to get backwards

Every image on a page competes for priority, and the one decision that most directly affects LCP is how you treat your largest above-the-fold image versus everything below the fold.

ImageCorrect treatmentWhy
Hero / largest above-the-fold imagefetchpriority="high", no loading="lazy"This is very likely your LCP element — delaying it delays your LCP score directly
Below-the-fold imagesloading="lazy"Downloading them early competes for bandwidth with content the user can actually see
Background images via CSSAvoid for LCP-critical contentThe browser discovers them later than an <img> tag found in the initial HTML parse

Watch out: lazy-loading the hero image is one of the most common LCP regressions. It looks harmless in a code review but tells the browser to wait until the image is nearly in view before even starting the download — directly on your critical path.

Preventing layout shift from images

Always set explicit width and height attributes (or an aspect-ratio in CSS) on every image. Without them, the browser doesn't know how much space to reserve before the image downloads, and the layout jumps when it arrives — the most common cause of poor CLS. See how layout shift fits into the rendering pipeline for the underlying mechanism.

A practical checklist

  1. Resize the source image to the largest size it will actually render at — not the camera original.
  2. Serve AVIF with WebP and JPEG fallbacks via <picture>.
  3. Use srcset + sizes for anything that renders at different widths across breakpoints.
  4. Set explicit width and height on every image, every time.
  5. fetchpriority="high" and eager-loading on the hero image only; loading="lazy" everywhere else.
  6. Compress before shipping — automated pipelines (Squoosh, sharp, an image CDN) catch what manual export settings miss.

Format choice gets most of the attention, but dimensions, loading priority and explicit sizing are what actually move LCP and CLS. Get those three right first, and format becomes a smaller optimization on top of an already-solid foundation.

Images are only one of several possible causes of a slow page. If fixing them did not help as much as you expected, work through the nine most common causes of a slow website to find what is really holding the page back.

Frequently asked questions

Should I always use AVIF?

AVIF gives the smallest files for photographic content, but serve it alongside a WebP and JPEG fallback via <picture> since encode time and support vary slightly by browser.

Why is my hero image hurting my LCP score?

The most common cause is lazy-loading it or leaving out fetchpriority="high" — both delay the download of what is very likely your largest above-the-fold element.

Do I need both srcset and sizes?

Yes. srcset lists the available resolutions; sizes tells the browser how large the image will actually render at different viewport widths so it can pick the right one before layout is known.

  • #Performance
  • #Images
  • #Core Web Vitals

Latest updates

The four most recent posts across StackSignal.

All posts