Image Component
TheImage component from astro:assets optimizes images at build time with automatic format conversion, resizing, and lazy loading.
src/pages/index.astro
Images are optimized during the build process, generating multiple sizes and formats for better performance.
Local Images
Import local images from your project to get type safety and automatic optimization:Remote Images
For remote images, specify dimensions explicitly:Image Properties
TheImage component accepts various properties for optimization:
ImageMetadata | string
required
Image source - local import or remote URL
string
required
Alternative text for accessibility
number
Target width in pixels. Required for remote images.
number
Target height in pixels. Required for remote images.
'avif' | 'webp' | 'png' | 'jpg' | 'svg'
Output format. Defaults to optimized format.
number | 'low' | 'mid' | 'high' | 'max'
Compression quality. Default: ‘mid’
number[]
Pixel density descriptors for responsive images
number[]
Generate multiple widths for srcset
Example with All Properties
Picture Component
Use thePicture component for art direction and multiple formats:
- Multiple format versions (AVIF, WebP, JPEG)
- Multiple sizes for responsive images
- Automatic
<picture>element with<source>tags
Image Service
Astro uses Sharp by default for image optimization. You can configure or replace the image service:astro.config.mjs
Available Services
- No-op Service
High-performance Node.js image processing:
getImage API
For programmatic image optimization, use thegetImage function:
Return Value
ThegetImage function returns an object with:
Images in Markdown
Reference images in Markdown and MDX files:src/content/blog/post.md
src/content/blog/post.mdx
Responsive Images
Generate responsive images with multiple sizes:Image Layouts
Control how images scale and fit:Constrained
Constrained
Image scales down on smaller screens but never exceeds its specified width.
Best for most use cases.
Fixed
Fixed
Image maintains exact dimensions at all screen sizes.
Best for avatars, icons, and UI elements.
Full Width
Full Width
Image always fills its container width.
Best for hero images and banners.
Background Images
Optimize background images usinggetImage:
Other Assets
Fonts
Import and reference fonts:Videos
Reference video files:Public Directory
For assets that shouldn’t be processed, use thepublic/ directory:
Files in
public/ are copied as-is to the build output without processing.Performance Best Practices
1
Use modern formats
Enable AVIF and WebP for better compression:
2
Lazy load off-screen images
3
Set appropriate quality
4
Generate responsive sizes
TypeScript Support
Images imports are fully typed:Related Resources
Markdown
Use images in Markdown content
Content Collections
Manage images in collections
Configuration
Configure image service
Performance
Performance optimization guide