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

Astro's Image component

3m:37s

Astro’s built-in image system can help us handle everything that Lighthouse Audit requires. It is not as seamless as the one I built, but let’s go through how you can use it first.

Using Astro’s Image Component

You have to import your image and pass the imported variable into the Image component. You cannot just pass the url directly.

index.astro
---
import { Image } from 'astro:assets'
import myImage from '../assets/penguin.png'
---
<Image src={myImage} alt="A very cool penguin!" />

The output looks like this in development:

<img
src="/_image/?href=some-long-string.......f=webp"
data-image-component="true"
alt="A very cool penguin!"
loading="lazy"
decoding="async"
fetchpriority="auto"
width="1588"
height="1054"
/>

And it looks like this in production:

<img
src="/_astro/penguin-3.ClS-zgCy_tIb6a.webp"
alt="A very cool penguin!"
loading="lazy"
decoding="async"
fetchpriority="auto"
width="1588"
height="1054"
/>

Two things to note here:

  1. The src string changes between development and production
  2. The data-image-component disappears in production

You don’t have to worry about these two differences because Astro handles them. I just wanted to let you know about this in case you looked at the HTML and got a shock.

Fulfilling Lighthouse audits

This resolves 3 out of 6 of Google Lighthouse’s audit issues.

  1. Images must be efficiently encoded
  2. Images must use better formats like WebP and AVIF
  3. Images should be lazy-loaded if they’re below the fold

1 and 2 are handled by the image processing engine Astro uses behind the scenes. (Sharp is the default processor). 3 is handled with the loading="lazy" attribute.

The rest are not resolved just yet, but they can be handled with a few more tiny changes.

  1. Images must be properly sized
  2. Images above the fold should be prioritized
  3. The layouts must not shift when images are loaded

Ensuring layouts don’t shift

Astro’s Image component produces height and width attributes that ensure that the layouts do not shift. That is the first step. The second step is just to add some CSS.

/* Important properties for responsive images! */
img {
max-width: 100%;
height: auto;
}

Images above the fold should be prioritized

Astro’s Image component takes in a priority prop that helps you prioritize images above the fold. It does the following:

  • Set loading="eager" — so the image is fetched immediately
  • Set fetchpriority="high" — tells the browser to download the image along with other critical assets
  • Set decoding="sync" — decodes the image immediately so the image can be painted
<Image src="..." alt="..." priority />

This is all you need to prioritize images above the fold.

Sizing images properly

Let’s say a user loads your website on their mobile device, and their screen is only 480px wide. They don’t need a 1500px image. We can serve them a smaller image instead. This saves bandwidth and improves load times.

However, if their mobile device has a higher pixel density — as most retina screens do nowadays — the image needed depends on the pixel density. A device with 2x pixel density will need an image that’s twice the device’s width.

Ultimately, we want to serve the smallest image that still looks sharp. In Astro, we can do this with 2 properties:

  • widths — tells Astro’s image processor to generate the image at several sizes. It produces an srcset attribute.
  • sizes — tells the browser how wide the image will display at each breakpoint, so it can pick the smallest image that still looks sharp. When omitted, sizes defaults to 100vw which is good enough for most cases.
<!-- sizes when omitted defaults to 100vw -->
<Image
src={myImg}
alt="..."
widths={[400, 800, 1200]}
sizes="100vw"
/>

Code you can copy

The major difference for all images is whether they are above the fold or below the fold. For images above the fold, you need to prioritize them with the priority property.

---
import { Image } from 'astro:assets'
import myImage from '../assets/penguin.png'
---
<Image src={myImage} alt="..." widths={[400, 800, 1200]} priority />

For images below the fold, you don’t have to.

---
import { Image } from 'astro:assets'
import myImage from '../assets/penguin.png'
---
<Image src={myImage} alt="..." widths={[400, 800, 1200]} />

In both cases, make sure you have the following CSS to make images responsive while preventing layout shifts.

img {
max-width: 100%;
height: auto;
}