Combining srcset with Picture
You'll learn how to use srcset inside <picture> elements to handle different image crops and multiple resolutions at the same time.
The Power of Combining Both Techniques
You've learned that <picture> lets you change which image loads based on conditions (art direction), and srcset lets the browser pick the best resolution of the same image. The real magic happens when you use them together.
Inside each <source> element within <picture>, you can add a srcset attribute. This means: "For this particular art direction scenario, here are multiple resolution options to choose from."
How It Works
Think of it like a restaurant menu with sections. The <picture> element says "choose your meal category based on your appetite" (screen size), and srcset says "within that category, pick small/medium/large portions" (resolution).
<picture>
<source
media="(min-width: 800px)"
srcset="hero-wide-1x.jpg 1x,
hero-wide-2x.jpg 2x">
<source
media="(min-width: 400px)"
srcset="hero-square-1x.jpg 1x,
hero-square-2x.jpg 2x">
<img
srcset="hero-narrow-1x.jpg 1x,
hero-narrow-2x.jpg 2x"
src="hero-narrow-1x.jpg"
alt="Hero image">
</picture>
The browser first checks which <source> matches (art direction), then picks the appropriate resolution from that source's srcset. This gives you complete control: different crops for different layouts, with high-DPI versions for each.
Key Takeaway: Combining srcset within <picture> sources lets you serve both the right image crop (art direction) and the right resolution (clarity) for any device.