ESC

Type to search the knowledge base.

SnippetPerformance

Image Lazy Loading

Native lazy loading plus progressive enhancement with IntersectionObserver and LQIP pattern notes.

Explanation

Start with loading="lazy" and correct width/height (or aspect-ratio) to avoid CLS. For older browsers or blur-up, enhance with IntersectionObserver.

html
<img
  src="/images/hero.jpg"
  alt="Team collaborating at a whiteboard"
  width="1200"
  height="630"
  loading="lazy"
  decoding="async"
/>

Usage example

html
<img data-src="/photo.jpg" alt="..." width="400" height="300" />
<script>lazyImages();</script>

// Prefer native when possible:
// <img src="..." loading="lazy" decoding="async" width height alt>

Interview tips

  • Always set dimensions or aspect-ratio to prevent CLS.
  • loading="lazy" is ignored for images in the initial viewport in most browsers — that’s good.

Related

← All snippets