Image optimization
Decide per image
| Image | Format | Loading |
|---|---|---|
| Logos, icons, illustrations with flat colors | SVG (optimized with SVGO), or an icon font/sprite | Inline or <img> |
| Photos | AVIF with WebP and JPEG fallback (or WebP + JPEG) | srcset + sizes |
| Screenshots / UI with text | WebP or PNG (lossless), AVIF if it keeps text crisp at your quality setting | srcset |
| Animated GIF | Convert to a muted, looping <video> (MP4/WebM) or animated WebP/AVIF |
<video autoplay muted loop playsinline> |
| Hero / LCP image | Best format above, preloaded by being in the HTML | fetchpriority="high", never lazy |
Markup
<!-- LCP hero: eager, high priority, explicit dimensions -->
<img src="/img/hero-1200.jpg"
srcset="/img/hero-640.avif 640w, /img/hero-1200.avif 1200w, /img/hero-1920.avif 1920w"
sizes="100vw" width="1920" height="1080" alt="..." fetchpriority="high">
<!-- below the fold: lazy -->
<img src="/img/card-480.webp" srcset="/img/card-480.webp 480w, /img/card-960.webp 960w"
sizes="(min-width: 64rem) 30vw, 90vw" width="960" height="640" alt="..." loading="lazy" decoding="async">
<!-- format fallbacks -->
<picture>
<source type="image/avif" srcset="/img/team.avif">
<source type="image/webp" srcset="/img/team.webp">
<img src="/img/team.jpg" width="1200" height="800" alt="The team at the 2026 offsite">
</picture>
Rules:
sizesmust describe the rendered width; a wrongsizesmakes the browser download the wrong file. Check in DevTools (Network panel, orcurrentSrcon the element).- Always set
widthandheight(the intrinsic ratio) or CSSaspect-ratioto avoid layout shift. - Generate widths around the real layout: typically 2-5 sizes, including 2x for the most common display size.
- Do not lazy-load images in the first viewport; it delays LCP.
- Write real
alttext (seeaccessibility-audit);alt=""for decorative images.
Build or CDN
Prefer automation over hand-exporting:
- Framework components: Next.js
<Image>, Astro<Image />/<Picture />, Nuxt Image, AngularNgOptimizedImage(they generate sizes, formats, lazy loading and dimensions). - Image CDNs that transform by URL parameters (format negotiation with
Accept, resize, quality). - Build-time:
sharp(Node),libvips,squoosh-style pipelines;svgofor SVG.
Quality starting points: AVIF around 50-65, WebP around 75-80, JPEG around 75-82; check visually on the image types that matter (faces, gradients, text).
Audit
- Lighthouse “Properly size images”, “Serve images in next-gen formats”, “Image elements do not have explicit width and height”, “Largest Contentful Paint image was lazily loaded”.
- Total image bytes per page template; set a budget (for example under 1 MB on mobile for content pages) and check it in CI if possible.
Report savings as bytes before/after and LCP before/after, not just “optimized”.