šŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
šŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

<picture>: Art Direction And Format Fallback

Master the <picture> element's structure of <source> candidates and a mandatory <img> fallback, how media-conditioned sources enable true art direction, and how the same pattern enables format-based fallback via the type attribute.

⚔ Total XP: 0|šŸ’» html XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

picture Element

Art direction & format fallback.


šŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
šŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

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.

<picture>
  <source media="(max-width: 600px)" srcset="mobile-crop.jpg">
  <img src="desktop-crop.jpg" alt="Team at product launch">
</picture>
localhost:3000
āœ“ Always Renders, Always Carries altRegardless of which matched, the same accessible name applies.

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.

<picture>
  <source media="(max-width: 600px)" srcset="portrait.jpg">
  <img src="landscape.jpg" alt="...">
</picture>
localhost:3000
≤600px viewport →
portrait.jpg (tighter crop)
>600px viewport →
landscape.jpg (wider crop)

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.

<picture>
  <source type="image/avif" srcset="photo.avif">
  <source type="image/webp" srcset="photo.webp">
  <img src="photo.jpg" alt="...">
</picture>
localhost:3000
āœ“ Zero-JS Automatic Format NegotiationEach browser automatically receives the best format it can decode, no detection logic required.

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

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

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

THE BUG

A <picture> element's image never changes between mobile and desktop despite having two <source> elements with different media conditions.

THE FIX

Verify each <source>'s media condition syntax is valid and that the conditions are mutually exclusive and cover the relevant breakpoints correctly.

THE BUG

A screen reader announces no description at all for a <picture> element.

THE FIX

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>

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Placing alt text on a <source> element instead of <img>

<picture> <source media="..." srcset="..."> <img src="..." alt="Correct description here"> </picture>

The Solution //

<source> elements never carry alt; only the fallback <img> does.

The Error //

Using <picture> for simple resolution switching with no real art direction or format need

<!-- Simpler for pure resolution switching --> <img srcset="..." sizes="..." src="..." alt="...">

The Solution //

Use plain <img srcset sizes> for simple resolution switching; reserve <picture> for genuine art-direction or format-fallback cases.

Lesson Glossary

[01]<picture>

A wrapper enabling conditional image source selection.

Code Preview
<picture><source><img></picture>

[02]<source> (image)

A conditional image candidate within <picture>.

Code Preview
media="..." or type="..."

[03]Art Direction

Serving genuinely different crops per layout via <picture>.

Code Preview
media attribute

[04]Format Fallback

Offering modern formats with automatic degradation.

Code Preview
type attribute

Continue Learning