The Order of <source> Elements Matters
What you'll learn: How browsers read <source> elements from top to bottom and stop at the first match.
When you use the <picture> element with multiple <source> children, the order you write them in your HTML directly affects which image gets displayed. This isn't arbitrary—browsers follow a strict top-to-bottom evaluation process.
How Browsers Evaluate Sources
Think of <source> elements like a checklist a browser works through sequentially:
- Start at the first
<source>element - Check if its
mediacondition matches (and/ortypeis supported) - If YES → use this source and stop looking
- If NO → move to the next
<source>and repeat
The browser never goes back up once it finds a match. This means your order creates a priority system.
The Rule: Most Specific First
Always place your most specific conditions at the top and broadest conditions toward the bottom. The fallback <img> goes last.
Wrong order (won't work as intended):
<picture>
<source srcset="wide.jpg" media="(min-width: 400px)">
<source srcset="ultrawide.jpg" media="(min-width: 1200px)">
<img src="default.jpg" alt="Landscape">
</picture>
At 1400px viewport width, the browser matches the first source (400px minimum) and stops—you never get the ultrawide version!
Correct order:
<picture>
<source srcset="ultrawide.jpg" media="(min-width: 1200px)">
<source srcset="wide.jpg" media="(min-width: 400px)">
<img src="default.jpg" alt="Landscape">
</picture>
Now the browser checks 1200px first, finds a match, and displays the correct image.
Key Takeaway: Browsers evaluate <source> elements from top to bottom and use the first valid match—always order from most specific to least specific conditions.