Sub topic 7
Reserving space with width and height
This chapter assumes you know the img element, basic CSS (including max-width and aspect-ratio), and the difference between CSS pixels and device pixels. It also assumes you have read the earlier chapters on srcset and sizes.
The problem: layout shift
A browser lays out a page before it has downloaded the images. If it doesn't know an image's size, it gives the image no space. When the file arrives, the browser learns the real size, and everything below the image jumps down. This jump is called layout shift. It is annoying for readers, who lose their place or tap the wrong link. It also hurts the Cumulative Layout Shift (CLS) score, which is one of the Core Web Vitals that search engines and performance tools report.
The fix is to tell the browser the image's proportions in the HTML, before any file loads.
How width and height work
Add width and height attributes to the img. Use plain integers with no units. They are measured in CSS pixels.
<img
src="photo-800.jpg"
width="1600"
height="1200"
alt="A lighthouse on a rocky coast at sunset">Modern browsers read these two numbers and compute the aspect ratio, here 4:3. They then reserve a box with that ratio before the file loads. The attributes act as a hint for the ratio. They do not lock the image to 1600 by 1200 pixels, as the next section shows.
Making it work with fluid layouts
Most responsive pages let images shrink to fit their container. Add this CSS:
img {
max-width: 100%;
height: auto;
}The max-width rule stops the image from overflowing. The height: auto rule is the important one. Without it, the height="1200" attribute would apply as a fixed height. The image would then stretch or squash when max-width narrows it. With height: auto, the browser keeps the ratio from the attributes and calculates the height from the rendered width.
<style>
img {
max-width: 100%;
height: auto;
}
</style>
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(min-width: 40em) 50vw, 100vw"
width="1600"
height="1200"
alt="A lighthouse on a rocky coast at sunset">On a 360-pixel-wide phone, the image renders about 360 pixels wide. The browser reserves a box 360 wide and 270 tall (a 4:3 ratio) before any bytes arrive.
Which numbers to use
With srcset and w descriptors, use the dimensions of any one of your candidates. All candidates must share the same aspect ratio, so the ratio comes out the same whichever file the browser picks. In the example above, the largest file is 1600 by 1200.
The numbers do not have to match the displayed size. Only their ratio matters when CSS controls the display width.
Common errors
- Writing units, such as
width="400px". Usewidth="400". - Setting
heightin CSS to a fixed value, which breaks the ratio. Useheight: auto. - Giving the attributes one ratio and the files another. If the attributes say 4:3 but the file is 3:2, the image jumps when it loads, or it is distorted.
- Using different ratios inside
picturefor art direction. Each crop can have its own ratio, so give eachsourceits ownwidthandheightattributes. Modern browsers support these onsourceelements.
<picture>
<source
media="(min-width: 50em)"
srcset="hero-wide.jpg"
width="1600"
height="900">
<img
src="hero-narrow.jpg"
width="800"
height="800"
alt="A sailor looking out to sea">
</picture>CSS alternative
If you cannot edit the HTML, use the CSS aspect-ratio property on the image or its container:
.hero img {
width: 100%;
height: auto;
aspect-ratio: 16 / 9;
}This reserves the space too. The HTML attributes are still preferable. They work before any stylesheet loads and they keep the markup self-describing.
Exercise
An image has width="1200" height="800" and your CSS sets img { max-width: 100%; height: 400px; }. On a narrow screen the image looks squashed. What is the cause, and what is the fix?
Show answer
The fixed height: 400px overrides the ratio. When max-width shrinks the width, the height stays at 400px, so the image is distorted. Change the rule to height: auto so the browser calculates the height from the 3:2 ratio given by the attributes.