Image Optimization for the Modern Web
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.
srcsetandsizesexist to stop the browser downloading a 2000px image for a 400px slot — the most common source of wasted image bytes.
Dimensions come before format
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
| Format | Best for | Trade-off |
|---|---|---|
| AVIF | Photographs, complex images — smallest file size | Slower to encode; slightly lower browser support than WebP |
| WebP | General-purpose replacement for JPEG/PNG | Larger than AVIF, but near-universal support and fast encoding |
| JPEG | Fallback for the rare unsupported browser | Largest file size of the three |
| SVG | Icons, logos, illustrations with flat colors | Not 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.
| Image | Correct treatment | Why |
|---|---|---|
| Hero / largest above-the-fold image | fetchpriority="high", no loading="lazy" | This is very likely your LCP element — delaying it delays your LCP score directly |
| Below-the-fold images | loading="lazy" | Downloading them early competes for bandwidth with content the user can actually see |
| Background images via CSS | Avoid for LCP-critical content | The 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
- Resize the source image to the largest size it will actually render at — not the camera original.
- Serve AVIF with WebP and JPEG fallbacks via
<picture>. - Use
srcset+sizesfor anything that renders at different widths across breakpoints. - Set explicit
widthandheighton every image, every time. fetchpriority="high"and eager-loading on the hero image only;loading="lazy"everywhere else.- 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.