Picture Responsive Srcset Art Direction

Serve responsive images with art direction using the picture element and multiple source tags with media queries.

HtmlFile Upload

Code

htmlsnippet
15 lines
<picture>
  <source
    media="(max-width: 768px)"
    srcset="mobile.webp 480w, mobile-hd.webp 960w"
    sizes="100vw"
    type="image/webp"
  />
  <source
    media="(min-width: 769px)"
    srcset="desktop.webp 1280w, desktop-hd.webp 2560w"
    sizes="(max-width: 1024px) 100vw, 50vw"
    type="image/webp"
  />
  <img src="fallback.jpg" alt="Hero image" loading="lazy" />
</picture>

What the code does

<picture> lets the browser pick the best source from a candidate list instead of hard-coding one file.

srcset width descriptors let the browser choose a candidate matching the layout slot and pixel density.

loading="lazy" defers offscreen image fetches until they approach the viewport.

Images opt into loading="lazy" so offscreen bytes are not fetched during initial render.

Statements that matter

<img src="fallback.jpg" alt="Hero image" loading="lazy" />

Interactive Tools for This Topic

FAQ

Which non-generic elements does the markup use?
`<picture>`, `<source>`, `<img>` — each carries native semantics (or a browser behavior) that generic containers do not.