Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811320
Back to HTML Notes
Topic #409

The picture Element

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

  1. <picture> wraps one or more <source> elements followed by a required, final <img>.
  2. The browser evaluates each <source> in order and uses the first one whose type (format) or media (viewport condition) it supports/matches.
  3. The trailing <img> is both the fallback if nothing matches, and the element that actually carries the alt text — <source> elements never have their own alt.

Important Attributes

AttributeOnPurpose
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>
SolvesSame image, different resolutionsDifferent images, formats, or crops per condition
Use casePerformance — smaller files for smaller screensArt 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 alt text 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, plain srcset on <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

  1. Build a <picture> element serving a WebP image with a JPEG fallback.
  2. Build a second <picture> that shows a different crop on mobile vs desktop using media.

Related HTML Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
Talk to Advisor Online now
1

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →