<picture & Responsive Images
<picture> & Responsive Images
Level 3 — Media & Embedding An HTML5 element and set of techniques used to serve different optimized image files to users based on their screen width, resolution, or format support.
1. Prerequisites
<img>— The image container that actually displays the pixels.altAttribute — The accessibility description.<source>Element — The tag used to list alternative media files.
2. Term Category
Media Element (Modern Browsers .): <picture> & Responsive Images is a fundamental concept in this technology stack. Level 3 — Media & Embedding
3. Explanation
(1) Design Motivation — "Why did we design this?"
Users visit websites on everything from tiny watches and mobile phones to massive 4K desktop monitors.
If you load a huge, high-resolution desktop image (e.g. 3000px wide, 2MB file size) on a mobile phone over a slow cellular connection, the page will load slowly, costing the user time and cellular data. However, if you serve a tiny mobile image (e.g. 400px wide) on a desktop monitor, it will look blurry and stretched.
To solve this, HTML5 introduced Responsive Images. The most powerful way to implement this is the <picture> tag. It allows developers to specify different image files for different screen sizes (known as Art Direction or resolution switching).
(2) How the <picture> Element Works
The <picture> tag is a wrapper. It does not render anything on its own. Instead, it coordinates nested <source> tags and a mandatory fallback <img> element:
mediaattribute: Specifies a CSS Media Query (e.g.(min-width: 800px)) defining when the browser should choose this image.srcsetattribute: Identifies the file path to load for that media query.- Fallback
<img>: The final element in the block. This is mandatory. The browser evaluates the sources, picks the best one, and injects its pixels directly into the fallback<img>tag. If a browser is too ancient to understand<picture>, it ignores the picture/source tags and just renders the normal<img>.
(3) Code Examples
Short Snippet
Serving a desktop image versus a mobile image:
<picture>
<!-- If screen is at least 768px wide, load this: -->
<source media="(min-width: 768px)" srcset="desktop.jpg">
<!-- Otherwise, fall back to rendering this: -->
<img src="mobile.jpg" alt="A developer working at a desk">
</picture>
Fuller Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Responsive Art Gallery</title>
</head>
<body>
<h1>Art Gallery</h1>
<!-- picture element demonstrating Art Direction -->
<!-- We serve a cropped, close-up image for mobile, and wide-angle for desktop -->
<picture>
<!-- Desktop (Screens 1024px and wider) -->
<source media="(min-width: 1024px)" srcset="images/gallery-wide.jpg">
<!-- Tablet (Screens 768px to 1023px) -->
<source media="(min-width: 768px)" srcset="images/gallery-medium.jpg">
<!-- Mobile & Fallback (Mandatory img tag holds the alt text!) -->
<img src="images/gallery-cropped.jpg" alt="Exhibition hall containing oil paintings on white walls" width="100%">
</picture>
</body>
</html>
4. Common Mistakes & Pitfalls
Mistake 1: Forgetting the fallback <img> tag inside <picture>
The mistake: Leaving out the <img> tag and only writing <source> elements:
<!-- BAD: Nothing will render on the screen! -->
<picture>
<source media="(min-width: 800px)" srcset="large.png">
<source srcset="small.png">
</picture>
Why it's wrong: The <picture> element is just a logical container. The browser relies on the internal <img> tag to physically draw the pixels and apply the alt description. Without <img>, the page remains blank.
Mistake 2: Omitting Fallback <img> Tag Inside <picture> Container
The mistake: Writing <picture><source srcset="a.webp"></picture> without an <img> tag inside.
Why it's wrong: The <picture> element is a wrapper wrapper mechanism for asset selection. The <img> tag INSIDE <picture> actually performs DOM rendering on screen! Without <img>, nothing renders.
Incorrect:
<picture>
<source srcset="hero.avif">
<!-- ❌ Missing fallback <img> tag! Nothing renders on screen! -->
</picture>
Fix:
<picture>
<source srcset="hero.avif" type="image/avif">
<img src="hero.jpg" alt="Hero banner">
</picture>
Mistake 3: Listing <source> Elements in Wrong Media Query Order
The mistake: Listing smaller media query <source media="(min-width: 500px)"> BEFORE larger query <source media="(min-width: 1200px)">.
Why it's wrong: Browsers evaluate <source> tags top-to-bottom and select the FIRST matching source. Placing min-width: 500px first causes desktop screens to select 500px mobile images.
Incorrect:
<source media="(min-width: 500px)" srcset="small.jpg"> <!-- ❌ Evaluated first! -->
<source media="(min-width: 1200px)" srcset="large.jpg">
Fix:
<source media="(min-width: 1200px)" srcset="large.jpg">
<source media="(min-width: 500px)" srcset="small.jpg">
5. Practice Exercises
Exercise 1: Art Direction for Mobile vs Desktop Layouts
Scenario: A designer uses <picture> and <source media="..."> to serve a cropped vertical image on mobile devices and a wide landscape image on desktops.
Requirements:
- Wrap
<source>elements and<img>inside<picture>. - Use
<source media="(max-width: 767px)">for mobile. - Use
<img>as the ultimate fallback.
Answer
Implementation
<picture class="hero-media">
<!-- Mobile Layout: Vertical Cropped Image -->
<source media="(max-width: 767px)" srcset="images/hero-mobile-crop.jpg">
<!-- Desktop Layout: Wide Landscape Image -->
<source media="(min-width: 768px)" srcset="images/hero-desktop-wide.jpg">
<!-- Ultimate Fallback Element (Mandatory!) -->
<img src="images/hero-desktop-wide.jpg" alt="Architectural rendering of the new eco-friendly corporate headquarters" width="1200" height="500">
</picture>
Technical Explanation
- Art Direction with
<picture>: The<picture>wrapper allows switching completely different image crops or aspect ratios based on CSS media queries. - The
<source media="...">Element: Evaluates media queries top-to-bottom; the first matching<source>is selected by the browser. - Mandatory
<img>Fallback: The<picture>element is a wrapper; the actual image is ALWAYS rendered via the inner<img>tag, which holdsaltandloadingattributes.
Exercise 2: Modern Image Format Fallback (AVIF -> WebP -> JPEG)
Scenario: Delivers next-gen AVIF and WebP image formats with fallback to standard JPEG for older browsers.
Requirements:
- Use
<source type="image/avif">. - Use
<source type="image/webp">. - Fallback to
<img>JPEG.
Answer
Implementation
<picture>
<!-- Next-Gen Format 1: AVIF (Highest Compression) -->
<source type="image/avif" srcset="images/photo.avif">
<!-- Next-Gen Format 2: WebP (Widespread Support) -->
<source type="image/webp" srcset="images/photo.webp">
<!-- Fallback: Standard JPEG -->
<img src="images/photo.jpg" alt="Sunset over mountain ridge" width="800" height="600" loading="lazy">
</picture>
Technical Explanation
- Format Content Negotiation: Browsers test
<source type="...">MIME types and download the first supported modern format (e.g. AVIF), ignoring unsupported ones. - Bandwidth Reduction: AVIF and WebP formats can reduce image file sizes by 30% to 50% compared to legacy JPEGs.
- Single
altSource of Truth: Alt text is declared once on the nested<img>tag, serving all formats seamlessly.
Exercise 3: High-DPI Retina Display Image Source Switching
Scenario: Serves 1x, 2x, and 3x resolution images for high-density smartphone screens.
Requirements:
- Use
srcsetpixel density descriptors (1x,2x).
Answer
Implementation
<picture>
<source media="(min-width: 800px)" srcset="images/logo-1x.png 1x, images/logo-2x.png 2x">
<img src="images/logo-1x.png" alt="Company Logo" width="200" height="50">
</picture>
Technical Explanation
- Pixel Density Descriptors (
2x): Instructs browser to fetch crisp2xresolution assets on Apple Retina or high-DPI screens. - Bandwidth Optimization: Standard 1x screens don't waste data downloading oversized 2x image assets.
- Seamless Display Scaling: Maintains sharp vector/raster presentation across monitor types.
6. Related Terms
<img>— The rendering image element.<source>Element — The sibling element specifying source lists.altAttribute — The required visual descriptor.
7. Key Takeaways
<picture>is an HTML5 wrapper tag used to serve responsive images.- It matches image assets to the user's screen width using the
mediaquery attribute. - The
srcsetattribute on<source>defines the path to load. - You must include a fallback
<img>tag at the end of<picture>, which actually renders the image. - Place the
altattribute strictly on the fallback<img>element, not on the<picture>tag.