Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Practical Astro
Lesson Plan
  1. Content Systems
    1. Introduction
    2. An import alias
    3. Using Markdown
    4. Markdown extended
    5. Markdown component
    6. Markdown component in frameworks
    7. Content collections made easy
    8. Basic blogs
    9. Blogs made easy
    10. Prev/Next navigation
    11. Pagination made easy
    12. Better tag pages
    13. Paginated tag pages
    14. Markdown layouts
    15. Partials
    16. JS over JSON Data
    17. Dynamic 404 pages
    18. Putting everything together
    19. Other useful systems
  2. Image Systems
    1. Introduction
    2. Images are complicated because of this
    3. Astro's Image component
    4. Astro's Picture component
    5. Improvements over Astro's Image and Picture Components
    6. Images in Markdown and MDX
    7. Images in framework components
    8. Background images
    9. Art Direction with Astro
    10. Videos without layout shifts
    11. GIF Replacements and background video
    12. SVGs and icons
    13. SVG backgrounds
    14. Adding noise to images
    15. Asset caching on GitHub Actions
    16. Putting everything together
    17. Other useful systems

Astro's Picture component

0m:57s

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.

index.astro
---
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

index.astro
---
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]}
/>