Picture, Source, and Art Direction: Responsive Images Beyond Resolution Switching
The picture element handles art direction and format switching: media and type on source, AVIF and WebP fallbacks, dimensions, and common pitfalls.
<picture> when the browser must not be left to choose freely: when different screens need a different crop or composition (art direction), or when you offer modern formats like AVIF with fallbacks (format switching). Each <source> is a condition plus a srcset; the first matching source wins, and the <img> inside is required, carries the alt text and styling, and acts as the fallback. For a single image at different sizes, plain srcset and sizes are simpler and give the browser more freedom.
A homepage hero shows a team of six people in front of a whiteboard, shot wide for desktop. On a phone, the same image shrinks to a strip where each face is a few pixels across and the whiteboard text is unreadable. Serving the same composition at a smaller size does not help; the phone needs a different crop, tighter on two people, in a taller aspect ratio. No amount of srcset can express that, because srcset only describes sizes of the same picture.
The <picture> element wraps one or more <source> elements and a required <img>. Each source can carry a media condition, a type (a MIME type) or both, plus its own srcset and sizes. The browser walks the sources in order, uses the first one whose conditions match, and falls back to the img if none do. Unlike srcset alone, these conditions are commands, not hints: if a source matches, the browser must use it.
How do you do art direction with media?
<picture>
<source media="(min-width: 1024px)"
srcset="/img/hero-wide-1600.webp 1600w, /img/hero-wide-2400.webp 2400w"
sizes="100vw" width="2400" height="1000">
<source media="(min-width: 600px)"
srcset="/img/hero-medium-900.webp 900w, /img/hero-medium-1400.webp 1400w"
sizes="100vw" width="1400" height="900">
<img src="/img/hero-tall-800.webp"
srcset="/img/hero-tall-600.webp 600w, /img/hero-tall-800.webp 800w, /img/hero-tall-1200.webp 1200w"
sizes="100vw" width="1200" height="1500"
alt="Two product managers reviewing a roadmap on a whiteboard"
fetchpriority="high">
</picture>
Large screens get the wide 12:5 composition, tablets a 14:9 crop, and phones a tall 4:5 crop. Each source still has a srcset with several widths, so within its media range the browser still picks a resolution suited to the screen. Art direction and resolution switching work together.
Order matters. Sources are checked top to bottom and the first match wins, so put the most specific or largest condition first. If the phone-sized source with no media condition came first, every device would get it.
Different aspect ratios need per-source dimensions
Width and height attributes reserve space and prevent layout shift. With art direction, each source has a different aspect ratio, so each needs its own width and height, as in the example. Every major browser has honored dimensions on <source> since Firefox 108 added support in late 2022. Without them, the browser uses the img‘s aspect ratio for every source and the layout jumps when the real image loads.
How do you switch image formats with type?
AVIF files are typically much smaller than JPEG at the same visual quality, and WebP sits between the two. AVIF now works in every major browser: Chrome since 85, Firefox since 93, Safari since 16.4 and, finally, Edge since version 121 in January 2024. WebP has been universal for longer. A typed <picture> lets you serve the best format each browser supports, with no server-side detection:
<picture>
<source type="image/avif"
srcset="/img/kanban-400.avif 400w, /img/kanban-800.avif 800w, /img/kanban-1200.avif 1200w"
sizes="(min-width: 1200px) 720px, calc(100vw - 32px)">
<source type="image/webp"
srcset="/img/kanban-400.webp 400w, /img/kanban-800.webp 800w, /img/kanban-1200.webp 1200w"
sizes="(min-width: 1200px) 720px, calc(100vw - 32px)">
<img src="/img/kanban-800.jpg"
srcset="/img/kanban-400.jpg 400w, /img/kanban-800.jpg 800w, /img/kanban-1200.jpg 1200w"
sizes="(min-width: 1200px) 720px, calc(100vw - 32px)"
width="1200" height="675" alt="Kanban board with To do, Doing and Done columns"
loading="lazy">
</picture>
A browser that cannot decode a type skips that source. The sizes value is repeated on every source because each srcset is evaluated separately. The sizing logic is the same as in srcset and sizes; <picture> just adds the format choice on top.
Whether you still need the WebP layer in 2024 depends on your audience. With AVIF in every current browser, an AVIF source plus a JPEG fallback in the img covers nearly everyone. Keep WebP if analytics show a meaningful share of older Safari or Edge versions. An image CDN that negotiates the format from the request’s Accept header is the alternative: one URL, no <picture> needed for formats, at the cost of depending on the CDN.
Why does picture still need an img element?
<picture> is only a container that changes which file the <img> displays. Everything else lives on the img:
altgoes on theimg. Sources have no alt text. If art direction crops out important content on small screens, write alt text that is true for every crop.- Styling targets the
img. Classes and CSS on<picture>affect the wrapper, which is an inline element by default. Stylepicture imgor put the class on the image, and setdisplay: blockon the picture if it sits in a layout. loading,decodingandfetchprioritygo on theimgand apply to whichever source is chosen.- JavaScript should read
img.currentSrcto see which file was used, and listen forloadon theimg.
Leaving out the img is the classic mistake: without it, nothing renders at all, in any browser.
Other useful media conditions
media accepts any media query, which opens up uses beyond screen width:
<!-- A dark-mode version of a screenshot or diagram -->
<picture>
<source srcset="/img/dashboard-dark.webp" media="(prefers-color-scheme: dark)">
<img src="/img/dashboard-light.webp" width="1200" height="750"
alt="Analytics dashboard with a weekly revenue chart">
</picture>
<!-- A still image for people who prefer reduced motion -->
<picture>
<source srcset="/img/onboarding-still.webp" media="(prefers-reduced-motion: reduce)">
<img src="/img/onboarding.gif" width="640" height="400"
alt="Dragging a task card from To do to Doing">
</picture>
Product screenshots in documentation are a good fit for the first pattern: a bright light-theme screenshot on a dark page is glaring. The second respects users who have asked their operating system to reduce motion, which matters for vestibular disorders, and is one of the few ways to honor that preference for animated GIFs.
When should you not use picture?
- Same image, different sizes: use
srcsetandsizeson a plainimg. Addingmediaconditions here takes the decision away from the browser, which could otherwise pick a smaller file on a slow connection. - Decorative backgrounds: CSS
image-set()and media queries handle background images. - Icons: SVG scales to any size with one file.
Art direction also has a production cost: every crop is a separate asset that someone has to create and keep in sync when the campaign changes. Use it for heroes and key marketing images where composition genuinely matters, not for every article image.
Frequently Asked Questions
When should I use picture instead of srcset?
Use picture for art direction, when different screens need a different crop or composition, and for format switching, such as AVIF with a JPEG fallback. For the same image at different sizes, a plain img with srcset and sizes is simpler and lets the browser optimize more freely.
Is the img element required inside picture?
Yes. The img is what actually displays the image; source elements only change which file it uses. Without an img, nothing renders. The img also carries the alt text, dimensions, loading attribute and styling.
Do I still need WebP fallbacks for AVIF?
AVIF is supported in all current major browsers since Edge 121 in January 2024, so an AVIF source with a JPEG fallback covers most audiences. Keep a WebP source if your analytics show significant traffic from older Safari or Edge versions.
Where do width and height go when sources have different aspect ratios?
Put width and height on each source as well as on the img. Browsers use the dimensions of the matching source to reserve space, which prevents layout shift when different crops have different aspect ratios.
Control when it matters, freedom when it does not
The picture element is for the cases where you, not the browser, must decide: a crop that reads well on a phone, a format the browser can actually decode, a dark-mode screenshot. Everywhere else, describe sizes with srcset and let the browser choose. Put alt text, dimensions and loading behavior on the img, order sources from most to least specific, and you get the right image on every screen without a line of JavaScript. For the full reference, see the MDN picture element reference and MDN on image file types. Related reading on this site: native lazy loading.
Originally posted on 12 March 2024 and post last modified on 12 March 2024