Course contentsShow
← Web Development
Lesson 2014 of 2,98237. Image and Asset OptimizationFree lesson

Media Queries in Picture Sources

Apply min-width and orientation media queries to <source> tags for precise art direction control.

Media Queries in Picture Sources

You'll learn how to use min-width and orientation media queries inside <source> elements to give browsers precise instructions about which image to display based on viewport conditions.

What Are Media Queries in Picture Sources?

Just as you've been using the <picture> element for art direction (showing different images for different scenarios), you can attach media queries directly to each <source> tag. This tells the browser: "Only consider this source if these viewport conditions are met."

Think of it like a restaurant menu that changes based on the time of day—breakfast items only appear before 11am, lunch after. Similarly, your <source> tags can specify "only use this image when the screen is at least 768px wide" or "only in landscape orientation."

Core Syntax

<picture>
  <source 
    srcset="wide-hero.webp" 
    media="(min-width: 1024px)">
  
  <source 
    srcset="tablet-hero.webp" 
    media="(min-width: 768px)">
  
  <img src="mobile-hero.webp" alt="Hero image">
</picture>

The browser evaluates each <source> in order, top to bottom. The first one whose media query matches wins. The <img> tag serves as the fallback.

Orientation Queries

<picture>
  <source 
    srcset="landscape-photo.webp" 
    media="(orientation: landscape)">
  
  <source 
    srcset="portrait-photo.webp" 
    media="(orientation: portrait)">
  
  <img src="square-photo.webp" alt="Photo">
</picture>

Perfect for mobile devices that rotate—show a wide composition in landscape, a tall one in portrait.

Combining Conditions

You can combine multiple conditions:

<source 
  srcset="large-landscape.webp" 
  media="(min-width: 1024px) and (orientation: landscape)">

Key Takeaway: Media queries in <source> tags give you surgical control over art direction, letting you match specific images to specific viewport sizes and orientations for optimal visual presentation.