The <picture> element lets you specify entirely different image sources for different conditions — not just different sizes of the same image, but genuinely different files, crops, or formats.
Syntax
<picture>
<source srcset="image.webp" type="image/webp">
<source srcset="image.jpg" type="image/jpeg">
<img src="image.jpg" alt="...">
</picture>
Basic Example
<picture>
<source srcset="/assets/images/banner.webp" type="image/webp">
<img src="/assets/images/banner.jpg" alt="CodingNow course banner">
</picture>
Browser output: a browser that supports the modern WebP format loads the smaller .webp file; a browser that doesn't falls back automatically to the .jpg — visually identical either way.
How It Works
<picture>wraps one or more<source>elements followed by a required, final<img>.- The browser evaluates each
<source>in order and uses the first one whosetype(format) ormedia(viewport condition) it supports/matches. - The trailing
<img>is both the fallback if nothing matches, and the element that actually carries thealttext —<source>elements never have their ownalt.
Important Attributes
| Attribute | On | Purpose |
|---|---|---|
srcset | <source> | The candidate image file(s) for this source |
type | <source> | The image format's MIME type, e.g. image/webp — used for format-based fallback |
media | <source> | A CSS media condition, e.g. (max-width: 600px) — used for art direction |
alt | <img> | Still required, and only ever placed on the final fallback <img> |
Real-World Example
<picture>
<source media="(max-width: 600px)" srcset="hero-mobile-tight-crop.jpg">
<source media="(min-width: 601px)" srcset="hero-desktop-wide-crop.jpg">
<img src="hero-desktop-wide-crop.jpg" alt="Students at the CodingNow campus">
</picture>
This is art direction: mobile visitors see a tightly cropped, subject-focused version, while desktop visitors see the full wide shot — a genuinely different image, not just a resized one. This is the key difference from srcset alone.
Comparison: srcset vs picture
srcset (on <img>) | <picture> | |
|---|---|---|
| Solves | Same image, different resolutions | Different images, formats, or crops per condition |
| Use case | Performance — smaller files for smaller screens | Art direction and modern-format fallback (e.g. WebP → JPEG) |
Common Mistakes
- Forgetting the required fallback
<img>at the end —<picture>doesn't work without one - Putting
alttext on a<source>element instead of the<img>—<source>doesn't support it
Best Practices
- Use
<picture>specifically for format fallback (serve WebP/AVIF where supported, JPEG otherwise) or genuine art direction — for simple resolution switching alone, plainsrcseton<img>is usually simpler - Always include a working, correctly-alt-texted fallback
<img>
Accessibility Considerations
Exactly the same as any image — the fallback <img>'s alt text is what screen readers announce, regardless of which <source> was actually selected.
SEO Considerations
Serving modern, smaller formats like WebP where supported meaningfully improves page-speed metrics without sacrificing compatibility — a real win search engines reward indirectly through Core Web Vitals.
Browser & Modern HTML Notes
<picture> is well-supported in all modern browsers and is the standard, recommended way to serve next-gen image formats with a safe fallback.
Mini Practice
- Build a
<picture>element serving a WebP image with a JPEG fallback. - Build a second
<picture>that shows a different crop on mobile vs desktop usingmedia.