Sub topic 9
AVIF: higher compression, with trade-offs
AVIF is an image format built on the AV1 video codec. It usually produces smaller files than JPEG and WebP at similar visual quality. This chapter covers what AVIF does well, where it costs you, and how to export it safely. It assumes you already know the difference between lossy and lossless compression and have read the JPEG and WebP chapters.
What AVIF is
AVIF stands for AV1 Image File Format. It stores a single image, or a short animation, using the same compression ideas as the AV1 video codec. Like WebP, it supports both lossy and lossless modes, and it supports transparency.
AVIF also supports features the older web formats handle poorly or not at all:
- High bit depth. It can store 10 or 12 bits per color channel instead of 8. This reduces visible banding in smooth gradients such as skies and soft shadows.
- Wide color and HDR. It can carry color spaces wider than sRGB and high dynamic range data. For ordinary web work you will still export in sRGB.
- Alpha transparency. Lossy AVIF can keep a smooth transparent edge, which makes it a candidate to replace PNG for photographic cutouts.
Where AVIF wins
On photographs, AVIF often reaches the same perceived quality as JPEG at roughly half the file size, and it frequently beats WebP by a smaller but still useful margin. The exact savings depend on the image, so treat any figure as a typical range and not a promise.
AVIF is strongest on:
- Photographs with smooth areas, such as skies, skin, and blurred backgrounds.
- Large hero images, where a saving of 100 KB or more affects load time.
- Photographic images that need transparency.
At very low quality settings, AVIF tends to smooth detail into a soft, painterly look. JPEG at the same file size shows blocky squares instead. Many people find the AVIF failure mode less ugly, but fine texture such as hair, fabric, and film grain can disappear.
The trade-offs
AVIF is not a drop-in upgrade. Check each of these before you commit to it.
| Concern | What to expect |
|---|---|
| Encoding time | Much slower than JPEG or WebP. Large images can take seconds each at high effort settings. |
| Browser support | Current versions of Chrome, Firefox, Safari, and Edge decode AVIF. Older browsers and some embedded web views do not. |
| Tool support | Some design tools, image viewers, and email clients cannot open or preview AVIF files. |
| Fine detail | Can lose texture and grain at moderate quality settings. |
| Small images | The format has fixed overhead, so tiny images such as icons gain little over WebP or PNG. |
| Animation | Supported, but tool and browser handling is less consistent than for video or animated WebP. |
Decoding is also somewhat more expensive than for JPEG. On low-end phones, many large AVIF images on one page can add CPU work that the file-size savings do not repay.
Always provide a fallback
Because support is not universal, serve AVIF through a picture element with a fallback. The browser uses the first source it supports and ignores the rest.
<picture>
<source type="image/avif" srcset="hero.avif">
<source type="image/webp" srcset="hero.webp">
<img src="hero.jpg" width="800" height="450" alt="Team working at a shared desk">
</picture>The img element at the end is required. It is the fallback for browsers that support neither format, and it also carries the alt text and the width and height that reserve layout space.
This costs you three exports per image. Whether the saving justifies that work is a decision for each image, which the budget section below helps with.
Exporting AVIF
Many design tools now export AVIF directly, but support varies by tool and version. If yours does not, a command-line encoder works well. This example uses avifenc, from the libavif project.
avifenc --min 20 --max 40 --speed 6 hero-1600.png hero-1600.avifThe options mean the following:
--minand--maxset the quantizer range. Lower numbers mean higher quality and bigger files. A range around 20 to 40 is a reasonable starting point for photographs.--speedruns from 0 (slowest, smallest file) to 10 (fastest, larger file). Values around 4 to 6 balance time and size.
Quantizer ranges are a different scale from the 0 to 100 quality sliders in design tools and in cwebp. A quality of 80 in WebP does not match any particular AVIF setting. Compare results by eye and by file size, as you did for the other formats.
Choosing a quality setting
Follow the same process as in the compression chapter, with one adjustment: AVIF hides problems differently, so zoom in on the areas most likely to suffer.
- Export at a middle setting.
- View the result at 100% zoom, then at the size it will display on the page.
- Check fine texture, text in the image, and smooth gradients.
- Raise quality if texture looks smeared or gradients show banding. Lower it if you cannot see a difference.
- Compare the file size against the same image as WebP and JPEG. If AVIF saves little, skip it for that image.
When to skip AVIF
- Icons, logos, and flat illustrations. Use SVG or PNG. AVIF has no real advantage there.
- Images under a few kilobytes. The saving is too small to repay a third export.
- Images that must open in any tool. If clients or colleagues will open the files in email or older software, JPEG or PNG is safer.
- Pages with a tight CPU budget. If your audience uses low-end devices, test decoding cost before adding many large AVIF files.
Common mistakes
- Serving AVIF with no fallback, so older browsers show a broken image.
- Putting the
imgelement before thesourceelements, or leaving it out. Theimgmust come last and is mandatory. - Reusing a WebP quality number as an AVIF setting.
- Judging quality only at a zoomed-out view, where lost texture is invisible.
- Exporting AVIF for every image without comparing sizes, then discovering that some files are not smaller.
Exercise
You export a 1600-pixel-wide photograph as JPEG (240 KB), WebP (170 KB), and AVIF (120 KB). The AVIF version shows smeared fabric texture when viewed at 100% zoom, but looks fine at its displayed size. What should you do, and what must the HTML contain?
Show answer
Check the file at the size it will display, since that is what visitors see. If the texture still looks acceptable there, keep the AVIF. If you want to be safe, lower the quantizer range slightly and re-compare, accepting a larger file. The HTML should use a picture element with an AVIF source first, a WebP source second, and an img element last that points to the JPEG and carries width, height, and alt. The img fallback is required for browsers that support neither AVIF nor WebP.
Exercise
A teammate says: "Our AVIF quality is 80 in the tool, so set avifenc to 80 too." Why is this a problem?
Show answer
avifenc's quantizer values are a different scale where lower numbers mean higher quality, and tool sliders vary between programs. A number does not carry over between encoders or formats. Pick the setting by comparing visual results and file sizes for the actual image.