Skip to main content
Astro provides powerful built-in asset handling for images and other media. The assets system optimizes images automatically, generates multiple formats, and provides a great developer experience with type safety.

Image Component

The Image 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:
Remote images require explicit width and height attributes. Configure allowed remote domains in your config.

Image Properties

The Image 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 the Picture component for art direction and multiple formats:
This generates:
  • Multiple format versions (AVIF, WebP, JPEG)
  • Multiple sizes for responsive images
  • Automatic <picture> element with <source> tags
Use Picture for responsive images with art direction. Use Image for simple cases.

Image Service

Astro uses Sharp by default for image optimization. You can configure or replace the image service:
astro.config.mjs

Available Services

High-performance Node.js image processing:

getImage API

For programmatic image optimization, use the getImage function:

Return Value

The getImage function returns an object with:

Images in Markdown

Reference images in Markdown and MDX files:
src/content/blog/post.md
Or use the Image component in MDX:
src/content/blog/post.mdx

Responsive Images

Generate responsive images with multiple sizes:
This generates:

Image Layouts

Control how images scale and fit:
Image scales down on smaller screens but never exceeds its specified width. Best for most use cases.
Image maintains exact dimensions at all screen sizes. Best for avatars, icons, and UI elements.
Image always fills its container width. Best for hero images and banners.

Background Images

Optimize background images using getImage:

Other Assets

Fonts

Import and reference fonts:

Videos

Reference video files:

Public Directory

For assets that shouldn’t be processed, use the public/ directory:
Reference them with absolute paths:
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:

Markdown

Use images in Markdown content

Content Collections

Manage images in collections

Configuration

Configure image service

Performance

Performance optimization guide