Layout stability and font performance
Definition and purpose
Layout stability means avoiding unexpected changes to visible positions. CLS aggregates unexpected layout shifts in session windows; it is not a direct measure of speed or all layout changes.
Reserve an image ratio
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { width: 200px; aspect-ratio: 2 / 1; } #b { display: block; width: 100%; height: auto; } </style> </head> <body> <div id="a"> <img id="b" alt="Blue rectangle" src="data:image/svg+xml,%3Csvg xmlns=%27http://www.w3.org/2000/svg%27 width=%27200%27 height=%27100%27%3E%3Crect width=%27200%27 height=%27100%27 fill=%27blue%27/%3E%3C/svg%3E"> </div> <p id="c">Following text</p> </body></html>A 200px box reserves 100px height.
Syntax and mental model
Use image width and height attributes matching the intrinsic ratio, or CSS aspect-ratio with a definite width, to reserve space before loading. For fonts, choose a compatible fallback and consider font-display and metrics overrides. A swap can change line breaks even if text becomes visible quickly. Inspect layout-shift entries and the Performance panel; compare cold and warm loads. The data image fixture verifies geometry only, not CLS or download delay.
Responsive ratio
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { width: 100%; max-width: 400px; aspect-ratio: 2 / 1; } #b { display: block; width: 100%; height: auto; } </style> </head> <body> <div id="a"> <img id="b" alt="Blue rectangle" src="data:image/svg+xml,%3Csvg xmlns=%27http://www.w3.org/2000/svg%27 width=%27200%27 height=%27100%27%3E%3Crect width=%27200%27 height=%27100%27 fill=%27blue%27/%3E%3C/svg%3E"> </div> <p id="c">Following text</p> </body></html>The reserved ratio scales with width.
Common mistakes
Never report CLS=0 from a single rectangle measurement. A fixed height that clips responsive content is not a robust solution.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Layout thrashing and profiling. Next lesson: CSS injection and privacy.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Layout stability and font performance — knowledge check
Five questions on layout stability and font performance.
What can an unsized image cause?
What reserves proportional space?
Can font swaps alter line breaks?
What does CLS measure?
What verifies real shift behavior?