<picture> Tag in HTML

Quick facts

CategoryFlow content, phrasing content, embedded content, palpable content
Content modelZero or more <source> elements, followed by exactly one <img>
Tag omissionNeither the start nor the end tag can be omitted
Permitted parentsAny element that accepts embedded content
DOM interfaceHTMLPictureElement

Syntax

<picture> <source srcset="photo.avif" type="image/avif"> <source srcset="photo.webp" type="image/webp"> <img src="photo.jpg" alt="A description of the photo"> </picture>

Live example

Edit the code below and watch the preview update instantly.

index.html
Preview

Attributes

<picture>

AttributeDescription
—Only the global attributes apply.

<source> (inside <picture>)

AttributeDescription
srcsetOne or more image URLs, optionally with widths (800w) or densities (2x)
mediaA media query, e.g. (min-width: 700px); the source is used only when it matches
typeThe image format, e.g. image/webp; skipped if the browser can’t show it
sizesHow wide the image will be displayed, so the browser can pick from srcset widths
width / heightThe image size for this source, to prevent layout shift

Common examples

Modern formats with a fallback

The browser picks the first format it supports — AVIF, then WebP, then the JPEG in <img>.

<picture> <source srcset="photo.avif" type="image/avif"> <source srcset="photo.webp" type="image/webp"> <img src="photo.jpg" alt="A red bicycle leaning on a wall" width="800" height="600"> </picture>

A different crop for phones

Show a tall, close-up crop on small screens and a wide one on desktops (“art direction”).

<picture> <source media="(max-width: 600px)" srcset="hero-square.jpg"> <img src="hero-wide.jpg" alt="Hikers on a mountain ridge at sunrise" width="1600" height="600"> </picture>

A logo for dark mode

A media query on the source swaps the image when the visitor uses dark mode.

<picture> <source srcset="logo-light.svg" media="(prefers-color-scheme: dark)"> <img src="logo-dark.svg" alt="Example Inc." width="160" height="40"> </picture>

AVIF vs WebP vs JPEG

The main reason to use <picture> is to serve smaller, modern image formats. AVIF usually gives the smallest files at the same quality, WebP is a little bigger but very widely supported, and JPEG works absolutely everywhere.

List sources from best to most compatible: AVIF first, then WebP, with JPEG (or PNG for graphics with transparency) in the <img> as the fallback. Smaller images make pages load faster, which helps both visitors and search rankings.

AVIFWebPJPEG
File sizeSmallestSmallLargest
TransparencyYesYesNo
SupportAll modern browsersAll modern browsersEverywhere

Common mistakes to avoid

  • Forgetting the <img>. Without it nothing is displayed — the img is required and must come last.
  • Wrong type values. A typo such as image/web makes the browser skip that source.
  • Sources in the wrong order. The browser uses the first match, so put the most specific or newest format first.
  • Styling the <picture> instead of the img. Width, height and object-fit belong on the <img>.

Accessibility & SEO tips

  • Always set width and height on the img to prevent the page jumping as images load (Cumulative Layout Shift).
  • Add loading="lazy" to images further down the page, but not to the main image at the top.
  • Google Images indexes the image in the <img> tag, so give that file a descriptive name and good alt text.

Browser support

Supported in all modern browsers, including Chrome, Edge, Firefox, Safari and their mobile versions. It was introduced with HTML5, and only very old browsers such as Internet Explorer 8 needed workarounds. Browsers that don’t know a format simply skip that source.

Related tags

Frequently asked questions

When should I use <picture> instead of srcset on <img>?
Use srcset on <img> when you only need the same image at different resolutions. Use <picture> when the image itself should change, such as a different crop on phones, or when you offer new formats like AVIF and WebP with a JPEG fallback.
Where does the alt text go?
On the <img> inside the <picture>. The <source> elements never get alt text; whichever image is chosen uses the <img>'s alt.
Does <picture> help page speed and SEO?
Yes. Serving smaller files to phones and modern formats like WebP or AVIF makes pages load faster, which improves Core Web Vitals, a Google ranking factor.
How do I convert images to WebP or AVIF?
Free tools such as Squoosh (squoosh.app) convert images in your browser. On WordPress, image-optimization plugins can create WebP and AVIF copies automatically.
Can I use lazy loading with <picture>?
Yes. Put loading="lazy" on the <img> inside the picture; it applies to whichever source the browser chooses.
Does <picture> work with SVG images?
Yes. Use type="image/svg+xml" on the source, and a PNG fallback in the <img> if you need to support very old browsers.