<picture>
<source type="image/webp" srcset="images/me.webp">
<img id="avatar" src="images/me.webp" alt="Me">
</picture>
When I use the picture element, the browser(chrome) doesn't load the image with the other resources. However, when I replace <picture> with only <img> element it works fine:
<img id="avatar" src="images/me.webp" alt="Me">
Why is that happening?
Your code will load the me.webp image if the browser supports its. If not, the jpg image will be shown (which works). Chrome 67 supports webp format, so the only issue I can figure out is that me.webp is not in the images folder.
AFAIK, <picture> is used when you want multiple possible images to use in a responsive design. The <img> at the end is the fallback. It sounds like images/me.webp isn;'t working.
Unless you are really making use of <picture>, use <img>.
Related
I'm trying to render different images in chrome and in safari. The problem is that we are using latest formats of images (like webp and jpeg2000) but safari won't accept webp. So what I would like to have is without js, if possible, a conditional rendering of sources. I pretty sure I can solve it using the "picture" tag of HTML5, or maybe a css solution but I haven't done it succesfully yet.
I tried this with picture tag:
<picture>
<source srcset="img_pink_flowers.webp">
<source srcset="img_white_flower.jp2">
<img src="img_orange_flowers.jpg" alt="Flowers" style="width:auto;">
</picture>
More details about the project is that I am using React but I do not think this is relevant for the issue. Any more info you might required will be provided!
Thanks very much for the help!
I tried to use the <picture> Tag in html to load "webp" images for faster loading times.
This works and loads the webp image:
<picture>
<source srcset="img/imageXYZ.webp" type="image/webp">
<img src="img/imageXYZ.png">
</picture>
This works and loads the png image:
<img src="img/imageXYZ.png">
But Firefox, Chrome and Edge aren't able to load the fallback image. I tested that by changing the name of the webp image to a wrong one like in the example below:
<picture>
<source srcset="img/imageXYZZZ.webp" type="image/webp">
<img src="img/imageXYZ.png">
</picture>
It just shows the "missing image symbol" like you see in the screenshot here:
Screenshot
EDIT: To get the test to work, Rodrigo Faria pointed out the right answer. For a more specific answer to my problem, see my answer.
The mistake was on my side: The fallback image does work, for example the png image is loaded on Safari, because Safari doesn't support webp images. The fallback img just doesn't work when you give the webp image a wrong name like I did with srcset="img/imageXYZZZ.webp". The reason for that is that the fallback image is loaded when the browser can't support the type (type="image/webp"). Firefox, Chrome and Edge can support the type and try to load the webp img and then don't find it because it is named wrong. But then they don't check again for the fallback image, they only do that when errors happen with "type" or "media".
The fallback image is used when none of sources matches with the attribute "media".
For your test, try to do this:
<picture>
<source srcset="img/imageXYZ.webp" type="image/webp" media="(min-width: 2800px)">
<img src="img/imageXYZ.png">
</picture>
You will see that the media doesn't match and the "img" will be used!
Look at this description: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture
I'm facing a problem, I just can't seem to find out what the problem is, I have:
<picture>
<source type="image/webp" srcset="/images/meh_logo.webp">
<img src="/images/meh_logo.png" type="image/png">
</picture>
On chrome, it's just defaulting to the png logo.
If I hover over the link in inspector, it shows the webp image.
If I open the webp image link in a new tab, it loads file.
My headers return:
image/webp,image/apng,image/,/*;q=0.8
If I change source srcset to img srcset - that will display the webp file.
Chrome: 70.0.3538.110
Tested locally on MAMP Pro and doesn't display.
The WEBP is a tree. The PNG is a rose. You used the code below...
<picture>
<source type="image/webp" srcset="https://www.gstatic.com/webp/gallery/4.sm.webp">
<img src="https://www.gstatic.com/webp/gallery3/1.sm.png" type="image/png">
</picture>
According to this source you should repeat the source, like this:
<picture>
<source srcset="https://www.gstatic.com/webp/gallery/4.sm.webp" type="image/webp">
<source srcset="https://www.gstatic.com/webp/gallery3/1.sm.png" type="image/png">
<img src="https://www.gstatic.com/webp/gallery3/1.sm.png" alt="Image" class="tm-img">
</picture>
When I run these scripts in FF and Chrome they show a tree, thus show the WEBP image. What do you see?
After reading through this question and answer, I was still a little confused, so I'd like to add this clarification: When you use the Chrome inspect tool on your image, it will still highlight the line with your <img> in it, which makes it seem that the larger file is loading. But, as you can see with the example given, the WEBP is actually what is loading and showing on the screen, because neither snippet shows the photograph of a rose found here.
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, Explore, and Safari I can't see them.
Is there a solution? Maybe without change every image's extension (cause they are a lot)?
Thanks
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)
I'm trying to migrate to using WebP images on our website.
We have quite a few images in our S3 buckets, so it would take a while to convert all of them to webp. Instead, I'd like to show webp only if the webp version is available (it'll have a deterministic URL structure); and if not, show the original JPG/PNG.
When I try the following, it doesn't work (don't worry the icons on the top right. also webpImageUrl and originalImageUrl have the correct values):
<picture>
<source srcSet={`${webpImageUrl}`} type="image/webp" />
<img src={`${originalImageUrl}`} />
</picture>
Thanks in advance!