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
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.
<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 serveportrait.jpg - At
700.1px, the browser will servelandscape.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.
---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:
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 }, ]}/>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