Sub topic 11
Different crops for different screens
This chapter assumes you know the img element and its attributes, basic CSS, and how breakpoints work in a responsive layout. It covers art direction: serving a differently cropped photo to different screens.
Why one crop is not enough
A wide 16:9 crop works on a desktop. On a phone, that same image shrinks to a thin strip. The subject becomes tiny and the detail is lost.
CSS cropping with object-fit and object-position helps, but it only removes pixels from one source file. If the subject sits in a small part of a wide photo, a narrow crop of that file may cut the subject off. It may also leave the subject too small to read.
Art direction solves this by giving the browser separate files, each cropped for its own layout. A designer makes each crop on purpose, so each one keeps the subject prominent.
The picture element
The picture element wraps several source elements and one img. The browser checks each source in order and uses the first one whose media condition matches. If none match, it uses the img.
<picture>
<source media="(min-width: 800px)" srcset="hero-wide.jpg">
<img src="hero-square.jpg" width="800" height="800" alt="A baker lifting a loaf from a stone oven">
</picture>Viewports at least 800px wide get the wide crop. Narrower viewports get the square crop, which was made separately so the baker stays large in the frame.
The img element is required. It holds the fallback file, the alt text, and the dimensions. The source elements only change which file is loaded.
Common errors
- Putting alt text on
source. Alt text belongs only on theimg. It applies to every crop. - Leaving out the
img. Without it, nothing renders. - Using one crop for everything and calling it done. If you are not changing the crop, you do not need
picture. Usesrcsetandsizesfor size changes alone. - Mismatched dimensions. If the wide and square crops have different aspect ratios, the
widthandheighton theimgdescribe only the fallback. Content can shift when a different source loads. Match each crop's ratio to the layout slot, and test each breakpoint. - Cropping out the focal point. Check every version. A crop that works at one ratio can lose the subject at another.
Combining picture with CSS cropping
You can use both techniques. Art direction handles the big changes between layouts. CSS handles small adjustments within one layout.
.hero img {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
object-position: 30% 40%;
}Here the image fills a 16:9 box and the browser crops the overflow. object-position moves the visible area toward the subject instead of the center. This works well when the wide source is slightly larger than the box.
When to use art direction
| Situation | Approach |
|---|---|
| Same crop, different file sizes | `srcset` and `sizes` |
| Same photo, small shift of focus | `object-fit` and `object-position` |
| Subject is lost or too small at some widths | `picture` with separate crops |
| Different formats only | `picture` with `type` |
Art direction costs more. Every extra crop is a file to produce, name, compress, and keep up to date. Use it for key images such as heroes and portraits. Skip it for decorative images where a CSS crop is enough.
Exercise
A hero photo shows a small boat on a wide lake. At 360px wide, the boat is only a few pixels across. Which technique fixes this, and which element must still carry the alt text?
Show answer
Use picture with a tighter crop of the boat in a source for narrow screens, or in the fallback img. The img element carries the alt text, because source elements do not accept it.