Skip to content

<picture> - The Picture Element

Media Element HTML5

The picture element provides a container for multiple image sources, enabling art direction (different images for different layouts) and format fallbacks. It gives developers precise control over which image is displayed based on viewport size, device capabilities, and format support.

Result
<picture>
<source srcset="image.webp" type="image/webp">
<source srcset="image.jpg" type="image/jpeg">
<img src="image.jpg" alt="Description">
</picture>

The browser evaluates <source> elements in order from top to bottom:

  1. Checks each <source> element’s media, type, and srcset attributes
  2. Uses the first matching source
  3. Falls back to the <img> element if no source matches
  4. The <img> element displays the selected source and provides the alt text

Art direction allows you to serve different crops or compositions based on viewport size:

Result
Result

Serve modern formats like AVIF and WebP with JPEG/PNG fallbacks:

Result

Format savings:

  • AVIF: ~50% smaller than JPEG
  • WebP: ~30% smaller than JPEG
  • Both maintain better quality at smaller sizes
Result

You can use both art direction and format fallbacks together:

Result

Use srcset in <source> elements for resolution switching:

Result
<picture>
<source media="(min-width: 1200px)" srcset="large.jpg">
<source media="(min-width: 768px)" srcset="medium.jpg">
<source media="(min-width: 480px)" srcset="small.jpg">
<img src="tiny.jpg" alt="Description">
</picture>
Result

Combine sizes with srcset for sophisticated responsive behavior:

Result
Result
Result
Result
Result

Use <picture> when you need:

  • Art direction: Different image compositions for different screen sizes
  • Format fallbacks: AVIF/WebP with JPEG/PNG fallback
  • Media queries: Complex conditions based on viewport, orientation, or preferences
  • Different aspect ratios: Square on mobile, landscape on desktop
Result

Use <img srcset> when you need:

  • Resolution switching: Same image, different sizes/resolutions
  • Simpler syntax: No art direction needed
  • Browser optimization: Let browser choose based on viewport and pixel density
Result
<!-- Different crops for mobile/desktop -->
<picture>
<source media="(min-width: 768px)"
srcset="wide-hero.jpg">
<img src="square-hero.jpg" alt="Hero">
</picture>
<!-- Modern format with fallback -->
<picture>
<source srcset="image.avif" type="image/avif">
<source srcset="image.webp" type="image/webp">
<img src="image.jpg" alt="Photo">
</picture>

Always provide alt text on the <img> element, not on <source>:

<picture>
<source srcset="image.webp" type="image/webp">
<img src="image.jpg" alt="Descriptive alt text">
</picture>
Result
  1. Order sources by specificity - Most specific first, most general last
  2. Use modern formats - AVIF and WebP can save 30-50% bandwidth
  3. Lazy load - Add loading="lazy" to the <img> element
  4. Set dimensions - Always include width and height on <img>
  5. Optimize all versions - Compress every image variant
  6. Test fallbacks - Ensure JPEG/PNG fallbacks work in older browsers
FeatureChromeFirefoxSafariEdge
<picture>38+38+9.1+13+
AVIF in <picture>85+93+16+85+
WebP in <picture>38+38+14+18+
Media queries38+38+9.1+13+
<!-- Missing img element -->
<picture>
<source srcset="image.webp" type="image/webp">
</picture>
<!-- img not last child -->
<picture>
<img src="image.jpg" alt="Photo">
<source srcset="image.webp" type="image/webp">
</picture>
<!-- Alt on source instead of img -->
<picture>
<source srcset="image.webp" type="image/webp" alt="Wrong">
<img src="image.jpg">
</picture>
<!-- Reversed media query order -->
<picture>
<source media="(min-width: 400px)" srcset="small.jpg">
<source media="(min-width: 800px)" srcset="large.jpg">
<img src="tiny.jpg" alt="Photo">
</picture>

The actual image element that displays within picture. Learn more →