Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WEBP image fall back

I searched a lot on the internet & can't find the right example or complete tutorial on the web which can full prof. So guys please suggest me some good examples.

I have tried WEBP code on lots or website like use with modernizer, checking browser support, or using the background image.

like image 491
Harden Rahul Avatar asked Dec 02 '17 11:12

Harden Rahul


People also ask

Is WebP supported by all browsers 2022?

As of 2022, WebP has a 97% market share among web browsers. This includes Google Chrome, Safari, Firefox, Edge, and Opera. CMSs like Webflow and graphics tools like Photoshop now commonly support WebP images as well.

Is WebP lossy?

WebP is a modern image format that provides superior lossless and lossy compression for images on the web. Using WebP, webmasters and web developers can create smaller, richer images that make the web faster. WebP lossless images are 26% smaller in size compared to PNGs.

Why is WebP not showing images?

There are a few reasons why WebP will not be displayed using picture tags: background images added via CSS. image is missing a WebP version (check by opening the image in browser and append “. webp” to its' URL)


3 Answers

There is an article on stucox where you can find some examples using webp images with modernizr and fallback images. This should cover your problem.

/* Result pending */
.js .container {
   background-image: none;
}
/* No JS / WebP not supported */
   .no-js .container,
   .js.no-webp .container {
   background-image: url('image.jpg');
}
/* WebP supported */
   .js.webp .container {
   background-image: url('image.webp');
}

http://www.stucox.com/blog/using-webp-with-modernizr/

like image 104
Honsa Stunna Avatar answered Oct 02 '22 16:10

Honsa Stunna


You can make use of the onerror event handler and fix it.

Here is a sample code

<img src="image.webp" onerror="this.onerror=null; this.src='image.png'">
like image 21
Kolappan N Avatar answered Oct 02 '22 15:10

Kolappan N


You should use picture html element, as described by w3schools:

<picture>
  <source type="image/webp"  srcset="img_pink_flowers.webp">
  <source srcset="img_white_flower.jpg">
  <img src="img_orange_flowers.jpg" alt="Flowers" style="width:auto;">
</picture>

The last img declaration is a fallback for incompatible browsers. The order of sources define the importance of each format. So, if the first is available in its browser, will load the first one.

like image 22
Lu Chinke Avatar answered Oct 02 '22 16:10

Lu Chinke