How to use webp image format in HTML
Asked Answered
N

2

23

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

Newsom answered 5/7, 2017 at 6:26 Comment(2)
Read this developers.google.com/speed/webpDragline
I know its old but... caniuse.com/webpSchoolbag
S
55

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>
Silversmith answered 5/7, 2017 at 6:52 Comment(4)
Thanks, but I need to use in Firefox browser and I want to dont change image format for displayNewsom
There's not much you can do thought, firefox just doesn't support it yet.Silversmith
Do you need the other <source> if the jpg one is already in the <img>?Dapsang
@Dapsang Yes, you really need the second source, 'cause on some version of safari, it is not supported well.Brazzaville
W
4

What if it doesn't find the image for the specified path, how can I show a no picture image in that case, something similar to this:

<img src="img.jpg" onerror="this.src = 'nopicture.gif';"/>
Wolpert answered 21/3, 2019 at 13:8 Comment(1)
onerror is deprecated (source).Kiangsu

© 2022 - 2024 — McMap. All rights reserved.