Containment and content visibility
Definition and purpose
Containment gives the browser explicit isolation guarantees. content-visibility can allow skipping rendering work for content that is not needed yet.
Size isolation
<!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 { contain: size; contain-intrinsic-size: 60px; } </style> </head> <body> <section id="a"> <div id="b" style="height: 80px">Feed card</div> </section> <p id="after">Next card</p> </body></html>The section reserves 60px rather than sizing from its 80px child.
Syntax and mental model
contain: layout establishes layout containment; paint containment clips descendant painting to the containment boundary (with the applicable clip edge). Size containment prevents descendants from determining the contained box size. content-visibility: auto applies layout, style and paint containment and allows skipping offscreen contents, adding size containment while skipped. Use contain-intrinsic-size: auto 80px to provide a placeholder and remember measured size when available. It is not a lazy network loader or a hiding mechanism for sensitive content.
Visible feed card
<!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 { content-visibility: auto; contain-intrinsic-size: auto 80px; } </style> </head> <body> <section id="a"> <div id="b" style="height: 80px">Feed card</div> </section> <p id="after">Next card</p> </body></html>A visible card uses actual contents; scroll a long feed to inspect placeholder changes.
Common mistakes
Do not apply size containment to an auto-height card expecting children to size it as before. Test scroll jumps and focus, not only parsing.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Stylesheet loading and critical CSS. Next lesson: Layout thrashing and profiling.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Containment and content visibility — knowledge check
Five questions on containment and content visibility.
What does size containment change?
What does paint containment constrain?
Why use an intrinsic placeholder?
Does auto visibility hide private content?
What should a feed test include?