Experimental responsive images
此内容尚不支持你的语言。
Type: boolean
Default: false
astro@5.0.0
Beta
Enables support for automatic responsive images in your project.
When this flag is enabled, you can pass a layout
prop to any <Image />
or <Picture />
component to create a responsive image.
When a layout is set, images have automatically generated srcset
and sizes
attributes based on the image’s dimensions and the layout type. Images with responsive
and full-width
layouts will have styles applied to ensure they resize according to their container.
This <Image />
component will generate the following HTML output:
The following styles are applied to ensure the images resize correctly:
Responsive image properties
Section titled Responsive image propertiesThese are additional properties available to the <Image />
and <Picture />
components when responsive images are enabled:
layout
: The layout type for the image. Can beresponsive
,fixed
,full-width
ornone
. Defaults to value ofimage.experimentalLayout
.fit
: Defines how the image should be cropped if the aspect ratio is changed. Values match those of CSSobject-fit
. Defaults tocover
, or the value ofimage.experimentalObjectFit
if set.position
: Defines the position of the image crop if the aspect ratio is changed. Values match those of CSSobject-position
. Defaults tocenter
, or the value ofimage.experimentalObjectPosition
if set.priority
: If set, eagerly loads the image. Otherwise images will be lazy-loaded. Use this for your largest above-the-fold image. Defaults tofalse
.
The widths
and sizes
attributes are automatically generated based on the image’s dimensions and the layout type, and in most cases should not be set manually. The generated sizes
attribute for responsive
and full-width
images
is based on the assumption that the image is displayed at close to the full width of the screen when the viewport is smaller than the image’s width. If it is significantly different (e.g. if it’s in a multi-column layout on small screens) you may need to adjust the sizes
attribute manually for best results.
The densities
attribute is not compatible with responsive images and will be ignored if set.
Responsive image configuration settings
Section titled Responsive image configuration settingsYou can enable responsive images for all <Image />
and <Picture />
components by setting image.experimentalLayout
with a default value. This settings can be overridden by the layout
prop on each component.
With responsive
set as the default layout, you can override any individual image’s layout
property:
For a complete overview, and to give feedback on this experimental API, see the Responsive Images RFC.
Reference