Search code examples
amp-html

How to fallback for webp to jpg with amp-img?


I use webp with fallback for jpg like:

<picture>
  <source srcset="img/my.webp" type="image/webp">
  <img src="img/my.jpg">
</picture>

But the <picture> tag are forbidden im AMP.
How can I handle to use webp with fallback to jpg?


Solution

  • You can do this by defining a fallback image inside the amp-img:

    <amp-img alt="Mountains"
      width="550"
      height="368"
      src="images/mountains.webp">
      <amp-img alt="Mountains"
        fallback
        width="550"
        height="368"
        src="images/mountains.jpg"></amp-img>
    </amp-img>
    

    https://www.ampproject.org/docs/reference/components/amp-img#example:-specifying-a-fallback-image