What is Lazy Loading?
Lazy loading is a strategy to identify resources as non-critical and load these only when needed. It’s a way to optimize web pages, which translates into reduced page load times. To read more about it: https://www.geeksforgeeks.org/what-is-lazy-loading/
Generally images are larger in size, so lazy loading can be used to defer the offscreen images.
Difference from JavaScript solutions ?
There are many JavaScript solutions to support lazy loading but recently browsers have also started to implement lazy loading of images and iframes. Using browser method is more performance but it is not fully supported right now.
Approach: To use Lazy Loading, use the loading attribute of image tag in html. Here are the supported values for the loading attribute:
- auto: Default lazy-loading behavior of the browser, which is the same as not including the attribute.
- lazy: Defer loading of the resource until it reaches a calculated distance from the viewport.
- eager: Load the resource immediately, regardless of where it’s located on the page.
Example:
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>Lazy Loading Images</title> </head> <body> <h1>Lazy Loading Images</h1> <!-- loading attribute of image tag is used to specify lazy loading --> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> <img src= alt="numbers" loading="lazy" width="300" height="300" style="display:block; margin:10px" /> </body> </html> |
Output:

Recommended Posts:
- How to design lazy loading of images/videos for mobiles using jQuery Plugin?
- What is Lazy Loading?
- How to show Page Loading div until the page has finished loading?
- How to Create Loading Blur Text Animation Effect using HTML and CSS ?
- How to Create a Dot loading Animation using HTML and CSS?
- Ways To Reduce The Loading Time Of Website | Set 1
- Ways To Reduce The Loading Time Of Your Website | Set 2
- How to load jQuery code after loading the page?
- Loading Text Animation Effect using CSS
- How to Create Skeleton Screen Loading Effect using CSS ?
- How to Create Animation Loading Bar using CSS ?
- HTML | Images
- HTML | DOM images Collection Property
- How to create a drag and drop feature for reordering the images using HTML CSS and jQueryUI ?
- Responsive images in Bootstrap with Examples
- How to set multiple background images using CSS?
- CSS | Border Images
- CSS | Styling Images
- How to download Google Images using Python
- Bootstrap 4 | Images
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


