AVIF is a file format based on a video codec and supports a high bit depth while maintaining a small file size. You can find many articles about how fantastic AVIF is right here, as well as anywhere else on the web. You clicked on this article because you want to learn how to use AVIF in HTML, so let's take a look at the topic.
The most important thing to keep in mind when using AVIF is that it is unfortunately not yet supported everywhere. That's a pity, isn't it? It took WebP (another excellent image format, but not good enough to keep up with AVIF) ten years to be fully supported as Apple wasn't ready to implement the format in Safari. But there is still 90% support. At the time of writing, AVIF has 64% support on browsers. Google Chrome and Opera support it. Firefox will support it from June 2021. Safari doesn't yet have AVIF support. However, AVIF is an invention of the non-profit industry consortium Alliance for Open Media AOM. Major browser giants Apple, Mozilla and Google are all part of the project, so support can be expected relatively quickly.
For now, you can still use the format in its complete glory with the native <picture> element in HTML. Why, you may ask? Well, the <picture>element allows progressive support. You candrop all image files in the order in which you want to load them. Your visitors' browsers load one image at a time, which reduces the load on the server. Besides, you don't need a script to process multiple images.
- Does not download more than one image at a time
- Native support for selecting the most appropriate image
- 96% browser support and automatic fallback
- Getting this implemented is easy and straightforward",
Currently 96% of browsers support the picture element. Even if your grandma visits your website with her disgusting Internet Explorer 6, her browser will revert to the default image format when you specify it. Have a look at the following example and copy it if you want to use it for your website:
1<picture> 2 <source srcSet="image.avif" type="image/avif" /> 3 <source srcSet="image.webp" type="image/webp" /> 4 <img 5 width="1280" height="720" decoding="async" loading="lazy" 6 src="image.jpg" alt="an avif image" /> 7</picture>
PS: The lazy loading attribute is supported by 75% of all browsers, make use of it! Also, don't forget to add width and height to avoid a Layout Shift. Providing width and height is more important than ever.
This is a nice set up for all visitors.make sure the first source tag is an AVIF image for the browser to display the first media type it can handle.
At this point, it seems great, doesn't it? Well, no, not yet, because our platform still lacks support for different types of devices. It's more difficult to create an optimal experience for Retina displays. We also want to make sure that mobile devices don't download images that are much larger than their screen.
1<picture> 2<source 3sizes="(max-width: 640px) 100vw, 640px" 4srcSet="/image-1280.avif 1280w, /image-640.avif 640w, /image-320.avif 320w" 5type="image/avif" /> 6<source 7sizes="(max-width: 640px) 100vw, 640px" 8srcSet="/image-1280.avif 1280w, /image-640.webp 640w, /image-320.webp 320w" 9type="image/webp" /> 10<source 11sizes="(max-width: 640px) 100vw, 640px" 12srcSet="/image-1280.avif 1280w, /image-640.jpg 640w, /image-320.jpg 320w" 13type="image/jpg" /> 14<img 15width="1280" height="720" decoding="async" loading="lazy" 16src="/image.jpg" alt="an avif image" /> 17</picture>
Okay, this is not as challenging as we originally thought, but creating AVIF images for any viewport of any format can be exhausting. Nevertheless, if your website does not have many images, it is a good way to prepare for the future. Please remember to provide fallback images for older browser versions or experimental use as in Firefox. Otherwise, your user experience may suffer as images are not rendered. AVIF is also good for backgrounds. Check out the CSS article.How about frameworks?Enjoy this article with the different types of frameworks you can use.