web-performance-optimization
/

Lazy Loading in Web

Last Sync: Today

On this page

11
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

web-performance-optimization

Lazy Loading in Web

What is Lazy Loading?

Lazy loading is a technique where non-critical resources are loaded only when they are needed, such as when they appear in the user's viewport.

Why Use Lazy Loading?

Lazy loading reduces initial page load time, saves bandwidth, and improves performance, especially for pages with many images or heavy content.

Basic Example

HTMLRead-only
1
<img src="image.jpg" loading="lazy" alt="Lazy Image">

Lazy Loading Images

Modern browsers support native lazy loading using the loading="lazy" attribute.

HTMLRead-only
1
<img src="photo.webp" loading="lazy" alt="Example">

Lazy Loading with JavaScript

You can use Intersection Observer API to implement custom lazy loading.

JavaScriptRead-only
1
const images = document.querySelectorAll('img');

const observer = new IntersectionObserver((entries, observer) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      const img = entry.target;
      img.src = img.dataset.src;
      observer.unobserve(img);
    }
  });
});

images.forEach(img => observer.observe(img));

Lazy Loading Videos

HTMLRead-only
1
<video controls preload="none">
  <source src="video.mp4" type="video/mp4">
</video>

Benefits of Lazy Loading

  • Faster initial page load
  • Reduced bandwidth usage
  • Better user experience
  • Improved SEO performance

When Not to Use Lazy Loading

  • For above-the-fold content
  • For critical UI elements
  • For important hero images

Best Practices

  • Use native lazy loading where possible
  • Provide placeholder or skeleton UI
  • Combine with image optimization (WebP)
  • Avoid lazy loading critical resources

Common Mistakes

  • Lazy loading everything including critical content
  • Not providing fallback for unsupported browsers
  • Using heavy JavaScript unnecessarily
  • Forgetting alt attributes for images

Conclusion

Lazy loading is a powerful technique to improve web performance. When used correctly, it enhances speed, reduces resource usage, and improves user experience.

Try it yourself

<img src="image.jpg" loading="lazy" alt="Example">

Test Your Knowledge

Q1
of 3

What does lazy loading do?

A
Loads everything immediately
B
Loads resources when needed
C
Blocks rendering
D
Reduces images
Q2
of 3

Which attribute enables lazy loading?

A
async
B
defer
C
loading
D
lazy
Q3
of 3

Which API is used for custom lazy loading?

A
Fetch API
B
Intersection Observer
C
DOM API
D
Storage API

Frequently Asked Questions

What is lazy loading?

It loads resources only when they are needed.

Does lazy loading improve SEO?

Yes, it improves page speed which helps SEO.

Can I lazy load videos?

Yes, using preload="none" or JavaScript.

Previous

web loading strategies

Next

web code splitting

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.