Skip to main content
Astro provides built-in image optimization and asset handling through the astro:assets module.

Importing

Image Component

The <Image /> component optimizes images and generates responsive image markup.

Props

ImageMetadata | string
required
Image source. Can be an imported image or a URL string.
string
required
Alternative text for the image. Required for accessibility.
number | string
Desired width of the image. Sets the width attribute and resizes the image.
number | string
Desired height of the image. Sets the height attribute and resizes the image if width is not provided.
'avif' | 'webp' | 'png' | 'jpg' | 'jpeg' | 'svg' | string
Output format for the image.Default: 'webp'
'low' | 'mid' | 'high' | 'max' | number
Quality preset or numeric value (0-100) for the output image.
(number | `${number}x`)[]
Pixel densities to generate for the image srcset.
number[]
Widths to generate for the image srcset.
'fill' | 'contain' | 'cover' | 'none' | 'scale-down' | string
How the image should be resized to fit the dimensions.Default: 'cover'
string
Position of the image when using fit. Similar to CSS object-position.
string
Background color to use when converting transparent images to non-transparent formats.
boolean
Automatically infer width and height from the source image.Default: true for local images

HTML Attributes

All standard HTML <img> attributes are also supported:

Example

Picture Component

The <Picture /> component generates a <picture> element with multiple sources and formats.

Props

ImageOutputFormat[]
Array of image formats to generate. The component will create a <source> for each format.Default: ['webp']
ImageOutputFormat
Format to use for the fallback <img> element.Default: 'png'
HTMLAttributes
Attributes to apply to the <picture> element.
All other props from the Image component are also supported.

Example

getImage

Programmatically get optimized image data without rendering an <img> tag.
ImageTransform
required
Image transformation options.
ImageMetadata | string
required
Image source.
number
Desired width.
number
Desired height.
ImageOutputFormat
Output format.
ImageQuality
Image quality.
All other Image component props are also supported.
Promise<GetImageResult>
Object containing optimized image data.
string
URL of the optimized image.
object
Srcset data for responsive images.
SrcSetValue[]
Array of srcset values.
string
Formatted srcset attribute string.
Record<string, any>
HTML attributes for the image (width, height, etc.).
ImageTransform
Processed transformation options.

Example

ImageMetadata Type

Type for imported images.

Example

Remote Images

To use remote images, configure image.domains or image.remotePatterns in your Astro config:
Then use remote URLs with the Image component: