On this page
  1. Media & Responsive Images
  2. Contents
  3. Resolution Switching (srcset & sizes)
  4. Format Fallback & Art Direction (<picture>)

Media & Responsive Images

Prev: Accessible Forms & Validation | Up: tutorials | Next: Native Interactive Patterns
Rel: Tables & Media, Box Model & Sizing, Reflow, Repaint & Performance
Ref: MDN Web Docs — Responsive Images
Tags: #public #web #html #tutorials #media


Contents


Resolution Switching (srcset & sizes)

Provide width descriptors (w) in srcset and tell the browser the rendered layout width in sizes so it downloads only the smallest sharp asset needed.

<img
  src="hero-800.jpg"
  srcset="hero-400.jpg 400w, hero-800.jpg 800w, hero-1600.jpg 1600w"
  sizes="(max-width: 600px) 100vw, 50vw"
  alt="Mountain sunrise"
  width="1600"
  height="900"
  decoding="async"
/>

Format Fallback & Art Direction (<picture>)

<picture> lets you serve modern formats (AVIF/WebP) or alternate crops per media query while keeping <img> as the fallback and accessibility anchor.

<picture>
  <source type="image/avif" srcset="photo.avif" />
  <source type="image/webp" srcset="photo.webp" />
  <img src="photo.jpg" alt="Product showcase" width="800" height="600" loading="lazy" />
</picture>

Linked from

Related notes