Astro has a Picture component as well.
The main advantage of Picture is that it lets you provide alternate image formats. So you can output modern image formats (like avif and webp) and provide a fallback (with jpg or png) at the same time.
You do this with the formats property.
---import { Picture } from 'astro:assets'import myImage from '../assets/penguin.png'---
<Picture src={myImage} alt="A very cool penguin!" formats={['avif', 'webp']} widths={[400, 800, 1200]}/>The output looks like this:
<picture> <source type="image/avif" srcset=" /_astro/penguin-3.ClS-zgCy_rwViz.avif 400w, /_astro/penguin-3.ClS-zgCy_Z2e9MSy.avif 800w, /_astro/penguin-3.ClS-zgCy_1rzk2E.avif 1200w " /> <source type="image/webp" srcset=" /_astro/penguin-3.ClS-zgCy_ZlKcao.webp 400w, /_astro/penguin-3.ClS-zgCy_22Jcrp.webp 800w, /_astro/penguin-3.ClS-zgCy_1OlJpl.webp 1200w " /> <img src="/_astro/penguin-3.ClS-zgCy_Zh8cPp.png" srcset=" /_astro/penguin-3.ClS-zgCy_JklOK.png 400w, /_astro/penguin-3.ClS-zgCy_Z1Vmnmn.png 800w, /_astro/penguin-3.ClS-zgCy_jM0Kn.png 1200w " alt="A very cool penguin!" loading="lazy" decoding="async" fetchpriority="auto" width="1588" height="1054" /></picture>Image vs Picture Component
Picture is a superset of Image. It also takes in all the properties of the Image component. So you can always use Picture over Image.
Code you can copy
---import { Picture } from 'astro:assets'import myImage from '../assets/penguin.png'---
<Picture src={myImage} alt="A very cool penguin!" formats={['avif', 'webp']} widths={[400, 800, 1200]}/>Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson