🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Responsive Images with the picture Element

Web FoundationsMediađŸŸĸ Free Lesson

The picture Element and Responsive Image Selection

The image element answers one question: which file should I display here. The picture element wraps it and asks a better one: given this browser's format support, this viewport, and this design, which file should I display here. The difference is that a single URL cannot express a change of crop, and a page that ships a desktop photograph to a phone pays for the pixels twice, once in bytes and once in layout time.

Picture is not a new image tag. It is a container holding zero or more source elements plus the required image element, and the browser walks the sources in order, applying each one's conditions, until something matches. If nothing matches, the image's own src wins, which is why the fallback inside the tag must be a real, complete image rather than a placeholder.

The three conditions compose independently, and each solves a problem that srcset cannot:

  • type declares the media type of the candidate, so an engine that cannot decode AVIF skips that source entirely without fetching a byte
  • media takes a media query, so the crop and the file can change at a breakpoint
  • srcset carries the candidate URLs plus width or density descriptors for selection within that source

💡Order sources from most specific to least

The browser uses the first source whose conditions it understands, not the best one it supports. Modern formats go first, the widest art-directed crop before the narrower ones, and the image element holds the last-resort file.

How the Browser Picks a File

Selection happens in two stages, and separating them explains most performance questions. First the conditions are evaluated: format support for type, media query truth for media. The first source that survives wins outright. Only then does the second stage run, inside that source, where srcset and sizes decide which candidate URL to fetch.

AttributeLives onPurposeIgnored when
typesourceSkip formats the engine cannot decodeAbsent, so any format is accepted
mediasourceChoose by viewport or feature queryAbsent, so it always matches
srcsetsource or imageList candidates with width or density hintsNothing else, it is always read
sizesimageSay how wide the image will renderOnly density descriptors are used
srcimageThe fallback and the resolution targetA source matched, so it is not consulted
altimageThe accessible descriptionNever; it is required on every image

The sizes attribute is the piece people omit, and it matters because the browser must estimate the rendered width before it can choose a candidate. Writing sizes="100vw" tells it the image spans the viewport, which pushes selection toward the largest file; writing a realistic value such as (min-width: 900px) 640px, 100vw lets it choose a much smaller candidate on a wide screen with a narrow column.

Density Versus Width Descriptors

Inside a srcset each candidate carries either a density hint or a width hint, and the two are not interchangeable. A density descriptor such as 2x says this file is meant for a display with twice the device pixels, and it implies that the image renders at one fixed CSS size; the browser simply multiplies. A width descriptor such as 1600w says this file is sixteen hundred physical pixels wide, and it requires the sizes attribute so the browser can work out how many pixels it actually needs before choosing.

<!-- Fixed-size icon: density is simpler and needs no sizes. -->
<img src="logo.png" srcset="logo.png 1x, logo@2x.png 2x"
     alt="Example Product" width="120" height="32">

<!-- Fluid hero: width descriptors plus an honest sizes value. -->
<img src="hero-800.jpg"
     srcset="hero-800.jpg 800w, hero-1600.jpg 1600w, hero-2400.jpg 2400w"
     sizes="(min-width: 1000px) 720px, 100vw"
     alt="A craftsperson soldering a circuit board"
     width="1600" height="900">

Mixing the two forms in one srcset is an error the parser reports as a broken candidate list, and the whole set is then discarded, leaving the src to render a single file. The practical rule is short: use density for elements whose rendered size never changes, such as logos, avatars, and toolbar icons, and use width descriptors for anything that participates in a fluid layout.

âš ī¸An optimistic sizes value is a bandwidth leak

Writing sizes="100vw" when the image renders in a 720 pixel column tells the browser to fetch a file sized for the full viewport. On a phone that means downloading roughly three times the pixels the layout will ever show.

Format Negotiation with Fallbacks

The first job of picture is progressive enhancement across decoders. Serving AVIF to engines that understand it and a JPEG to those that do not saves a large share of bytes on the same markup, with no scripting and no user-visible difference beyond sharpness.

<picture>
  <source srcset="hero.avif" type="image/avif">
  <source srcset="hero.webp" type="image/webp">
  <img
    src="hero.jpg"
    alt="The workshop bench with tools laid out in morning light"
    width="1200"
    height="800"
    loading="lazy"
    decoding="async">
</picture>

The loading="lazy" attribute on the image applies whichever source is eventually chosen, so the off-screen deferral is preserved through format negotiation. One caveat for above-the-fold heroes: lazy loading the first image on the page delays its discovery until layout, which typically costs more than it saves. Keep eager loading for anything in the first viewport and lazy for the rest.

Encoding settings matter as much as the format. AVIF at a moderate quality usually beats WebP at a visually equivalent quality, but encode times are far longer, so a build step that converts on every deploy needs caching keyed by source hash. A practical ladder is one AVIF, one WebP, and the original JPEG for the fallback, each with a target dimension rather than a device-pixel guess.

Art Direction: A Different Crop Per Breakpoint

Format negotiation changes bytes, not composition. A portrait hero that reads beautifully on a phone becomes a thin band on a desktop, and the only fix is a different file with a different crop. The media condition on each source is where that decision lives.

<picture>
  <source
    media="(min-width: 1200px)"
    srcset="hero-wide-1600.jpg 1600w, hero-wide-2400.jpg 2400w"
    sizes="100vw">
  <source
    media="(min-width: 700px)"
    srcset="hero-medium-1200.jpg 1200w"
    sizes="100vw">
  <img
    src="hero-tall-800.jpg"
    alt="A craftsperson soldering a circuit board, seen from above"
    width="800"
    height="1000"
    fetchpriority="high">
</picture>

Two decisions are visible here. The wide source carries a srcset with width descriptors so the browser can pick between 1600 and 2400 physical pixels for a high-density display, while the narrow sources carry a single file because the composition is already tight. And fetchpriority="high" replaces lazy loading for this element, telling the network stack that this image is on the critical path for the largest contentful paint.

The cost of art direction is a set of exports to maintain. Keep them in one directory with a naming convention that encodes the crop, generate them from the source file in the build, and let the markup reference names rather than dimensions so a resolution change never touches HTML.

Width, Height, and the Layout Shift You Can Avoid

Even a perfectly chosen image can wreck the page if the browser does not know how tall it will be before the bytes arrive. Without intrinsic dimensions, the element occupies zero height until decoded, then expands, pushing every paragraph below it down the screen. That shift is measured directly in the layout stability metric that search engines report.

Declaring width and height on the image tells the browser the aspect ratio in advance. Combined with a stylesheet that sets width to full and height to auto, the reserved space matches the final rendered box exactly, regardless of which source was selected.

img {
  max-width: 100%;
  height: auto;
  display: block;
}

.hero {
  aspect-ratio: 16 / 9;
  overflow: hidden;
}

.hero img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: 30% 40%;
}

The object-fit: cover rule in the second block is what makes art direction forgiving. When the container's ratio differs slightly from the file's, cover fills the box and crops the overflow rather than letterboxing, and object-position decides which edge gives way. Used together with a declared aspect ratio on the container, the layout is stable before a single byte of image data has been downloaded.

Choosing Files for Each Slot

A responsive set is a design decision before it is a performance one. Start from the rendered widths the layout actually produces: a column that is never wider than 640 pixels does not need a 1600 pixel candidate, and providing one only invites the browser to download it on a high-density screen where the difference is invisible.

The next decision is how many densities to offer. For photographic content, two candidates at one times and two times cover almost every real device, because three times and four times screens are rare at the sizes where the difference shows. For line art and screenshots, the higher density matters more and a third candidate is justified. Every additional candidate costs a build step and a cache entry, so add one only when a measurement says the quality gap is visible.

Finally, audit the result rather than trusting the markup. The network panel's image column shows the intrinsic size and the transferred size for each request, and a page that always downloads the largest candidate is telling you that sizes is wrong or missing.

Common Mistakes

Using media queries to swap the whole image while ignoring type ordering

Fix: Put format sources first inside each breakpoint so AVIF and WebP are negotiated before the crop is chosen.

Omitting sizes on a srcset with width descriptors

Fix: Declare the rendered width the image will occupy; without it the browser assumes full viewport width and over-fetches.

Leaving width and height off a lazy image

Fix: Always declare both so the browser reserves the aspect ratio and the page does not shift as images decode.

Lazy loading the largest image in the first viewport

Fix: Use eager loading or fetchpriority high for above-the-fold media; lazy loading there only delays the paint.

Key Takeaways

  • picture wraps an image and lets sources filter by format and media query; the image inside remains the fallback and carries the alt text.
  • Selection is two-stage: conditions pick a source, then srcset and sizes pick a file within it.
  • Use type for format negotiation and media for art direction; they solve different problems.
  • Declare width and height, or set an aspect ratio on the container, to reserve space before bytes arrive.
  • Audit which candidate the browser actually downloads, because a missing or inaccurate sizes attribute silently selects the largest file.
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-10-12

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Web Development Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement