Lazy Loading Images: How It Works, When to Use It and When It Hurts
In this article
Lazy loading means the browser does not download an image until the visitor is about to scroll it into view. It takes one attribute, loading="lazy", on the img element, and every current browser supports it without any JavaScript. On a long page with many pictures it cuts the first load to the handful of images the visitor can actually see.
The rule for using it is short. Lazy load everything below the first screen. Never lazy load the image at the top, because that image is almost always the one Google times for Largest Contentful Paint, and deferring it makes the page measurably slower. Give every lazy image a width and height so the page does not jump when the file arrives.
Get those two things wrong and lazy loading makes a page slower and jumpier than it was before. Get them right and it is the cheapest performance win on most sites. The rest of this post explains what the browser is actually doing, where to use it, and how we set it up in Django templates.
What the browser does with the loading attribute
Without the attribute, the browser's parser requests every image the moment it reads the img tag, in document order. That happens whether the image is at the top of the page or three screens down. On a product listing with sixty photos that is sixty downloads competing for the same connection. The ones the visitor can see get no priority over the ones they cannot.
With loading="lazy", the browser reads the tag, notes where the image will sit on the page, and does nothing else. Once the page has been laid out it checks how far each lazy image is from the viewport. When the visitor scrolls to within a set distance of one, the browser requests it, so that by the time it comes into view the file has usually arrived.
The Chrome team's documentation gives that distance as 1,250 pixels on a fast connection and 2,500 pixels on a slow one. On a phone the next screen or two of images is always in flight. Firefox and Safari use their own thresholds, but the behaviour is the same.
Two consequences follow from this mechanism. The browser needs to know where the image is before it can decide. So it waits for the stylesheet and the layout before fetching any lazy image, even one in the first screen. And the browser needs to know how big the image is, or it cannot work out where anything below it sits. Without dimensions the whole page reflows when the file lands.
Where to use it and where not to
| Element | Setting | Why |
|---|---|---|
| Hero image or banner at the top | Eager, with fetchpriority set to high | It is the LCP element; it must start downloading immediately |
| Logo, icons, anything in the header | Eager (the default) | Visible on arrival, small, and needed for the first paint |
| First row of a product or portfolio grid | Eager | In the viewport on most phones; deferring it only adds delay |
| Rest of the grid | Lazy | Off screen on arrival; this is where the saving is |
| Images inside an article | Lazy | Most readers never reach the bottom |
| Footer logos, badges, partner marks | Lazy | Rarely seen, often many small files |
| Carousel slides | First slide eager, others lazy | Only the first is visible, if the carousel is kept at all |
| Maps and video embeds (iframes) | Lazy, or a thumbnail that loads the embed on click | An embed pulls in far more than an image does |
| CSS background images | Not supported by the attribute | Use an img element instead, or JavaScript |
The two mistakes that make lazy loading slower
Lazy loading the LCP image
Page builders and some plugins add loading="lazy" to every image on the site, hero included. The browser then waits for the stylesheet, lays out the page, discovers the hero is in view, and only then requests it. It could have started the download the moment it read the HTML. Lighthouse flags this directly with an audit that reports the LCP image was lazily loaded. The fix is to remove the attribute from that one image and add fetchpriority="high" instead. Our post on what LCP is and how to fix it goes through the rest of the chain.
No width and height
A lazy image with no dimensions takes up no space until it loads. The text below it renders high on the page, then jumps down when the image arrives. That jump is a layout shift, and Google's CLS metric counts every one of them, scored by how much of the screen moved and how far. Put the real pixel dimensions on every img; the browser uses them to reserve a box of the right proportions even when CSS scales the image to fit its container. Our Core Web Vitals guide covers how CLS is scored and read in the reports.
<img src="/media/cutters/dinosaur-800.webp" width="800" height="800" loading="lazy" alt="Dinosaur cookie cutter">
Lazy loading with responsive images and modern formats
Lazy loading decides when an image downloads. It does nothing about how big the file is, and on most sites the file size is the larger problem. The two work together: srcset and sizes let the browser pick a file no wider than the space it will fill. A picture element lets it pick a modern format where supported.
<picture>
<source type="image/avif" srcset="/media/p/cutter-400.avif 400w, /media/p/cutter-800.avif 800w">
<source type="image/webp" srcset="/media/p/cutter-400.webp 400w, /media/p/cutter-800.webp 800w">
<img src="/media/p/cutter-800.jpg"
srcset="/media/p/cutter-400.jpg 400w, /media/p/cutter-800.jpg 800w"
sizes="(max-width: 600px) 50vw, 25vw"
width="800" height="800" loading="lazy" alt="Cookie cutter">
</picture>
Which format to generate depends on the picture. Photographs compress best as AVIF, graphics with flat colour often do better as WebP, and JPEG XL, now shown by Chrome, Firefox and Safari but not Edge, still needs a fallback. Our comparison of WebP, AVIF and JPEG XL goes through the trade-offs. The image format tester shows the file size of your own photo in each format before you change anything. Because the browser chooses from the list, a phone on a narrow screen downloads the 400 pixel AVIF and a desktop the 800 pixel one, and neither downloads the original.
When you need JavaScript
The attribute only works on img and iframe elements. For CSS background images, components that should not render until they are seen, or feeds that load more items as the visitor scrolls, the browser offers the Intersection Observer API. It calls your code when an element comes within a chosen distance of the viewport. That replaces the old approach of watching every scroll event, which was slow and which Google's crawler does not trigger.
const io = new IntersectionObserver((entries) => {
for (const entry of entries) {
if (entry.isIntersecting) {
entry.target.src = entry.target.dataset.src;
io.unobserve(entry.target);
}
}
}, { rootMargin: '600px 0px' });
document.querySelectorAll('img[data-src]').forEach((img) => io.observe(img));
The rootMargin value is the equivalent of the browser's built-in distance threshold: start the download 600 pixels before the element is visible. Use this only where the native attribute cannot do the job. A script that re-implements what the browser already does is one more file to download and one more thing to break.
How we do it in Django templates
In a Django build the decision belongs in the template rather than with whoever uploads the photo. Django's ImageField can store the width and height of each upload in two extra fields, so the dimensions are always available without opening the file. Resized WebP or AVIF copies can be generated when the photo is saved. Our AVIF vs JPEG XL comparison covers which format to generate. The grid template can then set eager loading for the first row and lazy for everything after it:
{% for product in products %}
<img src="{{ product.image_800.url }}"
width="{{ product.image_width }}" height="{{ product.image_height }}"
loading="{% if forloop.counter <= 4 %}eager{% else %}lazy{% endif %}"
alt="{{ product.name }}">
{% endfor %}
Four is the number of cards that fit in the first screen of that particular grid on a phone. The right number is whatever fills the first screen on your layout, which our mobile-first design checklist helps you work out.
On the online shops we build this matters most on category pages, where a shop like Stamp and Bake lists dozens of cookie cutters on one page. A shopper on a phone should get the first four photos immediately and the rest as they scroll. The same template logic applies to the gallery on a product page, which our post on product pages that convert covers from the sales side.
Questions people ask
Does lazy loading affect SEO?
Not when it is done with the native attribute or Intersection Observer. Google renders pages with a tall viewport and loads lazy images in the normal way, so they are indexed. The approach that does cause trouble is JavaScript that only loads images on a scroll event, because the crawler does not scroll and never sees them.
Should the logo be lazy loaded?
No. It is visible as soon as the page opens, it is small, and lazy loading it only delays the header. Leave anything in the first screen at the default.
Does the attribute work on iframes?
Yes, loading="lazy" on an iframe is supported in current Chrome, Firefox and Safari. For YouTube videos and maps it is still better to show a thumbnail and load the embed on click. An embed brings its own scripts and fonts with it, not just an image.
Can CSS background images be lazy loaded?
Not with the attribute. Either switch to an img element, which also lets the browser find the image earlier and lets you use srcset, or load the background with Intersection Observer as above.
What to do this week
Open your busiest page on a phone-sized window with the Network panel recording and count how many images download before you scroll. If the number is close to the total on the page, add loading="lazy" to everything below the first screen and confirm the hero does not have it. Then add width and height to any img that lacks them. Re-run PageSpeed Insights and check the audits for lazily loaded LCP images and layout shifts both come back clear. That is usually an afternoon's work and it is the whole of lazy loading done properly.