Sub topic 8
Responsive images and media
This chapter assumes you know basic HTML, the CSS cascade and the box model, and that you have already set the viewport meta tag. Here you will make images fit any screen width and avoid sending a phone more pixels than it needs.
Keep images inside their container
By default an image renders at its intrinsic size. A 1600 px photo in a 360 px column overflows and forces horizontal scrolling. This rule is a sensible part of your base styles:
img {
max-width: 100%;
height: auto;
}max-width: 100% stops the image from growing past its container. height: auto keeps the aspect ratio when the width shrinks. The same rule works for video and svg, so many stylesheets list them together.
Let the browser choose a file
Shrinking a large file in CSS fixes the layout but not the download. A phone still fetches the 1600 px file. The srcset and sizes attributes fix this by offering several files and letting the browser choose.
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(min-width: 48em) 50vw, 100vw"
width="800" height="600"
alt="A hiker on a mountain ridge">srcsetlists candidate files. Thewvalue is each file's real pixel width.sizestells the browser how wide the image will be displayed. Here it is half the viewport at 48em and wider, and the full viewport below that.srcis the fallback for browsers that ignoresrcset.
The browser combines the displayed width with the screen's pixel density. On a 400 px wide phone with a 2x screen, it needs about 800 pixels, so it picks photo-800.jpg.
sizes is evaluated before CSS finishes loading, so it cannot read your stylesheet. Keep its breakpoints in step with your media queries by hand.
Reserve space with width and height
The width and height attributes in the example give the browser the aspect ratio before the file arrives. With height: auto in your CSS, the browser reserves the right amount of space, so the text below does not jump when the image loads. That jump is called layout shift.
Different crops with picture
srcset swaps sizes of the same image. When a small screen needs a different crop, such as a tight close-up instead of a wide landscape, use picture:
<picture>
<source media="(min-width: 48em)" srcset="hero-wide.jpg">
<img src="hero-tall.jpg" width="600" height="800" alt="A hiker on a mountain ridge">
</picture>This follows the mobile-first pattern. The img is the small-screen default, and the source with a min-width condition applies only on wider screens. The browser uses the first source that matches. The img element is required, and it is where alt goes.
Quick reference
| Need | Tool |
|---|---|
| Image must not overflow | `max-width: 100%; height: auto` |
| Same image, several resolutions | `srcset` with `sizes` |
| Different crop per screen | `picture` with `source media` |
| No layout shift | `width` and `height` attributes |
Common errors
- Writing
sizes="100vw"for an image that is really half the width. The browser then downloads files that are too large. - Using
pxvalues insrcsetdescriptors wherewvalues are needed, or mixingwandxdescriptors in one list. - Omitting
alt. Use an emptyalt=""only for purely decorative images.
Exercise
A card image is displayed at 100% of the viewport width below 48em, and at 33% of the viewport width at 48em and wider. Write the sizes attribute.
Show answer
sizes="(min-width: 48em) 33vw, 100vw". The browser checks conditions in order and uses the first match. The final value, 100vw, is the default when no condition matches.