LEARNING OBJECTIVES ⌵
- Understand CSS Containment Level 2 (
contain: layout paint size style). - Implement
content-visibility: autoto skip rendering of off-screen DOM trees. - Prevent scrollbar jumps using
contain-intrinsic-sizeaspect ratio placeholders. - Benchmark 10x rendering speedups on massive DOM structures (e.g. 5,000-row feeds).
📖 The Mental Model & Story
Imagine an architect building a 50-story skyscraper. If changing a picture frame on the 40th floor required structural engineers to recalculate the foundation stability for the entire 50-story building, construction would grind to a halt.
Without CSS Containment, the browser engine treats the DOM as one interconnected web. A small DOM change inside a complex card triggers style and layout recalculations across the entire document.
With contain: layout paint or content-visibility: auto, you place a soundproof glass box around that card. The browser completely skips rendering it until it scrolls into the viewport!
Viewport:
+------------------------------------+
| Visible Card 1 (Rendered) |
| Visible Card 2 (Rendered) |
+------------------------------------+
Below Viewport (content-visibility: auto):
| Off-screen Card 3 ===> SKIPPED! (0ms Layout & Paint time) ⚡
| Off-screen Card 4 ===> SKIPPED! (0ms Layout & Paint time) ⚡
| Off-screen Card 500 => SKIPPED! (0ms Layout & Paint time) ⚡
💻 Interactive Code Playground
📌 Key Takeaways
content-visibility: autoskips rendering of off-screen subtrees until needed.- Always pair
content-visibility: autowithcontain-intrinsic-sizeto prevent scrollbar jumping. - --
❓ Knowledge Check
1. Which of the following is correct?
2. Which of the following is correct?