A page that looks right can still be slow, and on a phone the most common cause is an image that is far larger than the screen it is displayed on. The fix is to offer the browser several files and let it choose, and to tell it how big the image will actually be so the choice is accurate.
The previous lesson in this module covered "How Do You Choose Breakpoints Without Guessing?", and this lesson is the last one in the Responsive Web Design module.
What is the problem?
An image that fills the width of a 400-pixel phone is downloaded at the size of a 1600-pixel desktop, because the src attribute names exactly one file. The visitor pays for four times the pixels they need, and the page is slower for everyone on a slow connection.
<!-- one file for every screen -->
<img src="hero-1600.jpg" alt="...">
Two attributes fix it. srcset offers several candidate files with their real widths, and sizes describes how wide the image will be displayed, so the browser can pick before the CSS has loaded.
How does srcset work?
<img src="hero-800.jpg"
srcset="hero-400.jpg 400w,
hero-800.jpg 800w,
hero-1600.jpg 1600w"
sizes="(max-width: 40rem) 100vw, 50vw"
alt="A workbench with the tools laid out in order of use"
width="1600" height="900">
The w suffix is a width descriptor: the browser knows the actual pixel width of each file. The sizes attribute gives the width the image will occupy in CSS pixels, and it is what makes the choice accurate — the browser combines the two with the screen width and the device pixel ratio to pick a file.
The sizes value is a media query list, and the first match wins. (max-width: 40rem) 100vw says "below 40rem, the image is the full width of the screen"; 50vw says "above that, it is half the screen". If you leave sizes out, the browser assumes the image fills the whole viewport, which is often wrong and means it downloads a file that is too large.
What if the image needs to be cropped differently?
Sometimes a wide banner should become a tall crop on a phone, and that is a different image, not a different size. The media descriptor in srcset handles it.
<picture>
<source media="(max-width: 40rem)" srcset="banner-portrait.jpg">
<source media="(min-width: 40rem)" srcset="banner-wide.jpg">
<img src="banner-wide.jpg" alt="A wide banner">
</picture>
The picture element gives the browser a list of sources to choose from, and the img inside it is the fallback that is always the last resort. The same element is how you serve a modern format with a fallback.
<picture>
<source type="image/avif" srcset="photo.avif">
<source type="image/webp" srcset="photo.webp">
<img src="photo.jpg" alt="A photograph of the workshop">
</picture>
How do images fill a box without distorting?
An image has its own aspect ratio, and a box has its own. When they disagree, something has to give — and by default the image is squashed.
.thumb {
width: 100%;
height: 12rem;
object-fit: cover; /* fill the box, crop the overflow */
border-radius: 8px;
}
object-fit: cover scales the image to cover the whole box and crops the overflow, which is what you want for a thumbnail. contain scales it to fit entirely inside the box, leaving empty space. fill is the default and stretches it, which is almost never what you want. none uses the intrinsic size.
The object-position property then controls which part survives the crop, and it takes a position or keywords.
.avatar {
width: 3rem;
height: 3rem;
object-fit: cover;
object-position: center; /* or "top", "left 20%", and so on */
border-radius: 50%;
}
The same properties work on a video element, which is what makes a video fill a fixed-height hero without letterboxing.
What else matters for performance?
Three things. The loading attribute, so images below the fold are deferred. The decoding attribute, which lets the browser decode asynchronously rather than blocking. And the width and height attributes, so the browser can reserve the space before the file arrives and the page does not jump.
<img src="card.jpg"
srcset="card-400.jpg 400w, card-800.jpg 800w"
sizes="(max-width: 40rem) 100vw, 30vw"
width="800" height="600"
loading="lazy"
decoding="async"
alt="A card of the workshop bench">
Note that the width and height describe the largest file, and the browser uses the ratio to reserve the right box for whichever file it picks. The loading="lazy" should not go on the image at the top of the page, because deferring it delays the moment the page looks complete.
How do you choose which sizes to offer?
The srcset attribute is a list of candidate files, and the browser picks one of them. The question is which files to put in the list, and the answer follows from how the image is displayed rather than from the device you are guessing at.
Start with the width the image actually occupies in the layout. If a card image is never wider than 400 pixels on any screen, a 2000 pixel file is wasted bytes no matter how fast the connection is. If it is full width on a large monitor, it needs to be large enough for that.
Then offer a few multiples of that width - typically one times, two times and three times - so that a high density screen has a sharper option. The browser picks the smallest candidate that is at least as large as the rendered size, which keeps the download proportionate.
<img
src="/images/cover-800.jpg"
srcset="
/images/cover-400.jpg 400w,
/images/cover-800.jpg 800w,
/images/cover-1200.jpg 1200w
"
sizes="(min-width: 60rem) 50rem, 100vw"
width="1200"
height="630"
alt="A laptop showing a dashboard"
>The sizes attribute is the part people get wrong. It tells the browser how wide the image will be before it has loaded, so the browser can choose a candidate. The value above says: half the viewport on a wide screen, and the full width otherwise. If you leave sizes out, the browser assumes the image is as wide as the viewport, which is usually far too large and leads to an unnecessary download.
Two rules keep the list short. Do not include a candidate larger than the largest size the image can ever occupy. And do not include more candidates than you need - each extra one is another file to generate, store and keep in step.
For video, the equivalent is more work, because a video file is large and the poster frame matters as much as the video. Use the poster attribute to give the player an image, and serve the video in a modern format so the browser can choose the quality it can play.
Quick reference
- srcset offers several files with their widths; sizes says how wide the image will be.
- Without sizes, the browser assumes the image fills the viewport.
<picture>handles different crops and different formats with a fallback.object-fit:cover fills a box and crops; contain fits inside it.object-positionchooses which part survives the crop.- Set width and height to stop the page jumping, and lazy-load below the fold.
Try it yourself: add a srcset and a sizes to an image, then open the network tab in device emulation and reload. Compare the file that was downloaded with the size of the area the image actually occupies on the screen. Then add object-fit: cover to a fixed-height box and see the crop.