Sub topic 3
Art Direction with `media`
Assume basic HTML, CSS media queries, responsive layouts, and the difference between CSS pixels and device pixels. This chapter focuses on choosing different image crops, formats, and resolutions for different conditions.
Art direction with `media`
Art direction means serving different image compositions, not merely different file sizes. A wide crop may work for a desktop banner, while a square crop keeps the subject visible on a phone.
picture evaluates its source elements in order. The browser uses the first source whose media condition matches. The nested img is the fallback and supplies the alternative when no source matches.
<picture>
<source media="(min-width: 50em)" srcset="hero-wide.jpg">
<img
src="hero-square.jpg"
width="800"
height="800"
alt="A sailor looking out to sea">
</picture>The media condition controls the crop. It does not describe the image's pixel density.
Putting `picture`, `srcset`, and `sizes` together
Use picture to choose a crop or format, then use srcset and sizes to choose an appropriate width within that choice.
<picture>
<source
media="(min-width: 50em)"
type="image/webp"
srcset="hero-wide-800.webp 800w, hero-wide-1600.webp 1600w"
sizes="100vw">
<source
type="image/webp"
srcset="hero-square-400.webp 400w, hero-square-800.webp 800w"
sizes="100vw">
<img
src="hero-square-800.jpg"
srcset="hero-square-400.jpg 400w, hero-square-800.jpg 800w"
sizes="100vw"
width="800"
height="800"
alt="A sailor looking out to sea">
</picture>Each selected source has its own candidate list. sizes tells the browser how wide the rendered image will be, allowing it to account for viewport width and device pixel ratio.
Generating image widths
Create variants that cover the display widths your layout actually uses. ImageMagick 7 can resize a source while preserving its aspect ratio:
for w in 400 800 1600; do
magick photo.jpg -resize "${w}x" "photo-${w}.jpg"
doneUse an image pipeline or build process for many images. Avoid generating dozens of nearly identical widths without evidence that they improve loading.
Lazy loading and above-the-fold images
Use loading="lazy" for images below the initial viewport:
<img
src="gallery-800.jpg"
width="1600"
height="1067"
loading="lazy"
alt="Boats moored in a harbor">Do not lazy-load the main image visible on first load. It can delay the page's primary content.
Checking which image was chosen
Inspect the image in developer tools and check its network request. You can also read currentSrc:
const img = document.querySelector("img");
console.log(img.currentSrc);
console.log(window.devicePixelRatio);Resize the viewport and test different device emulation settings. The selected URL can change because of viewport width, pixel ratio, supported formats, cache state, and network conditions.
Exercise
Which feature chooses between a wide crop and a square crop?
Show answer
The media attribute on picture's source elements. Use srcset and sizes to choose the resolution within the selected crop.