# AVIF

> The AVIF is a modern image format based on the AV1 video codec that provides high compression with good quality.

## What is AVIF and how do I use it on the web?

The image format based on the AV1 codec that usually produces smaller files than JPEG and WebP at the same quality. Supports transparency, HDR and wide color gamut, and works in all modern browsers.

- Topic: Graphics
- Type: format

## Example

Wrong:

```html
<img src="hero.avif"
  alt="Mountain lake at dawn">
```

Right:

```html
<picture>
  <source srcset="hero.avif" type="image/avif">
  <img src="hero.jpg" alt="Mountain lake at dawn" width="1200" height="800">
</picture>
```

The `<picture>` element lets browsers pick AVIF and fall back to JPEG, and the `<img>` keeps `alt` and size.

## Common mistakes

- Serving only AVIF, so browsers without support show a broken image.
- Encoding large AVIF images on the fly per request, which is slow and CPU heavy.

## Key points

- AVIF images sit inside `<picture>` with a `<source type='image/avif'>` and a JPEG or WebP `<img>` fallback.
- The server sends AVIF files with the 'image/avif' `Content-Type`.
- The fallback `<img>` keeps `alt`, `width` and `height`, since the `<source>` only swaps the file.

## Interview questions

### AVIF vs WebP: which is smaller?

AVIF is usually smaller than WebP at the same visual quality, especially for photos. WebP encodes faster, so many sites serve AVIF first and WebP or JPEG as fallback.

### Does AVIF support transparency and animation?

Yes. AVIF supports an alpha channel, HDR, wide color gamut and animated image sequences.

### Why is AVIF encoding so slow?

AV1 compression does heavy analysis to find small encodings. Encode images at build time or cache the result instead of converting on every request.

## Related cards

- [.webp](https://wonderium.dev/engineering/webp.md)
- [`<picture>`](https://wonderium.dev/html/picture.md)
- [`<source>`](https://wonderium.dev/html/source.md)
- [.jpeg](https://wonderium.dev/engineering/jpeg.md)
- [image-set()](https://wonderium.dev/css/image-set.md)

---

- Level: [Engineering](https://wonderium.dev/engineering.md)
- HTML version: https://wonderium.dev/engineering/avif
- Last updated: 2026-11-25
