Tips for rolling your own lazy loading | CSS-Tricks

You may have heard (or even issued the call) that “we can just use lazy loading!” when looking for a way to slim down a particularly heavy web page.

Lazy loading is a popular technique for gradually requesting images as they come into view, rather than all at once after the HTML of the page has been parsed. It can reduce the initial page weight, and help us hit our performance budgets[1] by requesting images when they're needed.

It can be effective. But it also comes with some baggage of its own. We’ll get to that! In fact, Rahul Nanwani did an extensive write-up that hits several lazy-loading methods[2] and illustrates just how complex some are.

In this post, we’ll look at an implementation that's already been covered in brief detail in this post by Preethi[3]. We're going to expand on that so you can add your own implementation of lazy loading to your site as I’ve done on this little demo site[4].

We’ll cover these topics along the way:

Here’s what we’ll be building: