Responsive Picture
Using the picture tag for performant, art-directed images while maintaining full accessibility.
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>
// Gold Standard: Art Direction and Performance with Accessibility
picture.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"
)
const ResponsivePicture = ({ desktopSrc, mobileSrc, alt }) => (
<picture>
<source media="(min-width: 1024px)" srcSet={desktopSrc} />
<img src={mobileSrc} alt={alt} loading="lazy" />
</picture>
);
<template>
<picture>
<source media="(min-width: 1024px)" :srcset="desktopSrc">
<img :src="mobileSrc" :alt="alt" loading="lazy">
</picture>
</template>
<picture class="block w-full overflow-hidden rounded-xl shadow-lg">
<source media="(min-width: 1024px)" srcset="https://images.unsplash.com/photo-1498050108023-c5249f4df085?auto=format&fit=crop&q=80&w=1200">
<img
src="https://images.unsplash.com/photo-1498050108023-c5249f4df085?auto=format&fit=crop&q=80&w=800"
alt="Developer workspace"
class="w-full object-cover aspect-video"
>
</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
imgmust 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?
- Performance Issues: Serving a 4K image to a mobile phone wastes bandwidth and slows down the experience.
- Missing Alt Text: Not providing an
altattribute on the fallbackimgleaves the entire<picture>invisible to screen readers. - Improper Fallback: Using only
sourcewithout a finalimgis invalid HTML and fails in older browsers.
β The Good (Accessibility-Ready Code)
Why This Works
- Art Direction: Different crops or versions of an image can be served based on device width.
- Centralized Accessibility: By placing the
alttext on the innerimg, we ensure that no matter which source is picked, the accessibility name remains consistent. - 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
altattribute on the<img>element, not thesourceorpicturetags. - Use descriptive
alttext that covers all versions of the art-directed image. - Use
loading="lazy"for performance, but only for images below the fold. - Verify that the
sourcemedia queries donβt accidentally hide the image on certain viewports.
<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
π¬ 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
EnterandSpace - Role: Identified as
Image