To make your <Picture /> component responsive and performant on mobile, you should:
- Enable responsive images in your Astro project by setting the
experimental.responsiveImages flag to true in your astro.config.mjs file.
- Set the
layout prop to "full-width" on your <Picture /> component. This will ensure the image scales to fit the width of its container and generates the appropriate srcset and sizes attributes for responsive behavior.
- Optionally, adjust the
priority prop if you want to control loading behavior for above-the-fold images.
Here’s how you can update your <Picture /> usage:
<Picture
pictureAttributes={{
class: "cs-background",
}}
src={backgroundImage}
quality="mid"
formats={["avif", "webp", "jpg"]}
alt=""
loading="eager"
decoding="async"
aria-hidden="true"
layout="full-width" <!-- Enables responsive, full-width behavior -->
/>
Additional steps:
- Make sure you have enabled the experimental flag in your config:
export default {
experimental: {
responsiveImages: true,
},
};
- The
layout="full-width" prop will generate responsive srcset and sizes attributes, and apply styles so the image fills the container width and adapts to different screen sizes, which is ideal for hero backgrounds on mobile and desktop [Responsive image properties] [Image layout].