Ask any question about Web Development here... and get an instant response.
How can I optimize page load speed using lazy loading for images?
Asked on Dec 19, 2025
Answer
Lazy loading is a technique to optimize page load speed by deferring the loading of images until they are needed, which reduces initial load time and bandwidth usage. This approach is particularly useful in modern web development with frameworks like React, Vue, and Angular, as well as in plain HTML/JavaScript setups.
<!-- BEGIN COPY / PASTE -->
<img src="placeholder.jpg" data-src="actual-image.jpg" alt="Description" loading="lazy" class="lazyload">
<script>
document.addEventListener("DOMContentLoaded", function() {
const lazyImages = document.querySelectorAll("img.lazyload");
const imageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.classList.remove("lazyload");
observer.unobserve(img);
}
});
});
lazyImages.forEach(image => {
imageObserver.observe(image);
});
});
</script>
<!-- END COPY / PASTE -->Additional Comment:
- Use the "loading='lazy'" attribute for native browser support where available.
- Intersection Observer API is efficient for detecting when images enter the viewport.
- Ensure placeholder images are small to minimize initial load impact.
- Consider using a library like "lazysizes" for more advanced lazy loading features.
- Test across different devices and browsers to ensure consistent behavior.
Recommended Links:
