Sub topic 11
Different crops for different screens
This chapter covers art direction: serving a different crop of a photo at different screen sizes. It assumes you know the img element, basic CSS, and how breakpoints work. It also assumes you have read the earlier chapters on aspect ratios, focal points, and cropping with object-fit and object-position.
Why one crop is not enough
A wide hero photo works well on a desktop monitor. On a phone, the same file is squeezed into a narrow column. If you scale it down, the subject becomes tiny. If you crop it with object-fit: cover, the browser cuts away the sides, and the subject may be cut away with them.
CSS cropping has limits. object-position can shift the visible window, but it can only show part of the one image you supplied. If the subject takes up a small part of a wide frame, a tall crop will show either a lot of empty space or a sliver of the subject.
Art direction solves this by giving the browser two or more separate image files, each cropped on purpose. You decide what each screen shows. The browser only picks which file to load.
Art direction versus resolution switching
Two features look alike but do different jobs.
| Goal | Tool | Who decides | Content of the files |
|---|---|---|---|
| Show a different crop or composition | `picture` with `source media` | You | Different framing of the photo |
| Show the same picture at a suitable file size | `srcset` with `sizes` | The browser | Same framing at different pixel widths |
If you only want a smaller file on a small screen, use srcset (covered in the next chapter). If you want a different framing, use picture. The two can be combined, which is shown later in this chapter.
The picture element
The picture element wraps one img and any number of source elements. The browser reads the source elements from top to bottom 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>Here is what happens:
- At 800px viewport width or more, the browser loads
hero-wide.jpg. - Below 800px, no
sourcematches, so the browser loadshero-square.jpgfrom theimg.
The img element is required. It is the fallback, and it is also the element that is actually displayed. Everything you style, label, or measure applies to the img, not to picture or source.
Order matters
The first matching source wins, even if a later one would be a better fit. List the most specific conditions first when you use more than one:
<picture>
<source media="(min-width: 1200px)" srcset="hero-ultrawide.jpg">
<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>If you reversed the two source elements, a 1400px-wide viewport would match min-width: 800px first and the ultrawide crop would never load.
Planning the crops
Make the crops before you write the markup. A practical process:
- Start from the original, uncropped photo at the highest resolution you have.
- Identify the focal point, such as the baker's face and hands.
- Choose the layouts you need. Two is usually enough: a wide crop for large screens and a square or portrait crop for phones.
- Crop each version in an image editor so the focal point sits where you want it in that frame.
- Export each file at the size it will be displayed, allowing for high-density screens.
For the baker photo, the wide crop might keep the oven, the counter, and some empty wall on the left for a headline. The square crop drops the wall and most of the counter and fills the frame with the baker and the loaf.
Choose ratios that match your layout
Pick ratios from the layout, not from the photo. If the hero is 16:9 on desktop and 1:1 on mobile, export the files at those ratios. The width and height attributes on the img should match the fallback file. Browsers use them to reserve space before the file loads.
Keeping the layout stable when crops differ
The two files in the example have different aspect ratios: wide and square. The width and height attributes on the img describe only the fallback (800 by 800). When the wide file loads, its aspect ratio is different, and the layout can jump.
You can prevent this in two ways.
Declare the ratio in CSS and change it at the same breakpoint as the source:
.hero img {
width: 100%;
height: auto;
aspect-ratio: 1 / 1;
}
@media (min-width: 800px) {
.hero img {
aspect-ratio: 16 / 9;
}
}Or give each source its own width and height attributes. Modern browsers accept these on source elements when used inside picture:
<picture>
<source media="(min-width: 800px)" srcset="hero-wide.jpg" width="1600" height="900">
<img src="hero-square.jpg" width="800" height="800" alt="A baker lifting a loaf from a stone oven">
</picture>Make sure the media query in your CSS uses the same breakpoint as the one in the source. If they differ, there will be a range of widths where the reserved space does not match the loaded file.
Combining art direction with srcset
Each source can have its own srcset and sizes, so you can offer several resolutions of each crop. The browser first selects the source by media, then picks a file from that source's srcset.
<picture>
<source
media="(min-width: 800px)"
srcset="hero-wide-800.jpg 800w, hero-wide-1600.jpg 1600w, hero-wide-2400.jpg 2400w"
sizes="100vw">
<img
src="hero-square-400.jpg"
srcset="hero-square-400.jpg 400w, hero-square-800.jpg 800w"
sizes="100vw"
width="800" height="800"
alt="A baker lifting a loaf from a stone oven">
</picture>The decision happens in two steps:
This gives you control over framing and lets the browser keep file sizes low.
Using other media conditions
The media attribute accepts any media query, not only width.
(orientation: portrait)suits a phone held upright or a tall window.(min-resolution: 2dppx)targets high-density screens, thoughsrcsetis usually the better tool for that.(prefers-color-scheme: dark)can swap in a darker photo for dark mode.
<picture>
<source media="(prefers-color-scheme: dark)" srcset="cafe-night.jpg">
<img src="cafe-day.jpg" width="1200" height="800" alt="Customers at a corner cafe">
</picture>Use orientation and color scheme sparingly. Each extra condition means another file to prepare, test, and keep up to date.
Combining picture with CSS cropping
You can still use object-fit on the img inside picture. This helps when a layout box has an unpredictable ratio, such as a card that stretches in a grid.
.hero img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: 30% 40%;
}Remember that object-position applies to whichever file is loaded. A value that suits the wide crop might not suit the square one. If they need different values, you cannot switch object-position by source, so use a media query in CSS at the same breakpoint.
Common mistakes
- Missing
img. Apicturewith onlysourceelements shows nothing. - Different alt text per source. Alt text belongs on the
imgand applies to every crop. Write text that is true for all versions. - Hiding information on one crop. If the wide crop shows a sign or a logo that the square crop removes, the page loses meaning on phones. Check that each crop keeps what the page depends on.
- Mismatched breakpoints. The
sourcemedia query and the CSSaspect-ratioquery should use the same number. - Too many crops. Three or more versions of every photo multiply the work. Reserve art direction for images where the subject really is lost, such as heroes and key editorial photos.
- Testing only by resizing. Browsers cache the file they have already chosen, and some do not re-evaluate on every resize. Reload the page after changing the window width, and check the network panel to confirm which file loaded.
When to skip art direction
Use plain CSS cropping when the subject is large and central, or when the difference between crops is small. Use picture when the subject is small in a wide frame, when you must place the subject on one side to leave room for text on desktop, or when a mobile layout needs a very different shape.
Exercise
A hero image has the subject on the right side with empty space on the left for a headline on desktop. On phones, the headline sits above the image and the subject should fill a square. Write the picture markup, with a wide file at 900px and up and a square fallback, and name the one CSS rule you must add to avoid layout shift.
Show answer
Use <picture><source media="(min-width: 900px)" srcset="hero-wide.jpg" width="1600" height="900"><img src="hero-square.jpg" width="800" height="800" alt="..."></picture>. Add a CSS rule that sets aspect-ratio: 16 / 9 on the img inside a @media (min-width: 900px) query, with the default aspect-ratio: 1 / 1, so the reserved space changes at the same 900px breakpoint as the source.
Exercise
You have two sources: (min-width: 800px) and (min-width: 1200px). A visitor has a 1400px-wide window and gets the 800px crop instead of the 1200px crop. What is the cause and the fix?
Show answer
The 800px source is listed first, so it matches first and the browser stops checking. Put the (min-width: 1200px) source before the (min-width: 800px) source.