Picking the right image format is one of the highest-leverage performance decisions you can make. The wrong format wastes bandwidth, slows pages, and degrades visual quality. This guide compares the major web image formats and gives you a decision framework.
Format Comparison
| Format | Type | Transparency | Best for | Notes |
|---|---|---|---|---|
| JPEG | Lossy bitmap | No | Photographs | Universal support; tunable quality |
| PNG | Lossless bitmap | Yes | Screenshots, icons, line art | Larger files; perfect for sharp edges |
| WebP | Lossy or lossless | Yes | Most web images | Compare encoded size and quality |
| AVIF | Lossy or lossless | Yes | Hero images, large assets | Compare encoded size and quality |
| SVG | Vector XML | Yes | Logos, icons, illustrations | Infinite scale; CSS-stylable |
| GIF | Lossless animation | 1-bit | Legacy only | MP4/WebM often worth comparing |
The Decision Framework
- Is it a logo, icon, or geometric illustration? → SVG.
- Does it need to be a small animation? → MP4 or WebM, not GIF.
- Is it a photograph or photographic asset? → AVIF primary, WebP fallback, JPEG for legacy.
- Does it need transparency or pixel-perfect edges (screenshots, UI assets)? → WebP lossless primary, PNG fallback.
- Is it a complex composite that needs both? → Use
<picture>with AVIF → WebP → JPEG/PNG fallback.
The <picture> Pattern
<picture>
<source srcset="hero.avif" type="image/avif">
<source srcset="hero.webp" type="image/webp">
<img src="hero.jpg" alt="..." width="1600" height="900" loading="lazy">
</picture>
With picture, the browser considers source order, media conditions, supported types and candidate selection rules. Keep the required img fallback; its sizing and alternative text still matter.
Typical Savings
- JPEG → WebP: Savings depend on image and encoder settings.
- JPEG → AVIF: Savings depend on image and encoder settings.
- PNG → WebP lossless: Compare actual encoded sizes.
- GIF animation → MP4: Compare size, playback and compatibility.
Getting Compression Quality Right
Format choice sets the ceiling; the quality setting decides where you land under it. A few rules that hold up in practice:
- Photos (lossy): Compare several encoder settings at the intended display size. Quality scales vary by codec and encoder; no universal 75–85 setting is visually identical, and artifacts can occur at any lossy setting.
- AVIF holds up lower: quality 50–60 often matches JPEG 80, so push it harder than you would a JPEG.
- Flat graphics, screenshots, text: use lossless (PNG or WebP-lossless). Lossy compression turns sharp edges and coloured text into visible halos.
- Resize before you compress. Serving a 4000px image into an 800px slot wastes far more bytes than any quality tweak — match pixel dimensions to the display size first.
- Reserve the intended image space using dimensions or aspect-ratio. Consider lazy loading for noncritical offscreen images, but not an important immediately visible image; measure the result.