Picture Responsive Srcset Art Direction
Serve responsive images with art direction using the picture element and multiple source tags with media queries.
Code
<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
Aspect Ratio CalculatorSimplify a width-to-height ratio, show its decimal value, and derive a matching height for a new width.CSS clamp() GeneratorGenerate responsive clamp(MIN, PREFERRED, MAX) values with automatic calc() wrapping for expressions.Viewport Unit CalculatorConvert between pixels and the CSS viewport units vw, vh, vmin, and vmax for a given viewport size, in either direction.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.