The <picture> element solves the art-direction problem introduced in the Responsive Images lesson ā genuinely different image compositions per layout ā and, as a structural bonus, also provides the mechanism for modern image format fallback.
1The Structure: Sources Plus A Mandatory Fallback
A <picture> element contains any number of <source> elements, each offering a conditional image alternative, followed by exactly one required <img> element as the final fallback. The browser evaluates each <source> in order, using the first one whose condition matches (or whose declared format it supports), and falls through to the <img> if none match.
Critically, <source> elements never render anything directly and carry no alt attribute ā the <img> element is what actually displays, and is the sole carrier of the required accessible name, regardless of which <source> (if any) ultimately determined which image file loaded.
2Genuine Art Direction With media Conditions
Each <source>'s media attribute functions like a CSS media query, letting different <source> elements offer genuinely different image files ā not just different resolutions of the same crop, but actually different compositions ā based on viewport width or other media features.
This directly addresses the art-direction problem from the Responsive Images lesson: a tightly-cropped portrait orientation photo optimized for a narrow mobile screen, versus a wide landscape crop that makes better use of available desktop space, both derived from the same source photo but composed differently for their respective contexts.
3A Second Use Case: Modern Format Fallback
The same <source>-plus-fallback structure elegantly solves an entirely different problem: offering a smaller, modern image format (AVIF, WebP ā both covered in the next two lessons) to browsers that support it, while transparently falling back to a universally-compatible JPEG or PNG for browsers that don't.
Here, the type attribute (rather than media) declares each <source>'s MIME type; the browser automatically skips any <source> whose declared type it can't decode, falling through to the next candidate or ultimately the <img> ā requiring zero JavaScript or server-side browser detection to achieve reliable, automatic format negotiation.
4Step-by-Step Breakdown
Solving The Problem srcset Can't. Recall from the Responsive Images lesson: srcset solves resolution switching, but not art direction ā genuinely different crops per layout. The <picture> element solves that distinct problem, and as a bonus, also solves image-format fallback, covered in the WebP and AVIF lessons that follow.
<picture> Wraps Multiple <source> Candidates Plus A Fallback <img>. A <picture> element contains one or more <source> elements, each with its own media condition and srcset, followed by a mandatory <img> as the final fallback ā the <img> is what actually renders and carries the alt text; <source> elements only supply conditional alternatives.
picture Element Structure. Within a <picture> element, which element carries the required alt attribute?
- āEach <source> element individually
- āThe single, mandatory <img> element
- āThe <picture> wrapper itself
media Conditions Select Between Genuinely Different Crops. Each <source>'s media attribute works like a CSS media query, letting art direction change the actual image composition ā not just its resolution ā based on viewport, exactly the scenario srcset alone cannot address.
Art Direction With media. A design calls for a tightly-cropped portrait photo on mobile and a wide landscape crop on desktop, of the same underlying photo. Which HTML feature correctly implements this?
- āsrcset alone, since it's the same underlying photo
- ā<picture> with <source media="..."> for each distinct crop
- āCSS object-position alone, without any HTML changes
<picture> Also Enables Format Fallback. The same <source>-with-fallback structure elegantly solves a second, unrelated problem: offering a modern image format (WebP, AVIF) to supporting browsers while falling back to universally-supported JPEG/PNG for browsers that don't ā using the type attribute instead of media.
Format Fallback With picture. Which <picture>/<source> attribute is used specifically for offering a modern image format with automatic fallback, as opposed to art direction?
- āmedia, the same attribute used for art direction
- ātype, specifying the MIME type of the format
- āA dedicated format attribute
picture Element Mastered. You now understand the <picture> element's structure, how media-conditioned <source> elements solve genuine art-direction problems srcset alone can't, and how the same mechanism, via the type attribute, also enables modern image format fallback ā setting up the WebP and AVIF lessons ahead.
Provide An Image Fallback. <picture> requires a fallback <img> for browsers that don't support any listed <source>.
Level Up š
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1A Single alt Attribute On The Fallback <img> Covers Every Possible Rendered Source
Regardless of art-direction crop or format negotiation outcome, the underlying visual content and its meaning remain the same, so one accurate alt attribute correctly serves every possible rendering path.
SEO Implications
- 1
picture-Based Format Fallback Directly Enables The AVIF/WebP File-Size Savings Covered In The Next Two Lessons
Without a reliable, automatic fallback mechanism, adopting newer, smaller image formats would require risky browser detection; <picture> removes that risk entirely, making format adoption safe and straightforward.
Best Practices
Reserve <picture> For Genuine Art-Direction Or Format-Fallback Needs, Not As A Default For Every Image
Simple resolution switching is fully served by plain <img srcset sizes> without the additional markup complexity of <picture> ā reach for it specifically when composition or format needs to vary.
Always List <source> Elements In Priority Order, With The Most Preferred Format Or Condition First
The browser uses the first matching <source> it encounters, so ordering directly determines which candidate wins when multiple conditions could technically apply.
Frequent Bugs
A <picture> element's image never changes between mobile and desktop despite having two <source> elements with different media conditions.
Verify each <source>'s media condition syntax is valid and that the conditions are mutually exclusive and cover the relevant breakpoints correctly.
A screen reader announces no description at all for a <picture> element.
Ensure the required <img> fallback element has a correct, non-empty alt attribute ā accessibility naming lives only on <img>, never on <source>.
Real-World Examples
Combined Art Direction And Resolution Switching
A hero image using <picture> for art direction, with each <source> also using srcset internally for resolution switching within that crop.
<picture>
<source media="(max-width: 600px)" srcset="portrait-480.jpg 480w, portrait-960.jpg 960w">
<img srcset="landscape-800.jpg 800w, landscape-1600.jpg 1600w" src="landscape-800.jpg" alt="Team celebrating launch">
</picture>