Lazy loading images: how the loading attribute works?
loading
attribute worksLazy loading (loading="lazy"
) is a way to load an image only when it’s actually needed by the user — for example, when they’ve scrolled to it on the page.
Here’s what it gives you:
Previously, to load an image only when needed, we had to:
IntersectionObserver
,requestIdleCallback
, setTimeout
, scroll events,It was painful.
But now — it’s different.
With the loading
attribute, the browser decides on its own when to load an image or iframe.
<img src="pokemon.png" loading="lazy" alt="pokemon pikachu" />
💡 Works out of the box. Supported in most modern browsers.
📖 Support: caniuse.com/loading-lazy
loading
valuesValue | What it does | When to use |
---|---|---|
eager | Loads the image immediately (default) | For above-the-fold content |
lazy | Loads only when the element enters the viewport | For everything below the first view |
<picture>
Yes, it works inside the <picture>
tag:
<picture>
<source media="(min-width: 768px)" srcset="test.jpg 1x, larger.jpg 2x" />
<img src="pokemon.jpg" loading="lazy" alt="pokemon pikachu" />
</picture>
<iframe>
too<iframe
src="https://www.youtube.com/watch?v=-R_3t7sHZVA"
loading="lazy"
width="630"
height="420"
title="Pokemon video"
/>
loading="lazy"
The loading
attribute is a simple way to:
Enable it. Use it. And let your pages load not only beautifully — but fast.