These two files hold the same photo, 1200 × 900, and on screen you can barely tell them apart. One is about 180 KB, the other about 80 KB. The only difference is the format: JPEG and AVIF.
An image format is the way a file stores its pixels. It's like packing clothes in a suitcase: folded normally (JPEG), rolled tight (WebP), or in a vacuum bag (AVIF). Same clothes, usually less and less space. But an old wardrobe (an old browser) can only handle normal folding, so you still pack a normal version as a backup.
Four terms first
- Raster: an image made of little squares of pixels, like a photo. JPEG, PNG, WebP and AVIF are all raster.
- Vector: an image stored as formulas for lines and shapes, so it stays sharp at any size. Its format is SVG.
- Lossy throws away detail you can barely see to make the file small. Lossless keeps every pixel exactly the same.
- Transparency (an alpha channel) means part of the image is see-through, so the page behind it shows.
| Format | Compression | Transparent | Good for | Browser support |
|---|---|---|---|---|
| JPEG | lossy | no | photos | all |
| PNG | lossless | yes | logos, icons, screenshots, text | all |
| WebP | lossy and lossless | yes | photos and graphics | all modern browsers |
| AVIF | lossy and lossless | yes | photos | modern browsers, Safari since version 16 |
| SVG | vector | yes | logos, icons, simple illustrations | all |
How much it saves
The 1200 × 900 photo from the Lab, at the same visible quality:
| Format | Size | Compared to JPEG |
|---|---|---|
| JPEG | about 180 KB | the baseline |
| WebP | about 120 KB | a third smaller |
| AVIF | about 80 KB | more than half smaller |
| PNG | about 1.4 MB | almost 8 times bigger |
The general rule: WebP is about 25 to 35% smaller than JPEG at the same quality. AVIF is about half, and for many photos it can be 2 to 3 times smaller (saving up to about 70%). So a 1920 × 1080 photo that's 150 KB as a JPEG becomes roughly 100 KB as WebP, and can drop to about 45 KB as AVIF. PNG is the wrong pick for a photo: lossless means every tiny detail is kept, so the file balloons.
The fallback order with <picture>
You already know <picture> from the Responsive Images lesson. The browser reads the <source> elements from the top, skips any whose type it doesn't know, and uses the first one it supports:
<picture>
<!-- leanest first -->
<source srcset="photo.avif" type="image/avif">
<source srcset="photo.webp" type="image/webp">
<!-- the fallback for every browser, plus alt and size -->
<img src="photo.jpg" width="1200" height="900" alt="Rice fields in the morning">
</picture>
| Browser | AVIF? | WebP? | What it downloads |
|---|---|---|---|
| Latest Chrome | yes | yes | photo.avif, about 80 KB |
| Safari 15 | no | yes | photo.webp, about 120 KB |
| A very old browser (IE 11) | no | no | photo.jpg, about 180 KB |
- Each browser downloads only one file. Skipped sources aren't downloaded at all.
typeis what lets a browser skip a format it doesn't know without trying it.- The order sets the priority: the smallest format goes at the top.
Pick the format for the content
- Photos: AVIF, then WebP, with JPEG as the fallback on the
<img>. - Logos, icons, simple illustrations: SVG if there's a vector version. It's often just a few KB and sharp on any screen.
- Raster logos or screenshots with text and transparency: PNG, or lossless WebP, which is usually about a quarter smaller. JPEG has no transparency and blurs the edges of text.
- Animations: video, not GIF. A 2.5 MB GIF often becomes about 250 KB as an MP4:
<video autoplay muted loop playsinline src="loading.mp4"></video>.
So for a transparent logo with sharp text: SVG if the logo is a vector, PNG if you only have it as an image. JPEG can't do it.
To convert, use squoosh.app in the browser for a file or two, or sharp in Node.js for many files at once during the build.
⚠️ Broken images, bloated files, or an AVIF nobody uses
- A
<source>withouttype. A browser that doesn't know AVIF still picks it, downloads it, then fails to open it. The result is a broken image, not the JPEG fallback. - The order the wrong way round. If WebP sits above AVIF, Chrome, which can do both, takes WebP, and the AVIF is never used.
- Photos saved as PNG. Many times bigger without looking any better.
- Converting from a badly compressed file. Compressing lossily twice piles up the damage. Convert from the high-quality original.
TL;DR: For a 1200 × 900 photo: JPEG about 180 KB, WebP about 120 KB (usually 25 to 35% smaller), AVIF about 80 KB (about half, and it can be 2 to 3 times smaller). Serve them with <picture>: an AVIF <source> with type="image/avif", then a WebP <source> with type="image/webp", then a JPEG <img> as the fallback; each browser downloads the first file it supports, and only that one. Use SVG for logos and icons, PNG (or lossless WebP) for images with text or transparency, video for animations, and never save a photo as PNG.
⚡ Same photo, different file
Guess first: at the same visible quality, how much smaller is a WebP usually than a JPEG?
About 25 to 35%. AVIF is about half, and can be 2 to 3 times smaller. The 1200 × 900 photo in the Lab: JPEG about 180 KB, WebP 120 KB, AVIF 80 KB.
Meaning: A format is the way a file stores its pixels. Lossy throws away detail you can barely see, lossless keeps every pixel exactly the same.
⚡ Leanest on top
Write:
<picture>
<source srcset="photo.avif" type="image/avif">
<source srcset="photo.webp" type="image/webp">
<img src="photo.jpg" width="1200" height="900" alt="Rice fields in the morning">
</picture>
Meaning: The browser reads the <source> elements from the top, skips any whose type it doesn't know, and downloads only the first file it supports. Without type, an old browser still takes the AVIF and the image breaks.
⚡ A format for the content
Meaning: Photos get AVIF, then WebP, with JPEG as the fallback. Logos and icons get SVG. Images with text or transparency get PNG (or lossless WebP). Animations get video, not GIF. Never save a photo as PNG.