Sub topic 8
The picture element and source selection order
This chapter assumes you can write an img with src and alt, that you know srcset and sizes from the earlier chapters, and that you can read a CSS media query.
What picture adds
With a plain img, the browser chooses among files that are all the same picture at different sizes. The picture element lets you offer different files for different reasons. A file can differ in crop, as in art direction, or in format.
picture is a wrapper. It contains zero or more source elements, followed by exactly one img. The picture and source elements only supply candidates. The img is what renders on the page.
<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>At 50em or wider, the browser uses hero-wide.jpg. Otherwise it uses hero-narrow.jpg from the img.
How the browser walks the sources
The browser does not compare all the sources and pick the best one. It reads them from top to bottom and uses the first one that qualifies. A source qualifies when both of these hold:
- Its
mediaattribute is absent, or the media condition is true right now. - Its
typeattribute is absent, or the browser supports that MIME type.
When a source qualifies, the browser stops looking at the other sources. It then picks a file from that source's srcset, using sizes and the device pixel ratio if the source uses w descriptors. If no source qualifies, the browser uses the img element's own src and srcset.
Order matters
Because the first match wins, the order of your sources is part of the logic. A common mistake is to list a broad condition before a narrow one.
<picture>
<source media="(min-width: 30em)" srcset="hero-medium.jpg">
<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>Every viewport that is at least 50em wide is also at least 30em wide. The first source always matches there, so hero-wide.jpg is never used. List the most specific condition first:
<picture>
<source media="(min-width: 50em)" srcset="hero-wide.jpg">
<source media="(min-width: 30em)" srcset="hero-medium.jpg">
<img src="hero-narrow.jpg" width="800" height="800" alt="A sailor looking out to sea">
</picture>The same rule applies to formats. Put the format you prefer first. Put AVIF before WebP, because a browser that supports both stops at the first one. The next chapter covers this in detail.
The img is not optional
The img does two jobs:
- It is the fallback for browsers that do not understand
picture, and for cases where nosourcequalifies. - It is the element that actually displays the image, and it carries the attributes that describe the image.
Selecting a source also does not change the img element's CSS. Any styling you apply to the img applies to whichever file was chosen, so the files should share a consistent aspect ratio, or width and height should match the one you expect.
Common errors
| Symptom | Likely cause |
|---|---|
| The wide image never appears | A broader media condition is listed before it |
| Nothing displays | The `img` is missing or placed before the `source` elements |
| The fallback JPEG always loads | The `type` value is misspelled, for example `image/avif` written as `image/avid` |
| The image changes shape but layout jumps | `width` and `height` on the `img` match only one of the crops |
Exercise
A picture has three sources in this order: media="(min-width: 30em)", media="(min-width: 50em)", and media="(min-width: 70em)". Each points to a different file. A visitor has a 75em-wide viewport. Which source is used, and how would you reorder the sources so that the visitor gets the file meant for 70em and wider?
Show answer
The first source (30em) is used, because it is the first one whose condition is true and the browser stops there. Reorder the sources from the most specific condition to the broadest: 70em first, then 50em, then 30em.