Web Development

Picture Tag: HTML Guide for Responsive Images & SEO

Use the picture tag for responsive images and art direction. This wiki guide explains how to serve modern formats like WebP and AVIF with legacy fallbacks.

1.3k
picture tag
Monthly Search Volume
Keyword Research

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 srcset attribute 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

  1. The browser encounters the <picture> element and checks its child <source> elements in the order they appear.
  2. For each <source>, the browser evaluates:
    • The media attribute (does the screen match this media query?)
    • The type attribute (does the browser support this MIME type?)
    • The srcset attribute (which image file from the list should it use?)
  3. The browser selects the first <source> where all conditions match.
  4. It loads the specified image resource into the src attribute of the nested <img> element.
  5. If no <source> elements match, the browser displays the image defined in the <img> element's own src attribute.

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 type attribute for format fallbacks. Specify MIME types like image/avif and image/webp to let browsers skip unsupported formats immediately without attempting to download them.
  • Add width and height attributes to <source> elements when using different aspect ratios. This prevents cumulative layout shift while the browser calculates the image dimensions.
  • Write one alt text that covers all variants. You cannot change the alt attribute 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, use srcset on 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 alt text per source. The <img> element can only have one alt attribute. 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: Use srcset and sizes attributes 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 and x (e.g., 2x) for pixel density descriptors, but never mix them in the same srcset.

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.

Start Your SEO Research in Seconds

5 free searches/day • No credit card needed • Access all features