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

Art Direction with Astro

3m:43s

Sometimes you want to show a different image at different viewports, like a portrait on mobile, but landscape on desktop. This is called art direction in the CSS community.

Below is an example of this in action, where:

  • the portrait image on mobile focuses on the person
  • the landscape image on desktop includes the scenery
A hiker on a mountain ridge at dawn

The HTML needed for art direction

The <picture> element is built to support art direction. To make this work, you need to write one source tag for each image. This source needs to include a media query.

Here’s an example that swaps the image at 700px.

index.html
<picture>
<!-- Portrait image at 700px and below -->
<source media="(max-width: 700px)" srcset="/images/portrait.jpg" />
<!-- Landscape image at 700px and up -->
<source media="(min-width: 700px)" srcset="/images/landscape.jpg" />
<!-- The fallback image -->
<img src="/images/portrait.jpg" alt="Hiker on a mountain ridge" />
</picture>

The browser looks for the first source that matches, so we can order the sources from a mobile-first approach, like the code shown above.

  • At 700px, the browser will serve portrait.jpg
  • At 700.1px, the browser will serve landscape.jpg

Art direction with Astro

Astro’s Picture component doesn’t support art direction, so you’ll have to build the <picture> element yourself if you want this. Here’s an example of the code you need.

index.astro
---
import { getImage } from 'astro:assets'
import landscape from '../assets/landscape.jpg'
import portrait from '../assets/portrait.jpg'
const widths = [400, 800, 1200, 1600]
const landscapeImg = await getImage({ src: landscape, widths, format: 'avif' })
const portraitImg = await getImage({ src: portrait, widths, format: 'avif' })
---
<picture>
<source
media="(max-width: 700px)"
srcset={portraitImg.srcSet.attribute}
type="image/avif"
/>
<source
media="(min-width: 700px)"
srcset={landscapeImg.srcSet.attribute}
type="image/avif"
/>
<img
src={portraitImg.src}
{...portraitImg.attributes}
alt="Hiker on a mountain ridge"
/>
</picture>

The output looks like this:

<picture>
<source
media="(max-width: 700px)"
srcset="
/_astro/portrait.dEf456.avif 400w,
/_astro/portrait.dEf456.avif 800w
"
type="image/avif"
/>
<source
media="(min-width: 700px)"
srcset="
/_astro/landscape.aBc123.avif 400w,
/_astro/landscape.aBc123.avif 800w,
/_astro/landscape.aBc123.avif 1200w
"
type="image/avif"
/>
<img
src="/_astro/portrait.dEf456.avif"
width="832"
height="1216"
loading="lazy"
decoding="async"
alt="Hiker on a mountain ridge"
/>
</picture>

This is an incredibly tedious process. It’s not something you’d want to do manually, so I built a Picture component that does art direction seamlessly.

Splendid Astro’s Picture component

Splendid Astro’s Picture works the same way as its sister Image component.

The difference is you include the properties for each type of picture in a property called artDirection. Here, min refers to a min-width media query, and max refers to a max-width media query.

---
import { Picture } from '@splendidlabz/astro'
---
<Picture
alt="Hiker on a mountain ridge"
artDirection={[
{ src: '/src/assets/portrait.jpg', max: 700 },
{ src: '/src/assets/landscape.jpg', min: 700 },
]}
/>

Splendid Astro’s Picture component outputs both avif and webp formats by default, so the HTML looks a little bit long.

<figure>
<picture>
<source
srcset="
/_astro/portrait.dEf456.avif 400w,
/_astro/portrait.dEf456.avif 800w
"
media="(max-width: 700px)"
type="image/avif"
/>
<source
srcset="
/_astro/portrait.dEf456.webp 400w,
/_astro/portrait.dEf456.webp 800w
"
media="(max-width: 700px)"
type="image/webp"
/>
<source
srcset="
/_astro/landscape.aBc123.avif 400w,
/_astro/landscape.aBc123.avif 800w,
/_astro/landscape.aBc123.avif 1200w
"
media="(min-width: 700px)"
type="image/avif"
/>
<source
srcset="
/_astro/landscape.aBc123.webp 400w,
/_astro/landscape.aBc123.webp 800w,
/_astro/landscape.aBc123.webp 1200w
"
media="(min-width: 700px)"
type="image/webp"
/>
<img
src="/_astro/portrait.dEf456.jpg"
width="832"
height="1216"
loading="lazy"
decoding="async"
alt="Hiker on a mountain ridge"
/>
</picture>
</figure>

The result is as follows:

A hiker on a mountain ridge at dawn

Picking the fallback

Splendid Astro uses the first image as the fallback image (because mobile-first!). If you wish to use another image as the fallback, you can include a fallbackIndex to indicate which image you want to use.

Here’s how you’d choose the second image (the landscape one) as the fallback.

<Picture
alt="Hiker on a mountain ridge"
fallbackIndex={1}
artDirection={[
{ src: '/src/assets/portrait.jpg', max: 700 },
{ src: '/src/assets/landscape.jpg', min: 700 },
]}
/>

When to use this

Use Picture with artDirection only if you want the image to change. If you just want the same image at smaller sizes, Image is simpler and good enough.

Code snippet to copy-paste

---
import { Picture } from '@splendidlabz/astro'
---
<Picture
alt="Description"
artDirection={[
{ src: '/src/assets/portrait.jpg', max: 700 },
{ src: '/src/assets/landscape.jpg', min: 700 },
]}
/>