FORMAT GUIDE6 MIN READ

Serve WebP and AVIF with a Compatible Fallback

Published

Knowing how to serve WebP and AVIF with a fallback means pairing modern image formats with a safety net for browsers, bots, or tools that cannot decode them. The two reliable methods are a picture element with ordered source candidates and explicit MIME types, or server-side content negotiation using the Accept header. Either approach lets each visitor receive a format their browser supports, without breaking the page for anyone else.

Why use modern image formats like AVIF and WebP?

AVIF and WebP offer superior compression compared to JPEG and PNG, but they require fallbacks for older browser versions that cannot decode either format. This is why a delivery strategy, not just an encoder, decides whether visitors actually benefit from the smaller files.

Browser decoding support varies by version and platform, so test the formats you plan to ship against your own audience rather than assuming universal support. A browser that renders AVIF images correctly does not prove that your conversion tool can encode AVIF; decoding and encoding are separate capabilities, and only checking the exported file type confirms the latter.

How does the picture element serve WebP and AVIF with a fallback?

The picture element lets you list several image versions and the browser picks the first one it supports, instead of downloading every candidate to compare file sizes. Each alternative sits inside a source element with a type attribute identifying its MIME type, and a standard img element closes the list as the universal fallback.

Illustrative structure (not a measured test)

  • →First candidate: an AVIF file, declared with type image/avif
  • →Second candidate: a WebP file, declared with type image/webp
  • →Fallback: a standard img pointing to a JPEG file, carrying width, height and descriptive alt text
  • →The img element is always last, so browsers that ignore picture, or don't support any listed format, still get a working image

This order is illustrative only; it is not a measured comparison of file sizes or load times, and your own images may compress differently depending on content and settings.

What is the correct order for source elements?

Browsers read source elements from top to bottom and stop at the first one whose type they support. Put the smallest expected format first, typically AVIF, then WebP, then rely on the img fallback for anything else. If you reverse this order, a browser that supports both AVIF and WebP will stop at WebP because it appears first, and it will never reach the AVIF candidate even though it could have used it.

The img element must remain the last child of picture; it is the fallback for browsers that ignore the picture tag entirely or do not support any of the listed formats.

Why is the type attribute essential for performance?

The type attribute lets the browser skip unsupported formats without downloading the file, because it reads the declared MIME type instead of fetching the image to inspect it. Omitting type, or providing only a generic value, risks the opposite outcome: the browser may need to request a file just to discover it can't use it, wasting bytes on a request that was never going to render.

How to use server-side content negotiation as an alternative

Instead of listing formats in markup, a server or image CDN can negotiate the format through the HTTP Accept header, returning the best match without touching your HTML. This works well when images are rendered dynamically or served through a CDN that already inspects request headers.

Content negotiation needs correct cache configuration. If the response doesn't vary by Accept header, a cache can serve a WebP file to a browser that only supports JPEG, or the reverse. Check the actual response, not just the filename or CDN dashboard setting: open the network panel and confirm the Content-Type header matches what that specific browser requested.

If you're choosing between formats partly for search visibility, Google Search now supports indexing AVIF files, so no special handling is required for AVIF images to appear in image search results.

ApproachWhere the choice happensMarkup changes neededCaching requirementGood fit for
Picture elementIn the HTML, per imageYes, add source elementsStandard caching rules applyStatic sites, full control over markup, infrequent image changes
Content negotiationOn the server or CDN, per requestNo, existing img tags can stayNeeds Vary: Accept header on responsesServer-rendered pages, image CDNs, large or frequently updated image libraries

Best practices for image attributes and accessibility

Whichever method you use, keep width, height and descriptive alt text on the img element. Reserved dimensions stop the layout from shifting when a different format loads, and alt text still describes the image to anyone using a screen reader, regardless of which file the browser picked.

If the image also needs to adapt across viewport widths, add srcset and sizes alongside the format candidates; format switching and resolution switching can coexist in the same picture element.

Don't lazy-load the image if it's the largest contentful paint candidate. Smaller AVIF or WebP bytes can help LCP by reducing download time, but only if the browser discovers the image early; a lazy-loading attribute on the LCP image delays that discovery regardless of format.

How to verify delivery works

  • →Open the browser's network panel and reload the page
  • →Confirm the image request shows the expected Content-Type, such as image/avif or image/webp, in a browser that supports it
  • →Load the same page in a browser without AVIF or WebP support and confirm the JPEG fallback request appears instead
  • →Compare dimensions and crop across formats so layout doesn't shift between visits
  • →Check that the image isn't marked for lazy loading if it's the largest contentful paint candidate

Limitations to keep in mind

  • →Browser decoding support varies by version and platform; confirm support for your actual audience rather than assuming it
  • →AVIF display support in a browser does not mean your conversion tool can encode AVIF files; check the exported file type directly
  • →A missing Vary: Accept header on a negotiated response can cause caches to serve the wrong format to the wrong browser
  • →Smaller image bytes can help LCP but do not directly improve TTFB or guarantee search rankings; CLS still needs reserved width and height regardless of format

Next step: pick one page with a large hero image, add picture markup with ordered AVIF, WebP and JPEG candidates (or confirm your CDN already negotiates formats), then open the network panel in two browsers, one that supports AVIF and one that doesn't, to confirm each receives an appropriate file.

Ready to optimize your images?

TRY ZIPMYIMAGES FREE →

Related guides