Cross-browser Webp images support

后端 未结 1 1526
情话喂你
情话喂你 2021-01-05 13:51

I have a problem. In my HTML pages, I have a lot of Webp images that I can visualize only using Google Chrome. With other browsers like Mozilla, <

相关标签:
1条回答
  • 2021-01-05 14:26

    You need to fallback to a supported image format.

    Below's example is using the <picture> element and the <source> elements with an img element fallback. The browser will try loading the assets inside the <picture> element from top to bottom until all the "conditions were satisfied" (optional sizes attribute and complex srcset which aren't in the below code) and the content format is supported.

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

    If the images are used in CSS:

    You can use Modernizr's .no-webp class name to target non-support browsers and serve a non-webp format instead:

    .no-webp .elementWithBackgroundImage {
      background-image: url("image.jpg");
    }
    

    This article has good information you can use


    Detecting browser WEBP support - A guide by Google (creator of WEBP)

    0 讨论(0)
提交回复
热议问题