Lazy Loading Best Practices for 2025
Lazy loading is one of those features that's almost trivial to turn on and surprisingly easy to get wrong. Done right, it trims your initial page weight without the visitor ever noticing. Done wrong, it delays your hero image or makes the page jump around as you scroll. Here's how to get the upside without the side effects.
Key Takeaways
- Native lazy loading is now supported by all major browsers without the need for heavy JS libraries.
- Never lazy load your LCP (Largest Contentful Paint) image. It severely impacts SEO performance.
- Explicit width and height attributes are mandatory to prevent Cumulative Layout Shift (CLS).
- For complex backgrounds, use the lightweight Intersection Observer API instead of scroll event listeners.
1. Native is King (Usually)
You no longer need a jQuery plugin or any third-party script to defer images. The native loading="lazy" attribute works in every major browser, and it's smarter than the old scroll-listener hacks: the browser picks when to fetch each image based on scroll position, connection speed, and device, all without you writing a line of JavaScript.
<!-- Native, zero-dependency lazy loading -->
<img
src="product-image.webp"
alt="Blue running shoes"
width="600"
height="400"
loading="lazy"
/>
The #1 SEO Mistake: Lazy Loading the LCP
Never apply loading="lazy" to your Hero Image or the main image visible above the fold. Google's algorithm measures the Largest Contentful Paint (LCP) as a primary ranking signal. If you lazy-load the LCP image, the browser purposely delays downloading it until the page is almost finished rendering, which will instantly tank your LCP score. Above-the-fold images must load eagerly.
2. Preventing Cumulative Layout Shift (CLS)
If you defer an image's load time, the browser does not know how much space to allocate for that image in the document flow. When the user scrolls down and the image finally renders, it pushes all the text and elements below it down the page. This is a Cumulative Layout Shift (CLS).
To prevent this, you must provide explicit width and height attributes. Modern browsers are smart enough to look at those attributes and calculate the CSS aspect-ratio on the fly, reserving a perfectly sized transparent box while the image loads over the network.
3. Background Images and Vanilla JavaScript
The native loading="lazy" attribute only works on <img> and <iframe> tags. It does not work on CSS background-image properties (often used for complex grid layouts or parallax sections).
To lazy load background images efficiently in 2025, use the native Intersection Observer API via Vanilla JavaScript. It runs off the main thread, making it incredibly lightweight compared to traditional scroll-event listeners.
<style>
.lazy-bg { background-image: none; }
.lazy-bg.is-visible { background-image: url('heavy-background.webp'); }
</style>
<script>
document.addEventListener("DOMContentLoaded", function() {
let lazyBackgrounds = [].slice.call(document.querySelectorAll(".lazy-bg"));
if ("IntersectionObserver" in window) {
let lazyBackgroundObserver = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
entry.target.classList.add("is-visible");
lazyBackgroundObserver.unobserve(entry.target);
}
});
});
lazyBackgrounds.forEach(function(lazyBackground) {
lazyBackgroundObserver.observe(lazyBackground);
});
}
});
</script>
Conclusion
A solid lazy loading strategy comes down to a few rules: defer off-screen assets, load the LCP hero image eagerly, set explicit width and height to prevent layout shift, and use Intersection Observer for CSS background images. Get those right and you cut initial page weight without hurting the metrics Google actually measures.
Are you lazy loading WebP or JPEG?
Lazy loading a 3MB JPEG still results in a slow experience when the user scrolls. Ensure your assets are optimized to WebP first.
Optimize Images NowAbout WebPMagic
WebPMagic is an independent project focused on image optimization and web performance. These guides are researched and edited to give developers clear, practical, and accurate information for building faster websites, with tips drawn from hands-on use of our own WebP conversion tool. Found an error or have a suggestion? Let us know via our contact page.