Image Optimization
Automatic WebP/AVIF conversion, responsive srcsets, lazy loading, and zero layout shift — built into Astro.
Astro’s built-in image tools
Astro ships two image components with zero configuration:
<Image />— Optimises a single image, adds correct dimensions, converts format<Picture />— Generates a<picture>element with multiple format fallbacks
Both automatically add width, height, and loading="lazy" attributes to eliminate
Cumulative Layout Shift (CLS) and improve your Lighthouse score.
Astro's <Image /> component
automatically converts images to WebP/AVIF, adds
correct width/height
attributes to prevent layout shift, and generates a responsive
srcset.
| Method | Format | CLS Risk | Lazy Load | Srcset |
|---|---|---|---|---|
| <img> | Original | High | Manual | Manual |
| <Image /> | WebP / AVIF | None | Auto | Auto |
| <Picture /> | AVIF + WebP | None | Auto | Multiple |
✗ Plain HTML img
src="/hero.png"
alt="Hero"
/>
✓ Astro Image component
src={heroImg}
alt="Hero"
width={800}
format="webp"
/>
Using the Image component
Import a local image
Import the image as a module so Astro can read its dimensions at build time.
---
import { Image } from 'astro:assets'
import heroImg from '../assets/hero.png'
---
<Image
src={heroImg}
alt="Site hero"
width={1200}
format="webp"
quality={80}
/> Use the Picture component for art direction
<Picture> lets you serve different image formats or crops at different screen sizes.
---
import { Picture } from 'astro:assets'
import heroImg from '../assets/hero.png'
---
<Picture
src={heroImg}
alt="Site hero"
formats={['avif', 'webp']}
widths={[400, 800, 1200]}
sizes="(max-width: 600px) 400px, (max-width: 900px) 800px, 1200px"
/> Use remote images
For images hosted elsewhere, pass the URL as a string and provide explicit dimensions.
---
import { Image } from 'astro:assets'
---
<Image
src="https://cdn.example.com/photo.jpg"
alt="Remote photo"
width={800}
height={600}
format="webp"
/>You must whitelist the domain in
astro.config.mjs:image: { domains: ['cdn.example.com'] }
Performance impact
| Metric | Plain <img> | Astro <Image /> |
|---|---|---|
| Format | Original (PNG/JPEG) | WebP / AVIF |
| File size reduction | — | 30–80% |
| CLS | Likely | None |
| Lazy load | Manual | Automatic |
| Srcset | Manual | Automatic |
Always set priority on your above-the-fold hero image to prevent it from
being lazy-loaded — this improves Largest Contentful Paint (LCP).
<Image src={hero} alt="Hero" loading="eager" fetchpriority="high" /> During development, images are transformed on-demand. In production builds,
all local images are processed at build time and output to dist/_astro/.