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.
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.
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.
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)
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/
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'">
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With