The <picture> tag is an HTML container that serves different image files based on the user's device, browser, or screen conditions. It wraps around one or more <source> elements and a required <img> fallback. For marketers and SEO practitioners, this means faster load times, access to modern image formats like AVIF and WebP, and precise control over how visuals display across devices without breaking older browsers.
What is Picture Tag?
The <picture> element gives web developers flexibility in specifying image resources. It contains zero or more <source> elements followed by one <img> element. The browser examines each <source> tag's media, type, and srcset attributes to find the first match that fits the current viewport width and device capabilities. The <img> element serves as the mandatory fallback and provides the actual image container that displays in the document.
The element works similarly to <video> and <audio>. You set up different sources, and the first source that fits the preferences is the one being used. Common use cases include art direction (cropping or modifying images for different displays), offering alternative image formats for cases where certain formats are not supported, and saving bandwidth by loading the most appropriate image for the viewer's display.
Why Picture Tag matters
- Faster page loads. Serve smaller, optimized files to mobile devices and larger ones to desktop screens. This reduces transfer sizes and improves load times, which supports Core Web Vitals.
- Modern format deployment. Use next-generation formats like AVIF and WebP while maintaining JPEG fallbacks for older browsers. You avoid breaking compatibility while gaining compression advantages.
- Art direction control. Show completely different image crops or compositions for mobile versus desktop. Instead of scaling one image down, you can serve a tall portrait crop for phones and a wide landscape for monitors.
- Command over suggestion. Where the
srcsetattribute gives suggestions to the browser, the<picture>element gives commands (web.dev). This ensures the browser follows your exact instructions for format and layout changes.
How Picture Tag works
- The browser encounters the
<picture>element and checks its child<source>elements in the order they appear. - For each
<source>, the browser evaluates:- The
mediaattribute (does the screen match this media query?) - The
typeattribute (does the browser support this MIME type?) - The
srcsetattribute (which image file from the list should it use?)
- The
- The browser selects the first
<source>where all conditions match. - It loads the specified image resource into the
srcattribute of the nested<img>element. - If no
<source>elements match, the browser displays the image defined in the<img>element's ownsrcattribute.
Picture Tag vs Srcset
Choose the right tool based on your responsive image needs:
| Feature | Picture Element | Srcset Attribute |
|---|---|---|
| Primary use | Art direction, format switching, theme adaptation | Resolution and density switching |
| Browser behavior | Commands (browser must follow the first match) | Suggestions (browser chooses based on preferences and network) |
| Syntax | Container with <source> children wrapping <img> |
Attribute on the <img> element |
| Best for | Different crops, file formats, or aspect ratios | Same image at different sizes or pixel densities |
Use srcset when you only need to serve differently sized versions of the same image. Reserve <picture> for when you need fine-grained control over which file loads, such as when switching between formats or displaying completely different compositions at different breakpoints.
Best practices
- Always include the
<img>element as the last child. The picture element will not work without it, and it serves as your fallback for unsupported browsers. Both the starting and ending tags are mandatory. - Order
<source>elements by priority. The browser executes the first source it can use, not the best match. Place your preferred or most specific conditions first. - Use the
typeattribute for format fallbacks. Specify MIME types likeimage/avifandimage/webpto let browsers skip unsupported formats immediately without attempting to download them. - Add
widthandheightattributes to<source>elements when using different aspect ratios. This prevents cumulative layout shift while the browser calculates the image dimensions. - Write one
alttext that covers all variants. You cannot change thealtattribute based on which source loads. Describe the image content in a way that works for both the wide desktop version and the cropped mobile version. - Skip
<picture>for simple retina displays. If providing higher-density versions of an image for high-DPI (Retina) displays, usesrcseton the<img>element instead. This lets browsers opt for lower-density versions in data-saving modes without explicit media conditions.
Common mistakes
- Mistake: Omitting the
<img>element. The picture element requires one<img>element as its last child to function. Fix: Always end your picture block with an<img>tag containing a fallback image and alt text. - Mistake: Putting sources in the wrong order. If you list a generic source before a specific one, the browser will match the generic one first and never reach your mobile-specific image. Fix: Order sources from most specific condition to least specific, or structure your media queries carefully.
- Mistake: Trying to change
alttext per source. The<img>element can only have onealtattribute. Fix: Write alt text that accurately describes the image content across all variants (e.g., "Company team meeting" works for both the wide group shot and the cropped headshot). - Mistake: Using
<picture>for simple file size variations. Fix: Usesrcsetandsizesattributes on the<img>element when you only need different resolutions of the same image. Reserve<picture>for art direction or format switching to avoid unnecessary HTML weight. - Mistake: Confusing width descriptors with pixel density descriptors. Fix: Use
w(e.g.,480w) for width descriptors andx(e.g.,2x) for pixel density descriptors, but never mix them in the samesrcset.
Examples
Serving modern formats with fallbacks:
<picture>
<source srcset="image.avif" type="image/avif">
<source srcset="image.webp" type="image/webp">
<img src="image.jpg" alt="Product photo" width="300" height="200">
</picture>
The browser checks for AVIF support first, falls back to WebP if AVIF is unsupported, and finally uses JPEG if neither modern format works.
Art direction for different screens:
<picture>
<source srcset="wide.jpg" media="(min-width: 75em)" width="1200" height="500">
<source srcset="regular.jpg" media="(min-width: 50em)" width="800" height="400">
<img src="square.jpg" alt="Team photo" width="400" height="400">
</picture>
Desktop users see a wide banner image, tablet users see a standard rectangle, and mobile users see a square crop. The different aspect ratios suit each context better than scaling alone would allow.
Dark mode support:
<picture>
<source srcset="logo-dark.png" media="(prefers-color-scheme: dark)">
<source srcset="logo-light.png" media="(prefers-color-scheme: light)">
<img src="logo-light.png" alt="Company logo">
</picture>
FAQ
What's the difference between the <picture> tag and the <img> tag?
The <img> tag displays a single image file. The <picture> tag is a container that wraps around multiple <source> elements and one <img> element, allowing you to offer different image files based on device conditions. The <img> tag inside <picture> serves as the fallback and the actual display element; without it, the picture element will not render.
When should I use <picture> instead of srcset?
Use <picture> when you need art direction (different crops or compositions), format fallbacks (AVIF to WebP to JPEG), or theme switching (dark/light mode). Use srcset when you only need the same image at different resolutions or sizes. Srcset gives the browser optimization flexibility; picture gives explicit control.
Does using <picture> improve Core Web Vitals?
It can support better performance metrics. By serving smaller file sizes to mobile devices and modern compressed formats where supported, you reduce file transfer sizes and potentially improve Largest Contentful Paint. However, the benefit depends on proper implementation and image optimization.
What browsers support the <picture> element?
Support is universal in modern browsers. Specific version benchmarks indicate support began with Chrome 38.0, Internet Explorer 13.0, Firefox 38.0, Safari 9.1, and Opera 25.0.
Can I use different aspect ratios with <picture>?
Yes. Unlike srcset, which scales the same image, <picture> lets you display completely different image files with different width and height ratios. Add width and height attributes to each source to prevent layout shift while the browser loads.
How do I prevent layout shift when using the picture element?
Add explicit width and height attributes to your <img> element and to each <source> element when they have different aspect ratios. You can also use the object-position and object-fit CSS properties on the child <img> element to control how the image fits within its frame.