Responsive Picture

Using the picture tag for performant, art-directed images while maintaining full accessibility.

intermediate
1.1.1 Non-text Content (Level A)1.4.4 Resizing Text (Level AA)
❌

The Bad (Inaccessible)

<img src="https://images.unsplash.com/photo-1498050108023-c5249f4df085?auto=format&fit=crop&q=80&w=800" class="responsive-img" alt="Summer sale banner">
βœ…

Accessibility-Ready Code

<!-- Gold Standard: Art Direction and Performance with Accessibility -->
<picture class="accessible-picture">
  <!-- Desktop Version (Art Directed) -->
  <source
    media="(min-width: 1024px)"
    srcset="https://images.unsplash.com/photo-1498050108023-c5249f4df085?auto=format&fit=crop&q=80&w=1200"
  >
  <!-- Tablet/Mobile Version -->
  <source
    media="(min-width: 640px)"
    srcset="https://images.unsplash.com/photo-1498050108023-c5249f4df085?auto=format&fit=crop&q=80&w=800"
  >
  <!-- Fallback Image (Where Alt Text Lives) -->
  <img
    src="https://images.unsplash.com/photo-1498050108023-c5249f4df085?auto=format&fit=crop&q=80&w=640"
    alt="Hands typing on a laptop in a brightly lit modern office"
    loading="lazy"
  >
</picture>

The Standard

The <picture> element is used for responsive images and art direction. Crucially, the accessibility properties (like the alt text) reside on the inner <img> element. The browser chooses the source, but the img remains the semantic anchor.

WCAG Criteria

  • 1.1.1 Non-text Content: The fallback img must provide the text alternative.
  • 1.4.4 Resizing Text: Ensuring images scale correctly for different viewports without losing context.

❌ The Bad (Inaccessible)

What’s Wrong?

  1. Performance Issues: Serving a 4K image to a mobile phone wastes bandwidth and slows down the experience.
  2. Missing Alt Text: Not providing an alt attribute on the fallback img leaves the entire <picture> invisible to screen readers.
  3. Improper Fallback: Using only source without a final img is invalid HTML and fails in older browsers.

βœ… The Good (Accessibility-Ready Code)

Why This Works

  1. Art Direction: Different crops or versions of an image can be served based on device width.
  2. Centralized Accessibility: By placing the alt text on the inner img, we ensure that no matter which source is picked, the accessibility name remains consistent.
  3. Native Optimization: Browsers handle the source selection natively, providing the fastest possible experience.

Accessibility Checklist

  • Always include a fallback <img> as the last child of the <picture>.
  • Place the alt attribute on the <img> element, not the source or picture tags.
  • Use descriptive alt text that covers all versions of the art-directed image.
  • Use loading="lazy" for performance, but only for images below the fold.
  • Verify that the source media queries don’t accidentally hide the image on certain viewports.
πŸ“ Perfect Implementation Reference
<picture>
  <source media="(min-width: 800px)" srcset="https://images.unsplash.com/photo-1522071823991-b1ae5e6a39c1?auto=format&fit=crop&q=80&w=1200">
  <img src="https://images.unsplash.com/photo-1522071823991-b1ae5e6a39c1?auto=format&fit=crop&q=80&w=400" alt="A team collaborating in a modern office">
</picture>

Technical Deep Dive

The Secret of <picture>

Assistive technologies don’t β€œsee” the <picture> or <source> elements directly. They interact with the resulting <img> element. The browser’s job is simply to swap the src of that image based on your rules.

Use Case: Art Direction

Sometimes you want a wide landscape image for desktop but a zoomed-in square crop for mobile. The <picture> tag is perfect for this, as long as the alt text remains relevant to both crops.

πŸ§ͺ

Interactive Behavioral Lab

πŸ’»

Interactive Sandbox

LIVE PREVIEW

πŸ”¬ Technical Internals

Understand how this component is processed by the browser and Assistive Technology (AT). This section bridges the gap between visual code and the hidden logic that powers accessibility.

🌲 Accessibility Tree

The data structure used by screen readers to "see" your page. It translates HTML roles and attributes into standardized objects.

βš™οΈ Event Logic

Expected behavioral standards for keyboard navigation and state transitions. Crucial for users who don't use a mouse.

  • Focus: Highlights via :focus-visible
  • Activation: Responds to Enter and Space
  • Role: Identified as Image

🌐 Browser & Screen Reader Compatibility

Browser
Screen Reader
Status
🍎 Safari
VoiceOver
βœ“ Safe
πŸͺŸ Chrome
NVDA
βœ“ Safe
πŸͺŸ Edge
JAWS
βœ“ Safe
🦊 Firefox
NVDA
βœ“ Safe
πŸ“± iOS Safari
VoiceOver
βœ“ Safe
πŸ€– Chrome Android
TalkBack
βœ“ Safe