“Why can’t we just use an image tag and be done with images?” If you’re asking that question, you’re not alone.
<!-- Why can't we just write this? --><img src="..." alt="..." />The real answer is: You can’t, because Google penalizes you when you do this.
Google Lighthouse Audit
The Lighthouse audit has lots of rules for images. Here’s the gist.
- Images must be properly sized
- Images must be efficiently encoded
- Images must use better formats like WebP and AVIF
- Images above the fold should be prioritized
- Images should be lazy-loaded if they’re below the fold
- The layouts must not shift when images are loaded
The output for an image that passes Google’s Lighthouse audit with flying colors looks like this:
<img src="/img/hero-800.webp" srcset=" /img/hero-400.webp 400w, /img/hero-800.webp 800w, /img/hero-1200.webp 1200w, /img/hero-1600.webp 1600w " sizes="(max-width: 600px) 100vw, 800px" width="1600" height="900" alt="Describe the image" loading="lazy" decoding="async"/>There’s also a <picture> version where you can set multiple image formats while falling back to the traditional jpg or png like this:
<picture> <source type="image/avif" srcset=" /img/hero-400.avif 400w, /img/hero-800.avif 800w, /img/hero-1200.avif 1200w, /img/hero-1600.avif 1600w " sizes="(max-width: 600px) 100vw, 800px" /> <source type="image/webp" srcset=" /img/hero-400.webp 400w, /img/hero-800.webp 800w, /img/hero-1200.webp 1200w, /img/hero-1600.webp 1600w " sizes="(max-width: 600px) 100vw, 800px" /> <img src="/img/hero-800.jpg" width="1600" height="900" alt="Describe the image" loading="lazy" decoding="async" /></picture>This sort of thing should never be hand-written.
So, over the next few lessons, let’s walk through how you can get these monster <img> and <picture> outputs by just writing the src and alt.
<!-- What you'll eventually write --><Image src="" alt="" />We’ll do this step-by-step, so you know you’ve got all the technical ground covered.
Let’s go.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson