Sub topic 10
Art direction with media
This chapter assumes you know the img element with src and alt, basic CSS media queries, and the difference between CSS pixels and device pixels. It also assumes you have seen srcset with x and w descriptors. Here you will learn how to use picture with media to serve different crops of an image at different viewport widths.
Resolution switching versus art direction
Resolution switching gives the browser several sizes of the same image and lets it choose. The browser makes that choice. Any of the candidates could be picked, and they all show the same picture.
Art direction is different. You want a specific image under a specific condition, because a different crop or composition works better. The browser does not decide this for you. You write the conditions, and the browser follows them.
A wide landscape photo of a sailor on a ship can look good on a desktop monitor. Scaled down to a phone, the sailor becomes a small figure and the details disappear. A tighter, square crop of the sailor's face and the sea works better on the small screen. These are two different images, not two sizes of one image.
| Goal | Tool | Who decides |
|---|---|---|
| Same picture, different file size | `srcset` with `sizes` | The browser |
| Different crop or composition | `picture` with `media` | You, through the media condition |
| Different file format | `picture` with `type` | The browser, from what it supports |
The structure of picture
A picture element contains zero or more source elements, followed by one required img element. The img is the element that actually renders. The source elements only supply alternative candidates for it.
<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 source carries a media attribute holding a media condition. The media attribute accepts the same syntax as a CSS media query. When the condition is true, the browser uses that source's srcset. When it is false, the browser moves on to the next one.
The img has three jobs in this structure:
- It is the fallback for browsers that do not understand
picture. - It is the default when no
sourcematches. - It carries the attributes that apply to the rendered image, such as
alt,width,height, andloading.
Put the alt text on the img, not on the source elements. The source element has no alt attribute.
How source selection works
The browser reads the source elements in document order. It picks the first one that matches and ignores the rest. If none match, it uses the img element's own src and srcset.
The diagram shows this decision process for the example above.
Order matters. Because the first match wins, you must arrange your sources carefully when conditions overlap.
Ordering with min-width
Suppose you have three crops: a wide one, a medium one, and a narrow default. With min-width conditions, list the largest breakpoint first.
<picture>
<source media="(min-width: 70em)" srcset="hero-panorama.jpg">
<source media="(min-width: 40em)" srcset="hero-landscape.jpg">
<img src="hero-square.jpg" width="800" height="800" alt="A sailor looking out to sea">
</picture>At a viewport of 80em, both the first and second conditions are true. The browser picks the first, which is correct. If you reversed the order, the 40em condition would match first at 80em and the panorama would never appear.
Ordering with max-width
With max-width, the order reverses. List the smallest breakpoint first.
<picture>
<source media="(max-width: 30em)" srcset="hero-square.jpg">
<source media="(max-width: 60em)" srcset="hero-landscape.jpg">
<img src="hero-panorama.jpg" width="1600" height="700" alt="A sailor looking out to sea">
</picture>At 25em, both conditions are true, and the first one wins. The rule is the same in both cases: the most specific condition goes first.
Avoiding overlap with ranges
You can make each condition self-contained so that order stops mattering.
<picture>
<source media="(min-width: 70em)" srcset="hero-panorama.jpg">
<source media="(min-width: 40em) and (max-width: 69.99em)" srcset="hero-landscape.jpg">
<img src="hero-square.jpg" width="800" height="800" alt="A sailor looking out to sea">
</picture>This is more verbose, and it is easy to leave a gap. Most teams rely on ordering with a single-sided condition and add a comment where the order is important.
The media attribute is not the same as sizes
Learners often confuse media on a source with the media conditions inside sizes. They look alike but do different jobs.
mediaon asourceis a hard rule. If the condition matches, that source is used. The browser has no say.- A media condition in
sizesdescribes the layout width of the image. The browser still chooses among thesrcsetcandidates itself.
Because media is a hard rule, the browser cannot apply its usual optimizations, such as choosing a smaller file when the connection is slow. Reserve picture with media for cases where the content truly differs. For the same picture at different sizes, use srcset and sizes on a plain img.
Which viewport does media check?
The media attribute checks the viewport, like a CSS media query. It does not check the width of the image's container. A narrow sidebar on a wide screen still matches (min-width: 70em).
This matters in component-based designs. If a card component can appear in a wide main column or a narrow sidebar, a viewport-based condition cannot tell the two apart. In that case, you have two options:
- Pass a different image set to the component depending on where it is placed.
- Pick a crop that works acceptably at every width the component can take.
A complete example with resolution switching
A source element can contain srcset with w descriptors and sizes, so each art-directed crop can also have several file sizes. This example uses sizes inside the source, which works the same way as on an img.
<picture>
<source
media="(min-width: 50em)"
srcset="hero-wide-800.jpg 800w, hero-wide-1600.jpg 1600w"
sizes="100vw">
<img
src="hero-narrow-800.jpg"
srcset="hero-narrow-400.jpg 400w, hero-narrow-800.jpg 800w"
sizes="100vw"
width="800"
height="800"
alt="A sailor looking out to sea">
</picture>The browser works in two steps. First it picks a source by checking media. Then it picks a candidate inside that source by using the width descriptors, the sizes value, and the device pixel ratio. A later chapter combines this with format switching.
Common errors
Putting the img first or leaving it out
The img must be the last child of picture. A picture without an img shows nothing. A source placed after the img is ignored.
Forgetting alt
Because alt belongs on the img, it is easy to leave out when the markup is copied around. Without it, screen readers may read out the file name. Write alt describing the image content, and keep it the same across crops, since the crops show the same subject.
Mismatched dimensions
Each crop can have a different aspect ratio. The width and height on the img describe only the image the browser uses by default. When a source with a different aspect ratio is selected, the browser reserves space using the img ratio until the file loads, and then the layout can jump.
Two fixes are common:
- Give every crop the same aspect ratio. This limits your design options.
- Set
widthandheighton eachsourceelement. Current browsers support these two attributes onsourceinsidepicture, and they use them to compute the ratio for the chosen source. Check support for the browsers you target before relying on it.
Using picture when srcset is enough
If every file is the same picture at a different size, picture adds complexity and removes the browser's freedom to choose. Use picture only when the content or format changes.
Testing only one width
Art direction bugs appear at breakpoints. Resize your window slowly across each breakpoint and watch which crop loads. Chrome, Firefox, and Safari all update the image when the window crosses a breakpoint, although some browsers keep a larger image that has already loaded and do not swap to a smaller one. A hard reload at the target width gives the clearest result.
Checking what was chosen
You can confirm which URL the browser selected with a short script. The currentSrc property of the img reports the result, even when the choice came from a source.
const img = document.querySelector('picture img');
console.log(img.currentSrc);
console.log(window.devicePixelRatio);Run this in the console, resize the window across a breakpoint, and run it again. If the currentSrc value did not change, the browser may be keeping an already loaded file. Reload the page at the new width.
Choosing a design
Use these questions to decide whether you need art direction.
- Does the important part of the image become too small or unreadable on small screens? If so, crop differently.
- Does the image contain text that must stay legible? Crop or replace it for small screens.
- Would the same file, scaled, work at every width? If so, use
srcsetandsizes, notpicture. - Do you have the time to produce and maintain each crop? Every extra crop is another file to export and test.
Exercises
Exercise
A page has two sources in this order: (min-width: 40em) pointing to medium.jpg, then (min-width: 70em) pointing to large.jpg. The viewport is 80em wide. Which file loads, and how do you fix the order so large.jpg loads at that width?
Show answer
medium.jpg loads, because both conditions are true at 80em and the browser uses the first matching source. Put the (min-width: 70em) source first, then the (min-width: 40em) source.
Exercise
A teammate wants to serve the same photo at 400, 800, and 1600 pixels wide, using three source elements with media conditions. Why is this a poor choice, and what should they use instead?
Show answer
A media condition is a hard rule, so the browser cannot account for device pixel ratio, layout width, or other factors when choosing. Since the picture is the same, they should use a single img with srcset using w descriptors and a sizes attribute.