Containment and Rendering Performance
What Is It?
Containment limits how a region interacts with the rest of a page. Content visibility can let the browser skip offscreen rendering work while preserving the content in the document.
Properties, Tags and Attributes Explained
contain: Applies layout, paint, size, or style containment, with different effects for each.content-visibility: auto: Lets the browser skip rendering work for irrelevant offscreen content.contain-intrinsic-size: Provides placeholder size information when size containment applies.will-change: Hints at expected changes; excessive hints can waste memory and resources.
Each example is a complete HTML page. The CSS is inside the head's style element. Use Try in new tab to edit the CSS or HTML, run your changes, and reset the example.
Example 1: Paint containment
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Containment and Rendering Performance</title>
<style>
.demo { contain: paint; width: 180px; padding: 16px; border: 2px solid #64748b; }
.child { width: 260px; background: #dbeafe; }
</style>
</head>
<body>
<div class="demo"><div class="child">This child paints only inside the containing boundary.</div></div>
</body>
</html>What you see: The oversized child is clipped at the paint containment boundary.
Example 2: Offscreen sections
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Containment and Rendering Performance</title>
<style>
.lesson { content-visibility: auto; contain-intrinsic-size: auto 220px; padding: 20px; border: 1px solid #cbd5e1; margin-bottom: 12px; }
</style>
</head>
<body>
<section class="lesson"><h2>First section</h2><p>Read the first lesson.</p></section><section class="lesson"><h2>Second section</h2><p>Offscreen sections may defer rendering.</p></section>
</body>
</html>What you see: The sections remain readable. Performance effects become measurable with many offscreen sections, rather than a tiny example.
Example 3: Apply a limited rendering hint
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Containment and Rendering Performance</title>
<style>
.tile { display: inline-block; padding: 20px; background: #dbeafe; }
.tile:hover, .tile:focus-visible { will-change: transform; transform: translateY(-2px); }
@media (prefers-reduced-motion: reduce) { .tile:hover, .tile:focus-visible { transform: none; will-change: auto; } }
</style>
</head>
<body>
<button class="tile" type="button">Inspect the hint in DevTools</button>
</body>
</html>What you see: The hint is present only during the interactive state. Its performance benefit is not visible in this tiny demo; measure a real rendering problem before using it, and prefer no hint by default.
When and Where to Use It
Consider these tools for long feeds and isolated widgets after measuring a rendering problem.
Common Mistake or Useful Tip
Containment changes layout or clipping behavior. Do not add it everywhere as an automatic optimization, and remove will-change after a temporary need ends.
Try It Yourself
Remove contain: paint and observe where the oversized child paints.