Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use webp image format in HTML

Tags:

html

css

image

webp

I know this is a new format for images, but I don't know how to show it in HTML.

Does anyone know how I can do that? Which browsers can show this image format besides chrome?

sample-webp-image.webp

like image 901
ttrasn Avatar asked Jul 05 '17 06:07

ttrasn


People also ask

How do I use WebP in HTML?

Using WebP in HTML You can use a WebP image in a normal <img> tag, but in browsers that without WebP support the image would be broken. In the code above we have different image versions in both WebP and JPEG to support high-res displays with 2x pixel density as well as dark mode.

How do I add a WebP image to CSS?

If you already use WebP images for your HTML <img> tags and want to enable WebP for the CSS background as well, the proper way is to use multiple backgrounds in the CSS styling. In this case, all browsers will get WebP images, except for ones that do not support WebP. They will load the . png image.

How do I use WebP photos?

Starting with WordPress 5.8, you'll be able to use the WebP image format the same way as JPEG, PNG, and GIF formats. Just upload your images to your Media Library and include them in your content. As WordPress 5.8+ supports the WebP format by default, you don't have to install third-party plugins to upload WebP images.

Should I use WebP images on my website?

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.


1 Answers

You use webp like any image:

<img src="img.webp" />

However since it's not always supported (see http://caniuse.com/#feat=webp), you can use this to set a fallback:

<picture>
  <source srcset="img.webp" type="image/webp">
  <source srcset="img.jpg" type="image/jpeg"> 
  <img src="img.jpg">
</picture>
like image 119
Sveta Avatar answered Oct 14 '22 16:10

Sveta