Lazy Loading Images
Lazy loading defers fetching images that are outside the initial viewport, reducing initial network and rendering work. It can improve page startup performance when applied to images users are unlikely to see immediately.
Native lazy loading is enabled with `loading="lazy"` on an `<img>` element. The browser can delay fetching that image until it is sufficiently close to the viewport, rather than treating every image as an immediate resource.
Lazy loading is most useful for below-the-fold content such as article images, product listings, galleries, and long pages. Images visible during the initial render generally should not be lazy-loaded because delaying them can increase the time before important visual content appears.
Lazy loading reduces unnecessary work when users never scroll far enough to view deferred images. It can lower initial network activity and reduce resource competition during page startup, although the exact timing of downloads is controlled by the browser.
For responsive images, combine lazy loading with `srcset` and `sizes` so the browser can select an appropriate image resource. Always provide meaningful `alt` text and dimensions or an aspect-ratio strategy to reduce layout shifts while the image loads.
Key points
- Use `loading="lazy"` for images that are initially offscreen.
- Avoid lazy-loading important above-the-fold images.
- The browser decides when a lazy image is actually fetched.
- Combine lazy loading with responsive image techniques such as `srcset` and `sizes`.
- Reserve image space with dimensions or CSS aspect ratio to help prevent layout shifts.
Interview questions
Should every image on a page use lazy loading?
No. Images that are important to the initial viewport should generally be available without lazy-loading delays. Below-the-fold images are stronger candidates because users may never request them by scrolling. The decision should consider the image's position and importance to the initial experience.
What is lazy loading for images, and why would you use it?
Lazy loading allows the browser to postpone fetching images that are not immediately needed. This can reduce initial network activity and rendering work, especially on long pages. It is commonly enabled with `loading="lazy"` on an image. I would generally reserve it for content that starts outside the initial viewport.
How do you enable native lazy loading in HTML?
Add `loading="lazy"` to the `<img>` element. The browser then has permission to defer fetching the image when it is not immediately needed. The browser still controls the exact loading behavior, so the attribute should not be treated as a precise scroll-trigger mechanism.
What other techniques should be combined with lazy loading for image performance?
I would also use responsive images with `srcset` and `sizes` so the browser can choose an appropriate resource. Compression and modern image formats can reduce transfer size. Explicit dimensions or an aspect ratio help preserve layout space while images load. For important images, I would also consider loading priority separately from lazy-loading decisions.
What trade-offs or limitations should you consider with native image lazy loading?
Native lazy loading is intentionally declarative, so the browser determines when a deferred resource becomes eligible for fetching. That makes it simpler than maintaining custom scroll observers, but it provides less application-level control over exact timing. Poorly chosen lazy-loading boundaries can delay important imagery, while overly aggressive eager loading can waste startup resources. A robust strategy therefore classifies images by visibility and importance and combines loading behavior with responsive sizing and priority hints where appropriate.