Sub topic 10
Art direction with media
This chapter assumes you know basic HTML, CSS media queries, and the img element with src and alt. It covers one job: showing a different crop of an image at different viewport widths.
Art direction versus resolution switching
Resolution switching gives the browser several sizes of the same picture and lets it choose. Art direction is different. You supply different pictures, such as a wide landscape crop and a tight square crop, and you decide which one appears at which width.
A wide photo scaled down to a phone often leaves its subject too small to see. A square crop centered on the subject reads better. The picture element lets you offer both.
The picture element and media
A picture element holds zero or more source elements and then one img. Each source can carry a media attribute with a media condition, the same syntax you use in CSS.
<picture>
<source media="(min-width: 50em)" srcset="hero-wide.jpg">
<img src="hero-narrow.jpg" width="800" height="800" alt="A sailor looking out to sea">
</picture>Here the browser uses hero-wide.jpg when the viewport is at least 50em wide. In every other case it falls back to the img, which shows the square crop.
How source selection works
The browser reads the source elements from top to bottom. It uses the first one whose media condition matches. It then ignores the remaining sources.
flowchart TDThe diagram below shows the decision.
Because the first match wins, order matters. With several sources, put the most specific condition first.
<picture>
<source media="(min-width: 70em)" srcset="hero-panorama.jpg">
<source media="(min-width: 40em)" srcset="hero-wide.jpg">
<img src="hero-narrow.jpg" width="800" height="800" alt="A sailor looking out to sea">
</picture>If you reversed these two sources, a 75em viewport would match (min-width: 40em) first and never reach the panorama.
Rules that cause common errors
- The
imgis required. Without it, nothing displays. It also carries thealttext and acts as the fallback. - Put the
imglast. Sources after it are ignored. - The
alt,width, andheightbelong on theimg, not onsource. The chosen file is rendered through thatimg. - Browsers treat
mediaas a hard rule, not a hint. Unlikesizes, the browser must obey it. When you resize the window, the image changes at the breakpoint. - The
mediacondition looks at the viewport, not at the space the image occupies. A narrow image in a wide viewport still matchesmin-widthconditions.
Matching dimensions
The crops usually have different aspect ratios. The wide crop might be 1600 by 600 and the square crop 800 by 800. The width and height on the img describe only the fallback. Modern browsers use them to compute an aspect ratio for reserving space, but when a source with another ratio is chosen, the layout can still shift. To avoid that, set the ratio in CSS at the same breakpoint.
picture img {
width: 100%;
height: auto;
aspect-ratio: 1 / 1;
}
@media (min-width: 50em) {
picture img {
aspect-ratio: 8 / 3;
}
}Use the same breakpoint in the CSS and in the media attribute so the two stay in step.
Exercise
Exercise
A page has <source media="(min-width: 40em)" srcset="a.jpg"> followed by <source media="(min-width: 70em)" srcset="b.jpg">, then an img with src="c.jpg". Which file shows at a viewport width of 80em, and how do you make b.jpg appear there?
Show answer
a.jpg shows, because 80em matches the first source and the browser stops at the first match. Swap the order so the (min-width: 70em) source comes first. Then 80em gets b.jpg, widths from 40em to 70em get a.jpg, and narrower viewports get c.jpg.