Tutorial · html-css-js · Published 2026-08-16 · 3 min read

The picture element: choosing formats and density

The picture element explained: format fallbacks, media queries, width and height, and how it composes with srcset and AVIF/WebP.

Flat editorial illustration showing wireframe bricks and translucent layer panels assembling into a small browser window outline with dual panes.
Illustration: this article at a glance.

When the picture element is the right tool

The <picture> element exists for one job: serving different images to different contexts, where "context" means a device capability (whether a browser supports AVIF) or a layout condition (mobile crops a photo differently than desktop). It makes the format decision the browser's to make, with a graceful fallback baked in. The srcset article is the sibling that picks among *sizes of the same image; picture is for different images*.

Editorial close-up illustration showing wireframe bricks and translucent layer panels assembling into a small browser window outline with dual panes.
Illustration: a closer look at the technique described above.

Users reach for <picture> in three cases:

Syntax anatomy

<picture>
  <source
    srcset="/img/hero.avif"
    type="image/avif"
    media="(min-width: 48rem)"
  />
  <source srcset="/img/hero.webp" type="image/webp" />
  <img
    src="/img/hero.jpg"
    alt="Worker taking a site live"
    width="1440"
    height="810"
  />
</picture>

The essential facts:

Deep dive: the three rules that matter

  1. type is checked first, media second. A source with type="image/avif" and no media is selected when the browser supports AVIF, silently skipping it otherwise. A media source without a type is selected when the media matches, regardless of the format the file is.
  2. Keep media and size switching on separate sources. A source with both srcset and media is selected only when the media matches; if it is the only fallback and the media fails, nothing serves. Use one source per type for format negotiation and one (srcset, no media) for size candidates.
  3. The <img>'s src is the loading order. The browser preloads the candidate the winning source declares. The img fetchpriority and loading still matter: loading="lazy" and a fetchpriority="high" on the hero behave exactly as on an <img> alone.

The no-React footnote

Server-rendered templates and static generators can use plain <picture> in the HTML directly. Client-side frameworks also support it through their JSX-to-HTML translations, but if you are emitting <picture> from a React component, remember the source and img are separate elements and the keys matter for hydration. For the rest of this resource, the pattern stays framework-agnostic: image optimisation runs the broader pipeline and format comparison weighs AVIF against WebP for the source files.

Need a website built, fixed, optimised, migrated or replaced?

This technical resource is written by CSMBAC, a small design and development studio. If you would rather hand the problem to a professional, the website service page explains how we build enquiry-ready websites.

Explore website services