Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot get HTML5 Picture Source to work correctly

I’m testing out the following HTML5 Picture element, but I can’t get it to show the correct source.

I would like the default image type to be ‘webp’, then ‘jpg’ as a fallback.

In this test, the webp image does not exist, so I was expecting the jpg to load instead.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But when trying on Chrome and Safari, it only fails to find the first source and does not try to load the existing jpg. So the picture is either blank or has a question mark icon.

Is there something wrong with the following code?

<picture class="item-slide" data-slideid="1">
  <source srcset="https://imgnotexist.com/test.webp" type="image/webp" />
  <img src="https://i.stack.imgur.com/QeSoC.jpg" />
</picture>

>Solution :

The <img> is only used if none of the <source> tags matches the media selection. If a <source> tag matches, as yours does, then that’s the one that will be displayed. In your example, the <img> will never be used.

There’s no way to do an "image does not exist" fallback without using Javascript. That seems like a strange desire.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading