Sub topic 14
Reserving space to avoid layout shift
This chapter assumes you know the img element and its attributes, basic CSS sizing, and how breakpoints work. It builds on the earlier chapters on aspect ratios and cropping with CSS.
What layout shift is
Layout shift happens when content moves after the page has started to display. A browser does not know how tall an image is until the file begins to download. If the page gives it no hint, the image takes up no space at first. When the file arrives, the browser inserts the image and pushes everything below it down.
Readers feel this as a jump. They lose their place, or they tap the wrong button because it moved just before the tap. Browsers measure this as Cumulative Layout Shift (CLS), one of the Core Web Vitals. A score of 0.1 or lower is considered good.
Declare width and height
The simplest fix is to put width and height attributes on every img. Browsers use the two values to compute an aspect ratio before the file loads, and they reserve a box of that shape.
<img
src="team-800.jpg"
width="800" height="600"
alt="Four colleagues around a whiteboard">The attributes are pixel values without units. They describe the file's intrinsic proportions (4:3 here), not the final display size. CSS can still resize the image.
Make it responsive without losing the ratio
Responsive layouts usually set width: 100% or a max-width. Set the height to auto so the image scales with its width and keeps the ratio from the attributes.
img {
max-width: 100%;
height: auto;
}Without height: auto, the height attribute would act as a fixed height. The image would then stretch or squash on narrow screens.
Reserve space with aspect-ratio
When CSS crops an image into a fixed shape, as in the hero example from the previous chapter, declare that shape in CSS. The box then keeps its size even before the image loads.
.hero img {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
}Here the CSS ratio takes precedence over the file's own ratio. The 16:9 box is reserved whatever the image's proportions are, and object-fit: cover fills it.
Picture and srcset
Different source files inside a picture element can have different ratios, such as a wide crop and a square crop. The width and height on the fallback img describe only that image, so they can be wrong for the other crops. Two fixes work:
- Give every crop the same ratio, so the attributes are right for all of them.
- Set the ratio per breakpoint in CSS with
aspect-ratio, and letobject-fit: coverhandle small differences.
With srcset and sizes, all candidates are the same picture at different widths. One width and height pair is therefore correct for all of them.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| No `width` or `height` | Content jumps when the image loads | Add both attributes |
| `height` attribute without `height: auto` in CSS | Distorted image on narrow screens | Add `height: auto` |
| Attributes that don't match the file's ratio | Wrong space reserved, then a small jump | Copy the real pixel dimensions |
| Images injected by script with no size | Shift when they appear | Set the size before inserting them |
| Lazy-loaded image with no reserved space | Shift while scrolling | Reserve space as for any image |
Exercise
A page has <img src="a.jpg" width="1200" height="800"> and the CSS img { width: 100%; }. On a phone the picture looks stretched vertically, and the content still shifts slightly. What is missing, and why does adding it fix both problems?
Show answer
The CSS needs height: auto. Without it, the height="800" attribute becomes a fixed height of 800px, which stretches the image on a narrow screen. With height: auto, the browser computes the height from the 3:2 ratio of the attributes and the actual width, so the image keeps its proportions and the reserved box matches the final one.
Exercise
You use object-fit: cover to crop a photo into a 4:3 card. Which property reserves the card's space before the photo loads?
Show answer
aspect-ratio: 4 / 3 on the image or its container. object-fit only controls how the image fills the box once it exists.