diff --git a/.changeset/proud-terms-swim.md b/.changeset/proud-terms-swim.md index e33b3d1aff..f5851dab44 100644 --- a/.changeset/proud-terms-swim.md +++ b/.changeset/proud-terms-swim.md @@ -1,5 +1,113 @@ --- -'astro': patch +'astro': minor --- -Adds experimental reponsive image support +Adds experimental support for automatic responsive images + +This feature is experimental and may change in future versions. To enable it, set `experimental.responsiveImages` to `true` in your `astro.config.mjs` file. + + ```js title=astro.config.mjs + { + experimental: { + responsiveImages: true, + }, + } + ``` + + When this flag is enabled, you can pass a `layout` props to any `` or `` 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. + + ```astro + // MyComponent.astro + --- + import { Image, Picture } from 'astro:assets'; + import myImage from '../assets/my_image.png'; + --- + A description of my image. + + ``` + This `` component will generate the following HTML output: + ```html title=Output + + A description of my image + ``` + + The following styles are applied to ensure the images resize correctly: + + ```css + [data-astro-image] { + width: 100%; + height: auto; + object-fit: var(--fit); + object-position: var(--pos); + aspect-ratio: var(--w) / var(--h) + } + + [data-astro-image=responsive] { + max-width: calc(var(--w) * 1px); + max-height: calc(var(--h) * 1px) + } + + [data-astro-image=fixed] { + width: calc(var(--w) * 1px); + height: calc(var(--h) * 1px) + } + ``` + You can enable responsive images for all `` and `` components by setting `image.experimentalLayout` with a default value. This can be overridden by the `layout` prop on each component. + + **Example:** + ```js title=astro.config.mjs + { + image: { + // Used for all `` and `` components unless overridden + experimentalLayout: 'responsive', + }, + experimental: { + responsiveImages: true, + }, + } + ``` + + ```astro title=MyComponent.astro + --- + import { Image } from 'astro:assets'; + import myImage from '../assets/my_image.png'; + --- + + This will use responsive layout + + This will use full-width layout + + This will disable responsive images + ``` + + #### Responsive image properties + + These are additional properties available to the `` and `` components when responsive images are enabled: + + - `layout`: The layout type for the image. Can be `responsive`, `fixed`, `full-width` or `none`. Defaults to value of `image.experimentalLayout`. + - `fit`: Defines how the image should be cropped if the aspect ratio is changed. Values match those of CSS `object-fit`. Defaults to `cover`, or the value of `image.experimentalObjectFit` if set. + - `position`: Defines the position of the image crop if the aspect ratio is changed. Values match those of CSS `object-position`. Defaults to `center`, or the value of `image.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 to `false`. + +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.