Lesson 3 of 30
Reserving space for images fixes the CLS bug everyone already knows about. The shifts that actually surprise teams in production come from fonts, injected content, and animations.
11 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
A page swaps from a fallback font to a custom web font once it loads, and this causes a visible layout shift. What determines whether that swap causes a shift at all?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
You already know the classic CLS fix: reserve space for an image before it loads, usually with width/height or aspect-ratio. That fix handles maybe a third of real-world CLS. The rest comes from sources that don't look like "an image with no dimensions" at all:
top, left, width, or height moves the box on every frame, and if that motion displaces other elements on the page, each of those frames can register as a real layout shift. An animation that uses transform instead moves pixels visually without changing any other element's layout position at all — so it doesn't register on CLS, even though it looks similar to the eye.// Bad: the banner mounts after initial layout, pushing page content down
function App() {
const [showBanner, setShowBanner] = useState(false);
useEffect(() => {
setShowBanner(true); // mounts on the client, after first paint
}, []);
return (
<>
{showBanner && <CookieBanner />}
<PageContent />
</>
);
}
/* Better: reserve the banner's space up front with min-height,
even before it's known whether it'll actually show */
.cookie-banner-slot {
min-height: 64px;
}
If CookieBanner renders at the top of the page after PageContent has already painted, every pixel of PageContent shifts downward the instant the banner appears — a textbook unexpected layout shift, and one that has nothing to do with an unsized image. Reserving the slot's height ahead of time (even while the banner's actual content is still loading) means the shift never happens, because the space was already there.
size-adjust, ascent-override, etc.) exist as a fix.transform-based animation genuinely doesn't count against CLS, because CLS only measures shifts of elements' layout boxes — animating transform changes what's painted on screen without moving any element's position in the layout, so nothing else reflows."We fixed all our unsized images but CLS is still bad — what else would you check?" is a real follow-up senior interviewers ask specifically to see if a candidate's CLS knowledge goes past the one obvious fix into fonts, injected content, and animation choices.