HTML Picture Element and Art Direction
Select a different composition
picture contains optional source elements followed by one img. Use this for art direction when narrow and wide screens need different compositions. The browser checks source conditions in order and uses the first suitable source set. The img provides the displayed element and text alternative.
<picture> <source media="(max-width: 400px)" srcset="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22160%22%20height%3D%22160%22%20viewBox%3D%220%200%20160%20160%22%3E%3Crect%20width%3D%22160%22%20height%3D%22160%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%20160%20L80%2032%20L160%20160Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E" width="160" height="160"> <img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22480%22%20height%3D%22240%22%20viewBox%3D%220%200%20480%20240%22%3E%3Crect%20width%3D%22480%22%20height%3D%22240%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%20240%20L240%2048%20L480%20240Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E" alt="Green hill beneath a blue sky" width="480" height="240" style="max-width: 100%; height: auto"></picture>A narrow viewport uses the square composition; wider screens use the landscape fallback.
Use format hints
A type hint names the MIME type of a source so browsers can skip formats they do not support. The source inside picture uses srcset, not src. The sample uses SVG to keep the demo self-contained; real projects can list AVIF or WebP followed by an appropriate fallback.
<picture> <source type="image/svg+xml" srcset="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22160%22%20height%3D%2280%22%20viewBox%3D%220%200%20160%2080%22%3E%3Crect%20width%3D%22160%22%20height%3D%2280%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%2080%20L80%2016%20L160%2080Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E"> <img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22320%22%20height%3D%22160%22%20viewBox%3D%220%200%20320%20160%22%3E%3Crect%20width%3D%22320%22%20height%3D%22160%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%20160%20L160%2032%20L320%20160Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E" alt="Green hill" width="160" height="80"></picture>A browser that accepts SVG may choose the first source.
Keep the fallback meaningful
Do not place alt on source or omit the final img. The shared alternative should remain true for all compositions. Use media conditions for intentional composition changes rather than for forcing an exact download size.
<picture> <source media="(min-width: 2000px)" srcset="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22480%22%20height%3D%22240%22%20viewBox%3D%220%200%20480%20240%22%3E%3Crect%20width%3D%22480%22%20height%3D%22240%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%20240%20L240%2048%20L480%20240Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E"> <img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22160%22%20height%3D%2280%22%20viewBox%3D%220%200%20160%2080%22%3E%3Crect%20width%3D%22160%22%20height%3D%2280%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%2080%20L80%2016%20L160%2080Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E" alt="Green hill" width="160" height="80"></picture>At ordinary viewport widths the fallback image is selected.
Practice and next steps
Sketch a narrow and wide composition for the same subject and write a shared alternative.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Picture Element and Art Direction — Knowledge Check
Check the core concepts from this lesson.
Which element owns the alternative text in picture?
Which attribute supplies candidates on a source inside picture?
When is art direction useful?
A narrow-screen layout needs a square crop while desktop uses a wide crop. Which mechanism fits?
What happens when no picture source is suitable?