The CSS contain property tells the browser that an element is **independent** from the rest of the page. This allows the browser to optimize painting, layout, style calculation, and size calculations. It improves performance, especially in large or complex UI layouts.
Note
β Prevents expensive layout recalculations
β Useful for components, widgets, modals, infinite lists
π¦ Syntax
Syntax
contain:
size | layout | style | paint | content | strict | none;
/* Multiple values */
contain: layout paint;
contain: size style;
contain: content; /* shorthand for layout + style + paint */π Values Explained
| Value | Description |
|---|---|
| size | Prevents size of element from depending on children. |
| layout | Restricts internal layout from affecting outside layout. |
| style | Styles inside do not affect external elements (e.g., counters). |
| paint | Prevents content from painting outside its bounds. |
| content | Shorthand for layout + style + paint. |
| strict | Applies ALL types of containment (best isolation). |
| none | No containment (default). |
π¨ 1. Basic Example
Basic contain
.card {
contain: layout paint;
}The cardβs layout and painting are isolated from the rest of the page.
π¨ 2. Using contain: size
size containment
.widget {
contain: size;
height: 200px; /* must define a size */
}Prevents the widgetβs children from affecting the widgetβs size.
Note
π¨ 3. Using contain: content
content shorthand
.modal {
contain: content;
}Equivalent to: layout + style + paint. Ideally used for fully isolated components.
π¨ 4. Using contain: strict
strict containment
.component {
contain: strict;
}Enables all forms of containment β maximum performance isolation.
π¨ 5. Prevent overflowing painting
paint containment
.gallery {
contain: paint;
}Prevents children from drawing outside the container box.
π Visual Breakdown
| Value | What It Prevents |
|---|---|
| layout | Internal layout affecting ancestors |
| paint | Overflow painting outside box |
| style | Counters, quotes, list styles leaking out |
| size | Size recalculation from children |
π§ͺ Real-World Use Cases
1οΈβ£ Reusable UI Components
component example
.card {
contain: content;
}Best for isolated cards, widgets, product components.
2οΈβ£ Infinite Scrolling Layouts
infinite list
.list-item {
contain: layout;
}Prevents deep layout recalculations when adding or removing items.
3οΈβ£ Web Apps with Many Panels
panels
.panel {
contain: strict;
}Boosts performance in dashboards, editors, and admin UIs.
β οΈ Common Mistakes
- β Using size without defining width/height
- β Expecting contain to fix overflow clipping (use overflow)
- β Applying strict to everything β may break layout
- β Forgetting that containment isolates counters and styles
Note
π₯ Summary
contain is a powerful performance tool that isolates layout, painting, size, and style effects. Used wisely, it makes complex UIs smoother and prevents costly reflows.
- contain: layout β layout isolation
- contain: paint β no painting overflow
- contain: content β full containment (layout + style + paint)
- contain: strict β maximum isolation