Art Direction with the Picture Element
What you'll learn: How to serve completely different images for different screen sizes—like showing a wide landscape on desktop and a cropped portrait on mobile—using the <picture> element.
The Problem srcset Can't Solve
You've learned how srcset lets browsers choose between different resolutions of the same image. But what if you need entirely different images at different breakpoints? Maybe your desktop hero image is a wide landscape, but on mobile you want a tighter crop focusing on the subject's face. This is called art direction, and <picture> is your tool.
How Picture Works
The <picture> element wraps multiple <source> elements, each with its own srcset and media attribute (a media query). The browser picks the first matching source.
<picture>
<source media="(min-width: 800px)" srcset="landscape.jpg">
<source media="(min-width: 400px)" srcset="square.jpg">
<img src="portrait.jpg" alt="Mountain view">
</picture>
Here's what happens:
- Desktop (≥800px): Shows
landscape.jpg - Tablet (≥400px): Shows
square.jpg - Mobile (<400px): Shows
portrait.jpg(the<img>fallback)
The img Element is Required
The <img> inside <picture> isn't optional—it's the fallback for old browsers and the actual element that displays the chosen image. Always include src and alt on it.
Think of It Like a Menu
The <picture> element presents a menu of image options, each paired with a condition. The browser walks down the list and picks the first dish (source) that matches its appetite (viewport size).
Key Takeaway: Use <picture> with <source> elements when you need to serve completely different images at different breakpoints, not just different resolutions of the same image.