<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Progressive Image Loading with CSS & Intersection Observer</title>
<style>
/* Reset and demo styles */
body {
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
margin: 0;
padding: 2rem;
background: #f5f5f5;
line-height: 1.6;
}
h1, h2 {
margin-top: 0;
}
.demo {
max-width: 1200px;
margin: 0 auto;
}
/* Core progressive image styles */
.image-container {
--aspect-ratio: 16 / 9; /* Customizable per image via inline style or CSS vars */
position: relative;
width: 100%;
height: 0;
padding-bottom: calc(100% / var(--aspect-ratio));
margin-bottom: 2rem;
overflow: hidden;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
background: #f0f0f0; /* Fallback color */
transition: box-shadow 0.3s ease;
}
.image-container:hover {
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}
.image-placeholder {
position: absolute;
inset: 0;
background-size: cover;
background-position: center;
filter: blur(15px) brightness(1.1); /* Progressive blur effect */
transition: opacity 0.4s ease, filter 0.4s ease;
}
.image-container.loaded .image-placeholder {
opacity: 0;
filter: blur(0) brightness(1);
}
.image-full {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
object-fit: cover;
opacity: 0;
transition: opacity 0.4s ease;
}
.image-container.loaded .image-full {
opacity: 1;
}
/* Responsive grid for demo */
.image-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 2rem;
margin-top: 2rem;
}
/* Instructions */
.instructions {
background: white;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
margin-bottom: 2rem;
}
code {
background: #f0f0f0;
padding: 0.2rem 0.4rem;
border-radius: 4px;
font-family: 'Courier New', monospace;
}
pre {
background: #1e1e1e;
color: #f8f8f2;
padding: 1rem;
border-radius: 8px;
overflow-x: auto;
}
</style>
</head>
<body>
<div class="demo">
<h1>Progressive Image Loading Strategy</h1>
<p>Images start with a low-quality, blurred placeholder (LQIP) loaded via CSS <code>background-image</code>. Full-resolution images load lazily via <strong>Intersection Observer</strong> when entering the viewport. Smooth cross-fade transition on load.</p>
<div class="instructions">
<h2>How it works:</h2>
<ol>
<li><strong>LQIP Placeholder</strong>: Tiny (~1-5KB) low-res image as blurred CSS background for instant display.</li>
<li><strong>Intersection Observer</strong>: Detects viewport entry, sets <code>img.src</code> from <code>data-src</code>.</li>
<li><strong>Progressive Fade</strong>: CSS transitions blur away the placeholder while fading in the full image.</li>
<li><strong>Vanilla only</strong>: No libraries, works in all modern browsers (IE11+ with polyfill).</li>
<li><strong>Customizable</strong>: Aspect ratio via CSS vars, easy to generate LQIPs (e.g., via ImageMagick or Sharp).</li>
</ol>
</div>
<div class="image-grid">
<!-- Example 1: Landscape -->
<div class="image-container" style="--aspect-ratio: 16/9;"
data-full="https://picsum.photos/800/450?random=1"
data-lqip="https://picsum.photos/20/11?random=1&blur=2">
<div class="image-placeholder"></div>
<img class="image-full" data-src="https://picsum.photos/800/450?random=1" alt="Landscape photo">
</div>
<!-- Example 2: Portrait -->
<div class="image-container" style="--aspect-ratio: 9/16;"
data-full="https://picsum.photos/450/800?random=2"
data-lqip="https://picsum.photos/11/20?random=2&blur=2">
<div class="image-placeholder"></div>
<img class="image-full" data-src="https://picsum.photos/450/800?random=2" alt="Portrait photo">
</div>
<!-- Example 3: Square -->
<div class="image-container" style="--aspect-ratio: 1/1;"
data-full="https://picsum.photos/600/600?random=3"
data-lqip="https://picsum.photos/30/30?random=3&blur=2">
<div class="image-placeholder"></div>
<img class="image-full" data-src="https://picsum.photos/600/600?random=3" alt="Square photo">
</div>
<!-- Spacer for scrolling demo -->
<div style="height: 2000px; background: linear-gradient(180deg, #f0f8ff, #e6f3ff); border-radius: 8px; display: flex; align-items: center; justify-content: center; grid-column: 1/-1;">
<h2 style="color: #007acc;">Scroll up/down to trigger loading 👆👇</h2>
</div>
</div>
<h2>Implementation Code</h2>
<h3>HTML Structure (per image)</h3>
<pre><code><div class="image-container" style="--aspect-ratio: 16/9;"
data-lqip="tiny-blurred.jpg" data-full="full.jpg">
<div class="image-placeholder"></div>
<img class="image-full" data-src="full.jpg" alt="Description">
</div></code></pre>
<h3>CSS (as above)</h3>
<h3>JavaScript</h3>
<pre><code><script>
const imageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const container = entry.target;
const img = container.querySelector('.image-full');
const lqip = container.dataset.lqip;
// Set LQIP background immediately (inline for perf)
container.querySelector('.image-placeholder').style.backgroundImage = `url(${lqip})`;
// Load full image
img.src = img.dataset.src;
img.onload = () => container.classList.add('loaded');
img.onerror = () => console.warn('Image load failed:', img.src); // Optional error handling
observer.unobserve(container); // Stop observing
}
});
}, {
rootMargin: '50px', // Preload 50px before viewport
threshold: 0.01
});
// Observe all containers
document.querySelectorAll('.image-container').forEach(container => {
imageObserver.observe(container);
});
</script></code></pre>
</div>
<script>
// Production-ready JS (paste this at end of body)
const imageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const container = entry.target;
const img = container.querySelector('.image-full');
const lqip = container.dataset.lqip;
// Set LQIP background (inline styles for critical rendering path)
if (lqip) {
container.querySelector('.image-placeholder').style.backgroundImage = `url(${lqip})`;
}
// Trigger full image load
img.src = img.dataset.src;
img.onload = () => container.classList.add('loaded');
img.onerror = () => {
console.warn('Image load failed:', img.src);
container.classList.add('loaded'); // Hide placeholder on error
};
observer.unobserve(container);
}
});
}, {
rootMargin: '50px 0px', // Preload slightly early
threshold: 0.01
});
// Initialize
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('.image-container').forEach(container => {
imageObserver.observe(container);
});
});
</script>
</body>
</html>
Key Benefits
- Performance: LQIPs load instantly (<50ms), full images only when needed.
- Smooth UX: Blur-up effect feels "progressive" without jank.
- SEO/Accessibility:
<img> with alt, proper aspect ratios.
- Responsive: Fluid grid, auto aspect-ratio.
- Edge Cases: Error handling, unobserve after load, rootMargin for prefetch.
- LQIP Generation Tip: Use tools like
sharp (Node): sharp(input.jpg).resize(20,13).blur(1).jpeg({quality:30}).toBuffer() for ~1KB files.
Copy-paste the full page to test! Scroll to see magic. 🚀