Codectionary / Developer documentation / HTML

<picture> and <source>

The <picture> element lets the browser choose the most appropriate image from multiple <source> options based on screen size, resolution, or format support, falling back to a required <img> element if none match. This is the standard way to serve responsive, art-directed, or next-gen image formats (like WebP or AVIF) with automatic fallback.

Syntax

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

Examples

Format Fallback

Serving a modern format with a JPEG fallback.

<picture>
  <source srcset="photo.avif" type="image/avif">
  <source srcset="photo.webp" type="image/webp">
  <img src="photo.jpg" alt="Mountain landscape">
</picture>

Responsive Art Direction

Showing a different crop of the same image depending on screen width.

<picture>
  <source media="(min-width: 800px)" srcset="banner-wide.jpg">
  <source media="(min-width: 400px)" srcset="banner-medium.jpg">
  <img src="banner-small.jpg" alt="Homepage banner">
</picture>

Resolution Switching

Serving higher-resolution images to high-density screens.

<picture>
  <source srcset="icon.png 1x, icon@2x.png 2x">
  <img src="icon.png" alt="App icon">
</picture>

Attributes

  • srcset (string): Used on source; one or more candidate image URLs
  • media (string): A media query controlling when that source is used
  • type (string): The MIME type of the image resource, letting the browser skip unsupported formats

Best practices

  • Always include a fallback <img> as the last child — it is what renders if no source matches, and is required for accessibility
  • Put the alt attribute on the fallback <img>, not on picture or source
  • List source elements from most to least preferred format, since the browser uses the first one it supports
  • Use picture for format/art-direction switching, and the simpler img srcset attribute for pure resolution switching

At a glance

Purpose
Structure and meaning for web content
File extension
.html · .htm
Runs in
Web browsers
Usually used with
CSS and JavaScript

Specifications & further reading

Related HTML documentation