HTML Lazy Loading Media
Optimize performance with smart image and video loading
⚡ What is Lazy Loading?
Lazy loading means images and videos only load when they're about to become visible on the screen. It's like having a smart assistant that only brings you things when you need them - saving time and bandwidth!
<!-- This image loads immediately -->
<img src="hero-image.jpg" alt="Hero image">
<!-- This image loads when user scrolls near it -->
<img src="content-image.jpg" alt="Content image" loading="lazy">
Why Lazy Loading Matters
Faster Loading
Only essential content loads first, making pages interactive sooner
Less Bandwidth
Users don't download images they never see, saving data
Better UX
Pages become usable faster, improving user experience
SEO Benefits
Faster pages rank better in search engine results
🔹 Native HTML Lazy Loading
The simplest way to implement lazy loading with just HTML:
<!-- This image loads immediately -->
<img src="hero-image.jpg" alt="Hero image">
<!-- This image loads when user scrolls near it -->
<img src="content-image.jpg" alt="Content image" loading="lazy">
Result:
The second image will only start downloading when it's about to enter the viewport, saving bandwidth and improving initial page load speed.
🔹 Loading Attribute Values
Different loading behaviors you can specify:
<!-- Load immediately (default) -->
<img src="image1.jpg" loading="eager" alt="Loads right away">
<!-- Load when needed (recommended for most images) -->
<img src="image2.jpg" loading="lazy" alt="Loads when visible">
<!-- Let browser decide (default behavior) -->
<img src="image3.jpg" loading="auto" alt="Browser decides">
Loading values explained:
-
eager- Load immediately (default behavior) -
lazy- Load when about to become visible -
auto- Let the browser decide when to load
🔹 Lazy Loading Videos
Apply lazy loading to videos for even better performance:
<video controls loading="lazy" poster="video-thumbnail.jpg">
<source src="large-video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
Pro tip:
Always use a poster image with lazy-loaded videos to show a preview while the video loads!
🔹 Combining with Responsive Images
Lazy loading works perfectly with responsive images:
<img src="placeholder.jpg"
srcset="image-400.jpg 400w,
image-800.jpg 800w,
image-1200.jpg 1200w"
sizes="(max-width: 600px) 400px,
(max-width: 900px) 800px,
1200px"
loading="lazy"
alt="Responsive lazy-loaded image">
🔹 Placeholder Strategies
Different ways to show placeholders while images load:
🔸 Solid Color Placeholder
<img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300'%3E%3Crect width='100%25' height='100%25' fill='%23f0f0f0'/%3E%3C/svg%3E"
data-src="real-image.jpg"
loading="lazy"
alt="Image with placeholder">
🔸 SVG Placeholder
<img src="/placeholder.svg?height=300&width=400"
data-src="actual-image.jpg"
loading="lazy"
alt="Image with SVG placeholder">
🔹 Lazy Loading Iframes
Apply lazy loading to embedded content like YouTube videos and maps:
<!-- YouTube video -->
<iframe src="https://www.youtube.com/embed/VIDEO_ID"
loading="lazy"
width="560"
height="315"
frameborder="0"
allowfullscreen>
</iframe>
<!-- Google Maps -->
<iframe src="https://www.google.com/maps/embed?pb=..."
loading="lazy"
width="600"
height="450"
style="border:0;"
allowfullscreen>
</iframe>
🔹 Complete Gallery Example
A practical example of lazy-loaded photo gallery:
<div class="gallery">
<!-- First image loads immediately (above fold) -->
<img src="photo1.jpg" alt="Photo 1" class="gallery-item">
<!-- Rest load lazily -->
<img src="placeholder.jpg"
data-src="photo2.jpg"
loading="lazy"
alt="Photo 2"
class="gallery-item lazy">
<img src="placeholder.jpg"
data-src="photo3.jpg"
loading="lazy"
alt="Photo 3"
class="gallery-item lazy">
</div>
<style>
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}
.gallery-item {
width: 100%;
height: 200px;
object-fit: cover;
border-radius: 8px;
transition: transform 0.3s;
}
.gallery-item:hover {
transform: scale(1.05);
}
.lazy {
background: linear-gradient(45deg, #f0f0f0, #e0e0e0);
}
</style>
🔹 Performance Best Practices
✅ Do This:
- Load critical above-the-fold images immediately
-
Use
loading="eager"for important hero images - Provide appropriate placeholder dimensions
- Test on slow connections to verify benefits
- Combine with responsive images for maximum efficiency
❌ Avoid This:
- Lazy loading hero images (above the fold)
- Not providing placeholder dimensions (causes layout shift)
- Lazy loading all images without considering UX
- Forgetting fallbacks for older browsers
🔹 Testing Lazy Loading
How to verify it's working:
- Browser DevTools: Open Network tab
- Disable cache: Check "Disable cache" option
- Reload page: See which images load immediately
- Scroll down: Watch new images load as you scroll
- Throttle connection: Test on slow 3G to see the effect