Sub topic 9
Serving AVIF and WebP with a JPEG fallback
This chapter assumes you know the img element, srcset with w descriptors, and the idea of a picture element wrapping source elements. Here you will use picture to offer modern image formats while keeping a JPEG that every browser can show.
Why switch formats
AVIF and WebP usually produce smaller files than JPEG at similar visual quality. Smaller files download faster. Not every browser supports every format, so you cannot simply point src at an .avif file. Browsers that do not understand it would show a broken image.
The type attribute on source solves this. It names the MIME type of the files in that source. The browser compares the type to the formats it can decode and skips the source if it cannot.
The basic pattern
<picture>
<source type="image/avif" srcset="photo.avif">
<source type="image/webp" srcset="photo.webp">
<img src="photo.jpg" width="800" height="600" alt="A lighthouse on a rocky coast at sunset">
</picture>The browser reads the source elements from top to bottom and uses the first one whose type it supports. It never downloads the files from the sources it skips. If no source matches, it uses the img.
Order matters. Put the format you prefer first, which is usually the smallest. If you put WebP above AVIF, a browser that supports both would pick WebP and never reach the AVIF.
The img element does the real work
The img inside picture is required. It is the fallback, and it is also the element that renders the image. The chosen file is loaded into it. So alt, width, height, and loading all go on the img, not on source. Putting alt on a source has no effect.
The JPEG in src also covers older browsers that do not know picture at all. They ignore the unknown picture and source tags and see only the img.
Adding widths
Format switching combines with srcset and sizes. Each source can list several widths of the same format. Repeat the same sizes value on each source and on the img, because they all describe the same layout slot.
<picture>
<source
type="image/avif"
srcset="photo-400.avif 400w, photo-800.avif 800w, photo-1600.avif 1600w"
sizes="(min-width: 40em) 50vw, 100vw">
<source
type="image/webp"
srcset="photo-400.webp 400w, photo-800.webp 800w, photo-1600.webp 1600w"
sizes="(min-width: 40em) 50vw, 100vw">
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(min-width: 40em) 50vw, 100vw"
width="1600"
height="1200"
alt="A lighthouse on a rocky coast at sunset">
</picture>The browser first picks a source by type. Then it picks a width inside that source, using the process from the earlier chapter on how the browser selects a file.
Common errors
- Missing fallback format. If every
sourcehas atypeand theimgpoints to a format the browser cannot decode, the image fails. Keep a JPEG or PNG inimg. - Wrong MIME type. The value is
image/avif, notimage/avif-imageoravif. A wrong value makes the browser skip thesource. - Mismatched files. Each format should show the same picture with the same aspect ratio. Otherwise the layout changes depending on the browser.
- Wrong server type. Servers should send the correct
Content-Typeheader for each file.
Exercise
A page has <source type="image/webp" srcset="a.webp"> listed before <source type="image/avif" srcset="a.avif">. A browser supports both formats. Which file does it download, and how do you fix the order if you want the AVIF?
Show answer
It downloads a.webp, because the browser uses the first source whose type it supports. To get the AVIF, move the AVIF source above the WebP source.