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

Type Attribute for Format Negotiation

Combine type and media attributes to serve modern formats like WebP with art direction fallbacks.

Type Attribute for Format Negotiation

What you'll learn: How to serve modern image formats like WebP alongside traditional formats, with optional art direction per device.


The Modern Format Challenge

Not all browsers support cutting-edge image formats like WebP or AVIF, even though they offer dramatically smaller file sizes. You need a way to say "use this WebP if you can, otherwise fall back to JPEG" — and potentially do that per breakpoint when combining with art direction.

How the type Attribute Works

Inside a <picture> element, each <source> can declare its MIME type using the type attribute. The browser tests each source in order and picks the first one where both the media query matches and the format is supported:

<picture>
  <!-- Desktop art direction, WebP format -->
  <source 
    media="(min-width: 800px)" 
    srcset="hero-wide.webp" 
    type="image/webp">
  
  <!-- Desktop art direction, JPEG fallback -->
  <source 
    media="(min-width: 800px)" 
    srcset="hero-wide.jpg">
  
  <!-- Mobile art direction, WebP format -->
  <source 
    srcset="hero-narrow.webp" 
    type="image/webp">
  
  <!-- Mobile fallback -->
  <img src="hero-narrow.jpg" alt="Hero scene">
</picture>

The browser evaluates top-to-bottom: if it's desktop and supports WebP, the first source wins. If desktop but no WebP support, the second wins. Mobile devices follow the same pattern with their sources.

Think of It as a Checklist

The browser asks: "Does my viewport match? ✓ Can I decode this format? ✓" — first match wins. Always end with the <img> for universal fallback.

Key Takeaway:
The type attribute lets you combine format negotiation with art direction by stacking multiple <source> elements that test both media conditions and browser format support.