Sub topic 11
Serving the right image size
This chapter assumes you know basic HTML, the CSS box model and units, and how to open your browser's developer tools. It builds on the fluid single-column layout from earlier chapters. Images are usually the heaviest part of a page, so sending a phone the same file as a large desktop monitor wastes data and time.
The problem with one image for every screen
A single 1600-pixel-wide photo looks sharp on a desktop. A phone showing that photo in a 360-pixel-wide column downloads far more pixels than it can display. The extra bytes slow the page on a mobile connection and give the user nothing in return.
The fix is to offer the browser several versions of the same image and let it choose. The browser knows the screen width, the pixel density and often the connection quality. You do not.
srcset and sizes
Two attributes work together on the img element:
srcsetlists candidate files and states each file's real width with awdescriptor, for example400w.sizestells the browser how wide the image will be displayed in the layout.
The browser reads sizes first, works out the display width, multiplies it by the screen's pixel density, and picks the smallest candidate that covers the result.
<img
src="team-800.jpg"
srcset="team-400.jpg 400w, team-800.jpg 800w, team-1600.jpg 1600w"
sizes="(min-width: 64em) 50vw, 100vw"
width="800"
height="450"
alt="The support team standing in front of the office"
loading="lazy">Read sizes from left to right. The first condition that matches wins. Here, at 64em and wider the image fills half the viewport. Otherwise it fills the whole viewport, which matches a single-column mobile layout. The last value has no condition and acts as the default.
The src attribute is the fallback for browsers that ignore srcset.
Reserve space with width and height
Set width and height to the image's intrinsic size. The browser uses the ratio to reserve space before the file arrives. Without it, text jumps down when the image loads. This jump is called layout shift.
Pair the attributes with this CSS so the image still scales fluidly:
img {
max-inline-size: 100%;
block-size: auto;
}The attributes supply the aspect ratio. The CSS keeps the image inside its column.
Lazy loading below the fold
The loading="lazy" attribute tells the browser to delay fetching an image until the user scrolls near it. This saves data for content that many visitors never reach.
Do not lazy-load the main image at the top of the page. It is needed immediately, and delaying it makes the page feel slower. Leave loading off for that image.
How the browser chooses
The following flow shows the decision for one image.
For example, a phone with a 2x screen showing a 100vw image in a 390-pixel viewport needs about 780 pixels. It picks team-800.jpg, not the 1600-pixel file.
Common errors
- Omitting
sizes. The browser then assumes the image is 100vw wide, which over-fetches when the image sits in a narrow column. - Writing
sizesthat disagrees with the CSS layout. The browser trustssizes, so keep it in step with your real column widths. - Leaving out
widthandheight, which causes layout shift. - Lazy-loading the top image.
- Using
srcsetwith a missingalt. Every content image still needs a text alternative.
Checking the result
Open the developer tools, go to the Network panel, and reload at a narrow width. Note which file loads. Widen the window, reload, and compare. Many browsers also show the chosen file as the image's current source in the Elements panel.
Exercise
A product image sits in a single column on phones and fills one third of the viewport at 64em and wider. You have files at 400w, 800w and 1600w. Write the sizes attribute, and say which file a 2x phone with a 390px viewport should download.
Show answer
sizes="(min-width: 64em) 33vw, 100vw". On the phone the image is 390 CSS pixels wide, so at 2x it needs about 780 pixels. The browser picks the 800w file, the smallest candidate that covers that width.
Exercise
Why should the hero image at the top of the page not have loading="lazy"?
Show answer
The user sees it immediately, so it must be fetched right away. Lazy loading delays the request and slows the first view of the page. Lazy loading is for images below the fold.