Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy Loading HTML5 picture tag

I have been searching (unsuccessfully) for a reliable method to lazy load images while using the HTML5 spec for <picture>. Most solutions/plugins out there currently rely on using data- attributes. I could be wrong, but it doesn't seem this method will work in conjunction w/ <picture>.

I'm really just looking to be pointed in the right direction. If anyone has a solution that they're currently using, I'd love to see. Thanks!

Here is standard markup per the HTML5 spec:

<picture width="500" height="500">     <source media="(min-width: 45em)" src="large.jpg">     <source media="(min-width: 18em)" src="med.jpg">     <source src="small.jpg">     <img src="small.jpg" alt=""> </picture> 
like image 678
MrRay Avatar asked Jun 03 '14 21:06

MrRay


People also ask

What is loading lazy in IMG tag?

The loading attribute specifies whether a browser should load an image immediately or to defer loading of off-screen images until for example the user scrolls near them. Tip: Add loading="lazy" only to images which are positioned below the fold.

How do you make a picture lazy load?

Chrome and Firefox both support lazy-loading with the loading attribute. This attribute can be added to <img> elements, and also to <iframe> elements. A value of lazy tells the browser to load the image immediately if it is in the viewport, and to fetch other images when the user scrolls near them.

Should I lazy load images?

You should always use lazy loading for the images below the fold. As we explained, lazy loading will reduce the real and perceived loading time. User experience will benefit from it — and you users will thank you.

How do you lazy load an image in CSS?

We attach the observer on all the images to be lazy loaded. Once the API detects that the element has entered the viewport, using the isIntersecting property, we pick the URL from the data-src attribute and move it to the src attribute for the browser to trigger the image load.


1 Answers

It's February 2020 now and I'm pleased to report that Google Chrome, Microsoft Edge (the Chromium-based Edge), and Mozilla Firefox all support the new loading="lazy" attribute. The only modern browser hold-out is Apple's Safari (both iOS Safari and macOS Safari) but they've recently finished adding it to Safari's codebase, so I expect it will be released sometime this year.

The loading="lazy" attribute is only for the <img /> element (and not <picture>) but remember that the <picture> element does not represent the actual replaced-content, the <img /> element does (i.e. the image that users see is always rendered by the <img /> element, the <picture> element just means that the browser can change the <img src="" /> attribute. From the HTML5 spec as of February 2020 (emphasis mine):

The picture element is somewhat different from the similar-looking video and audio elements. While all of them contain source elements, the source element's src attribute has no meaning when the element is nested within a picture element, and the resource selection algorithm is different. Also, the picture element itself does not display anything; it merely provides a context for its contained img element that enables it to choose from multiple URLs.

So doing this should just work:

<picture width="500" height="500">     <source media="(min-width: 45em)" srcset="large.jpg" />     <source media="(min-width: 18em)" srcset="med.jpg" />     <source src="small.jpg" />     <img src="small.jpg" alt="Photo of a turboencabulator" loading="lazy" /> </picture> 
like image 108
Dai Avatar answered Sep 23 '22 11:09

Dai