Lesson 7 of 7
Image Optimization & Performance
Images account for 50-70% of a typical webpage's total size. Optimizing images directly improves page load time, Core Web Vitals scores, and search engine rankings. This topic covers compression techniques, modern format selection, lazy loading strategies, and the critical metrics Google uses to measure image performance.
Image Performance Checklist
- Use WebP/AVIF as primary format - 30-50% smaller than JPEG at same quality. Use <picture> for fallback.
- Compress all images before uploading - Tools: Squoosh.app (free), TinyPNG, ImageMagick. Target 80% quality for photos.
- Serve correct sizes with srcset - Never scale a 2000px image to 400px with CSS. Generate multiple sizes.
- Use loading='lazy' for below-fold images - Defers loading until user scrolls near image. Saves bandwidth.
- Always set width and height - Prevents Cumulative Layout Shift (CLS). Browser reserves space before image loads.
- Use loading='eager' for hero/LCP images - Above-fold images should load immediately, not lazily.
- Add fetchpriority='high' to LCP image - Tells browser to prioritize this image download over others.
- Use a CDN (Content Delivery Network) - Cloudinary, imgix, or Vercel Image Optimization serve images from edge servers worldwide.
- Consider CSS background-image for decorative elements - Background images don't block page rendering.
- Test with Lighthouse - Run Chrome DevTools > Lighthouse to find image optimization opportunities.
Optimized Image Example
Optimized Image Example
<!DOCTYPE html>
<html>
<body>
<h1>Optimized Image Loading</h1>
<!-- HERO IMAGE: Load immediately with high priority -->
<picture>
<source srcset="hero.avif" type="image/avif">
<source srcset="hero.webp" type="image/webp">
<img
src="hero.jpg"
alt="Hero banner"
width="1200" height="600"
loading="eager"
fetchpriority="high"
decoding="async"
>
</picture>
<!-- BELOW-FOLD IMAGES: Lazy load -->
<img
src="product-800.jpg"
srcset="product-400.jpg 400w, product-800.jpg 800w"
sizes="(max-width:600px) 100vw, 50vw"
alt="Product photo"
width="800" height="600"
loading="lazy"
decoding="async"
>
</body>
</html>Diagram
Loading diagram…
Native lazy loading defers off-screen images until scrolled into view
Quick Quiz: Image Optimization
Module 4 - Knowledge Check
Module 4 - Knowledge Check
Question 1 of 5Which attribute on <img> is required for accessibility and SEO?
Practice Task: Media-Rich Landing Page
- Create a landing page with a hero image using <picture> that serves AVIF, WebP, and JPEG formats with appropriate fallbacks
- Add a responsive image gallery using srcset and sizes that loads different image sizes based on viewport width
- Embed a YouTube video using <iframe> with proper title, loading='lazy', and sandbox attributes
- Add loading='lazy' to all below-the-fold images and verify they load only when scrolled into view using DevTools Network tab
- Use <figure> and <figcaption> to add semantic captions to at least 2 images
Key Takeaways
- Images account for 50-70% of a typical webpage's total size.
- Use WebP/AVIF as primary format - 30-50% smaller than JPEG at same quality. Use <picture> for fallback.
- Compress all images before uploading - Tools: Squoosh.app (free), TinyPNG, ImageMagick. Target 80% quality for photos.
- Serve correct sizes with srcset - Never scale a 2000px image to 400px with CSS. Generate multiple sizes.
Finished this lesson?
0 of 7 in Module 4