Course contentsShow
← Web Development
Lesson 1232 of 2,98222. Responsive Images and MediaFree lesson

Combining srcset with Picture Sources

Use srcset within picture source elements to provide both art direction and resolution switching.

Combining srcset with Picture Sources

What you'll learn: How to use srcset inside <picture> elements to give the browser both art direction control and resolution choices for each breakpoint.

The Best of Both Worlds

You already know that <picture> lets you serve completely different images for different screen sizes (art direction), and that srcset lets the browser pick the best resolution version of a single image. Here's the powerful part: you can use srcset within each <source> element inside <picture>.

Think of it like a restaurant menu with categories. The <picture> element is the menu structure (appetizers, entrees, desserts), the media attributes decide which category you're ordering from, and srcset gives you size options within that category (small, medium, large).

How It Works

Each <source> element can have its own srcset and sizes attributes. The browser first uses the media conditions to choose which <source> matches, then uses that source's srcset to pick the best resolution:

<picture>
  <source 
    media="(min-width: 1000px)"
    srcset="wide-800.jpg 800w, wide-1200.jpg 1200w, wide-1600.jpg 1600w"
    sizes="80vw">
  
  <source 
    media="(min-width: 600px)"
    srcset="square-600.jpg 600w, square-900.jpg 900w"
    sizes="90vw">
  
  <img 
    src="tall-400.jpg"
    srcset="tall-400.jpg 400w, tall-600.jpg 600w"
    sizes="100vw"
    alt="Responsive hero image">
</picture>

On a wide desktop, the browser uses the first source and picks between three resolutions. On a tablet, it switches to the square crop and chooses from those resolution options.

Key Takeaway: Combining srcset with <picture> sources gives you complete control—different crops for different layouts, plus optimal file sizes for every screen density and viewport.