Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native lazy-loading (loading=lazy) not working even with flags enabled

I am currently trying to update my Website using the new loading="lazy" attribute as shown here: https://web.dev/native-lazy-loading

As seen in the video, everything works as expected, but compared with my waterfall diagram in chrome, it doesn't.

How it looks: chrome developer tools

How it should look:

chrome developer tools

This is how its implemented:

<img class="has-border" src="https://andreramoncombucket.s3.amazonaws.com/static/assets/img/work/personal-website/pw_full.jpg" style="object-fit: cover;" alt="..." loading="lazy">
like image 801
André Ramon Avatar asked Sep 02 '19 07:09

André Ramon


People also ask

How do I know if Lazyload is working?

If you're not sure if lazy loading is working correctly, you can open the Chrome DevTools and check that the images are not loaded until the scroll.

Why does angular lazy loading not work?

It's because you haven't added routing to your student module. The student module needs to use . forChild() when declaring routes since it is a child module. Save this answer.

How do you activate lazy loading?

To enable lazy load in WordPress, go to the “Perfmatters” settings in your WordPress admin dashboard. Click on the “Lazy Loading” tab. There are a few different options you have when it comes to lazy loading. They can also be used together at the same time.

How do I enable lazy loading in HTML?

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.


1 Answers

I had a similar problem, and after some research, I found the solution:

Just need to add width and height to the IMG tag, this is because the browser needs to know the size of the element before applying lazy loading.

Your code:

<img class="has-border" src="..." style="object-fit: cover;" alt="..." loading="lazy">

Your code after adding width and height:

<img class="has-border" src="..." style="object-fit: cover;" alt="..." loading="lazy" width="200px" height="200px">

Another alternative is using inline style:

<img class="has-border" src="..." style="object-fit:cover; height:200px; width:200px;" alt="..." loading="lazy">

Take into consideration I just randomly set the dimensions of the IMG tag to 200px. You can find more information on this web.dev article

hope it helps 👍

like image 191
Jonathan Rangel Bernal Avatar answered Nov 03 '22 01:11

Jonathan Rangel Bernal