Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Practical Astro
Lesson Plan
  1. Content Systems
    1. Introduction
    2. An import alias
    3. Using Markdown
    4. Markdown extended
    5. Markdown component
    6. Markdown component in frameworks
    7. Content collections made easy
    8. Basic blogs
    9. Blogs made easy
    10. Prev/Next navigation
    11. Pagination made easy
    12. Better tag pages
    13. Paginated tag pages
    14. Markdown layouts
    15. Partials
    16. JS over JSON Data
    17. Dynamic 404 pages
    18. Putting everything together
    19. Other useful systems
  2. Image Systems
    1. Introduction
    2. Images are complicated because of this
    3. Astro's Image component
    4. Astro's Picture component
    5. Improvements over Astro's Image and Picture Components
    6. Images in Markdown and MDX
    7. Images in framework components
    8. Background images
    9. Art Direction with Astro
    10. Videos without layout shifts
    11. GIF Replacements and background video
    12. SVGs and icons
    13. SVG backgrounds
    14. Adding noise to images
    15. Asset caching on GitHub Actions
    16. Putting everything together
    17. Other useful systems

Images are complicated because of this

1m:27s

“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.

  1. Images must be properly sized
  2. Images must be efficiently encoded
  3. Images must use better formats like WebP and AVIF
  4. Images above the fold should be prioritized
  5. Images should be lazy-loaded if they’re below the fold
  6. 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.