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

The Order of <source> Elements Matters

Learn how browsers evaluate source elements from top to bottom and match the first valid one.

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:

  1. Start at the first <source> element
  2. Check if its media condition matches (and/or type is supported)
  3. If YES → use this source and stop looking
  4. 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.