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.
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*.
Users reach for <picture> in three cases:
- Format negotiation: WebP/AVIF where a legacy browser needs JPEG. The format comparison article covers the trade-offs.
- Art direction: a tall portrait on a phone, a wide landscape on desktop, using the same semantic image.
- Derivations from one source: an image CDN or content system produces per-context renditions, and picture serves the right one.
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:
- The
<img>is the fallback and the element that appears in the DOM;<source>elements never render. The<img>carriesaltandwidth/height, which is what the image optimisation guide and the layout-shift article need for accessibility and CLS. - The browser selects the first
<source>whosetypeandmediaboth match, as a lexical order from top to bottom. So put the newest format first and the JPEG<img>last. mediais optional; use it only for art direction. Size-based switching inside a<source>usessrcseton the source with nomedia, exactly like an<img>.
Deep dive: the three rules that matter
typeis checked first,mediasecond. A source withtype="image/avif"and nomediais selected when the browser supports AVIF, silently skipping it otherwise. Amediasource without atypeis selected when the media matches, regardless of the format the file is.- Keep
mediaand size switching on separate sources. A source with bothsrcsetandmediais selected only when the media matches; if it is the only fallback and the media fails, nothing serves. Use one source pertypefor format negotiation and one (srcset, no media) for size candidates. - The
<img>'ssrcis the loading order. The browser preloads the candidate the winning source declares. Theimgfetchpriorityandloadingstill matter:loading="lazy"and afetchpriority="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.