lazy loading technique
What is Lazy Loading?
Lazy Loading (also called "deferred loading" or "lazy loading") is a performance optimization technique where images, videos, and other media content are only loaded when they actually appear in the user's visible area. This technique significantly reduces the initial load time of a website and improves web performance metrics.
Core Principle of Lazy Loading
The basic principle is based on the Intersection Observer API, which detects when an element enters the user's viewport. Only at this point is the corresponding image or video loaded from the server.
Technical Implementation
Native HTML Implementation
Modern browsers support Lazy Loading natively via the loading="lazy" attribute:
<img src="bild.jpg" alt="Beschreibung" loading="lazy">
JavaScript-based Solution
For older browsers or extended functionality:
const images = document.querySelectorAll('img[data-src]');
const imageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.classList.remove('lazy');
imageObserver.unobserve(img);
}
});
});
images.forEach(img => imageObserver.observe(img));
CSS Implementation
.lazy {
opacity: 0;
transition: opacity 0.3s;
}
.lazy.loaded {
opacity: 1;
}
SEO Benefits of Lazy Loading
1. Improved Core Web Vitals
Lazy Loading has a positive impact on all important Core Web Vitals:
- Largest Contentful Paint (LCP): Reduces the load time of the largest visible element
- Input Responsiveness (FID): Reduces JavaScript execution time
- Cumulative Layout Shift (Cumulative Layout Shift): Prevents layout shifts from lazy-loaded images
2. Reduced Bounce Rate
Faster load times lead to a lower bounce rate, which Google values as a positive user experience signal.
3. Better Crawl Efficiency
Google can work more efficiently with the available crawl budget when pages load faster.
Best Practices for Lazy Loading
1. Exclude above the fold Images
Images in the visible area should be loaded immediately:
<img src="hero-bild.jpg" alt="Hero" loading="eager">
<img src="content-bild.jpg" alt="Content" loading="lazy">
2. Use Placeholders
Use placeholders to avoid layout shifts:
<img src="Placeholder.jpg" data-src="original-bild.jpg" alt="Beschreibung" loading="lazy">
3. Fallback for JavaScript
Ensure that images are also loaded without JavaScript:
<noscript>
<img src="bild.jpg" alt="Beschreibung">
</noscript>
4. Preload for Critical Images
<link rel="preload" as="image" href="kritisches-bild.jpg">
Lazy Loading for Different Content Types
Images
The most common application of Lazy Loading:
<img src="thumbnail.jpg"
data-src="original-bild.jpg"
alt="Produktbild"
loading="lazy"
class="lazy-image">
Videos
<video controls preload="none" poster="video-thumbnail.jpg">
<source data-src="video.mp4" type="video/mp4">
</video>
Iframes
<iframe data-src="https://example.com"
loading="lazy"
width="560"
height="315">
</iframe>
Performance Metrics and Monitoring
Important Key Metrics
Monitoring Tools
- Google PageSpeed Insights: Checking Core Web Vitals
- Google Search Console: Monitoring Page Experience signals
- Chrome DevTools: Lighthouse audits for performance
- WebPageTest: Detailed performance analysis
Avoiding Common Mistakes
1. Lazy Loading All Images
❌ Wrong: Also lazy loading above-the-fold images
✅ Correct: Only lazy loading below-the-fold images
2. No Fallbacks
❌ Wrong: Only JavaScript-based solution without fallback
✅ Correct: Native HTML attributes with JavaScript fallback
3. Ignoring Layout Shifts
❌ Wrong: Images without defined dimensions
✅ Correct: Always set width and height attributes
4. Excessive Lazy Loading
❌ Wrong: Also lazy loading small icons and logos
✅ Correct: Only lazy loading larger media content
smartphone seo and Lazy Loading
Special Importance for Mobile
Mobile devices particularly benefit from Lazy Loading:
- Limited Bandwidth: Reduces data consumption
- Battery Consumption: Fewer CPU-intensive loading processes
- Touch Performance: Faster interactions
Mobile-specific Optimizations
<picture>
<source media="(max-width: 768px)"
data-srcset="mobile-bild.jpg"
loading="lazy">
<source media="(min-width: 769px)"
data-srcset="desktop-bild.jpg"
loading="lazy">
<img src="fallback-bild.jpg"
alt="Responsive Images Bild"
loading="lazy">
</picture>
Future of Lazy Loading
Native Browser Support
Modern browsers are expanding Lazy Loading functionality:
- Priority Hints:
fetchpriority="high"for critical images - loading hints: Extended preload functions
- Intersection Observer v2: Improved performance APIs
AI-based Optimizations
- predictive loading: AI recognizes which images are likely needed
- variable quality: Automatic adjustment of image quality based on connection speed
- Smart Resource Loading: Intelligent preloading based on user behavior
Checklist: Lazy Loading Implementation
Before Implementation
- Identify above-the-fold images
- Categorize critical vs. non-critical content
- Measure current performance metrics
- Define fallback strategy
During Implementation
- Use native HTML attributes
- Implement JavaScript fallback
- Create placeholder images
- Define layout dimensions
- Preload for critical images
After Implementation
- Conduct performance tests
- Monitor Core Web Vitals
- Cross-browser tests
- Check mobile performance
- Validate user experience
Conclusion
Lazy Loading is one of the most effective techniques for improving website performance and thus SEO rankings. By reducing initial load time and improving Core Web Vitals, Lazy Loading can bring significant SEO benefits.
However, implementation should be carefully planned to avoid negative impacts on user experience. With the right best practices and modern browser APIs, Lazy Loading can significantly improve both performance and SEO results.