Back to HTML

Lazy Loading Images and Why It Matters

Coding Tutorials Team 8 July 2026

Browsers used to download every image on a page immediately, whether or not the visitor would ever scroll far enough to see it. On a long article that meant fetching a great deal of data nobody looked at. Native lazy loading fixed this with a single attribute.

The attribute

<img src="/img/photo.jpg" alt="A harbour at dusk" width="800" height="450" loading="lazy">

An image marked lazy is not requested until the user scrolls near it. The threshold is decided by the browser and varies with connection speed, being more generous on a slow connection so the image has time to arrive before it comes into view.

The other value is eager, which is the default and forces an immediate download. Setting it explicitly is occasionally useful for clarity, though it changes nothing on its own.

Do not lazy load what is visible on load

This is the mistake that turns an optimisation into a regression. Applying the attribute to every image, including the one at the top of the page, delays the single most important image on the site.

A lazy image is discovered later in the loading process, after the parser has worked out that it is in view, which adds a round trip before the request even begins. For an image visible without scrolling, that delay is directly visible to the user and directly measured by the largest contentful paint metric, which is one of the performance signals search engines use.

The rule is straightforward. Anything visible in the initial viewport loads eagerly. Everything below the fold loads lazily. On a template where you cannot know which is which, exempting the first image or two is a reasonable approximation.

Dimensions are not optional here

Lazy loading makes width and height attributes more important rather than less. An image that arrives long after the surrounding content has been laid out will shift everything below it unless space was reserved.

With dimensions present, the browser calculates the aspect ratio immediately and leaves a correctly sized gap. Without them, the page reflows every time the user scrolls past another image, which is exactly the jarring experience the layout stability metric penalises.

Iframes too

The same attribute works on iframes, and it is arguably more valuable there. An embedded video player or map is far heavier than an image, often pulling in scripts and further requests of its own.

<iframe src="https://example.com/embed/" loading="lazy" width="560" height="315" title="Product demonstration"></iframe>

An embed near the bottom of an article that never gets scrolled to should not be costing every visitor its full weight.

The fetchpriority attribute

A companion attribute lets you tell the browser which images matter most, independently of when they load.

<img src="/img/hero.jpg" alt="..." fetchpriority="high">

Setting a high priority on the main image promotes it ahead of other requests competing for bandwidth. Setting a low priority on images that are visible but unimportant, such as a decorative pattern, frees capacity for the things that matter.

Use it on one or two elements at most. Marking everything as high priority is identical to marking nothing.

What replaced the old approach

Before the attribute existed, lazy loading required a script that watched scroll position or used an intersection observer, swapping a placeholder source for the real one when an image approached the viewport.

That machinery is now unnecessary for the common case, and removing it is usually a straightforward win. The one thing scripts still do better is placeholder handling, such as showing a blurred low resolution preview while the full image loads. If you want that effect, a script remains involved, though it should build on the native behaviour rather than replacing it.

Placeholders and perceived speed

A blank space where an image will appear is honest but unhelpful. A low resolution placeholder, a solid colour sampled from the image, or a simple blur gives the visitor something to look at and makes the arrival feel less abrupt.

The cheapest version is a background colour on the container, which costs nothing and removes the stark empty rectangle. More elaborate approaches embed a tiny preview directly in the markup, which avoids an extra request at the cost of a little page weight.

Testing it

Open the network panel, filter to images, and load the page without scrolling. Only the images near the top should appear. Scroll slowly and watch the rest arrive.

If everything downloads immediately, the usual causes are a missing attribute, a script that has already swapped the sources, or a CSS rule that renders the images in a way the browser considers visible. Checking against the actual requests is more reliable than assuming the attribute took effect.