Use CSS contain to tell the browser which kinds of work inside a component can be treated independently from the rest of the page. Start with the smallest value that matches your goal—often layout or content—and test for changes to overflow, positioning, and stacking. Use size or strict only when the component has an appropriate independent size.
What CSS containment does
The MDN reference describes contain as indicating that an element and its contents are, as much as possible, independent from the rest of the document tree. That declaration lets the browser limit certain layout, style, paint, or size calculations to the component subtree.
As an Amazon Associate I earn from qualifying purchases.
Containment is a statement about how a component behaves, not a universal performance switch. It may reduce the work needed for some updates, but the benefit depends on the page and the kind of change. If a component’s own dimensions depend on its children, changing those children can still require recalculating the component’s size. Profile the page you are optimizing rather than assuming a particular speedup.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallChoose the value that matches the component
| Value | What it contains | When to consider it |
|---|---|---|
none |
No containment. | Use to remove containment or leave it unapplied. |
layout |
Internal layout is isolated from the rest of the page. | When the component’s layout should be independent, without size containment. |
paint |
Descendant painting is constrained by the containing box; overflow is clipped. | Only when clipping content outside the component is intended. |
size |
The element’s inline and block sizes are calculated independently of its children. | When the component can have a useful size without relying on its content. Do not combine with inline-size. |
inline-size |
The inline size is calculated independently of child content. | When only inline-size independence is wanted. Do not combine with size. |
style |
Scopes relevant effects such as CSS counters. | For those style-related containment effects; it is not general CSS scoping like Shadow DOM or @scope. |
content |
Shorthand for layout paint style; it excludes size containment. |
When those three forms of containment are desired without size containment. |
strict |
Shorthand for size layout paint style. |
When the strongest listed containment is appropriate and the component can be sized independently of its children. |
You can combine compatible values, for example contain: layout paint or contain: inline-size layout. Choose the smallest set that satisfies the component boundary you intend to establish.
#1 Best Overall
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
Apply containment in CSS
Set the property on the element whose subtree you want to isolate. For example, this component uses layout containment while retaining content-driven sizing:
.card-list-item {
contain: layout;
}
If paint containment is appropriate and clipping is intended, you could instead write:
.card-list-item {
contain: layout paint;
}
For size containment, give the element a useful size or provide an intrinsic placeholder size. Otherwise, its dimensions may not reflect its children. For example:
Rank #2
.deferred-panel {
contain: size layout paint;
contain-intrinsic-size: 20rem 12rem;
}
The values in this example are illustrative CSS dimensions, not recommended defaults. Choose dimensions that fit the component and confirm the resulting layout. The MDN contain-intrinsic-size reference describes the property used to supply a size when size containment applies.
Check for layout and visual side effects
Applying layout, paint, content, or strict creates a new containing block for absolutely and fixed-positioned descendants, a new stacking context, and a block formatting context. These changes can affect descendants even when the declaration appears to be a performance-only adjustment.
- Positioned descendants: Check menus, tooltips, dialogs, or other absolutely or fixed-positioned elements. Their positioning may now be relative to the containment boundary.
- Stacking: A new stacking context can change how descendants with
z-indexlayer against nearby page content. - Overflow: Paint containment clips descendants that extend outside the containing box. Check shadows, badges, and other intentional visual overflow.
- Size: With
sizeorstrict, verify the element still occupies the dimensions the design needs. Its size may be calculated as though it had no children.
Test the component in its normal and expanded states before shipping the change; a tooltip or menu that opens beyond the component boundary is an especially useful case to check.
Rank #3
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
Understand related containment properties
contain, content-visibility, and contain-intrinsic-size address related but distinct concerns. Containment declares which aspects of a subtree are independent. content-visibility controls whether an element’s contents are rendered and can let the browser defer rendering work until needed. contain-intrinsic-size supplies a placeholder size where size containment applies. See MDN’s guide to using CSS containment for further context.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsBrowser support
MDN marks contain as Baseline and widely available, with browser availability dating to March 2022. Its reference follows the latest syntax in CSS Containment Module Level 2 and notes that not every part may be implemented in every browser. Check the specific values you use against your project’s browser targets.
Troubleshoot common surprises
A tooltip or menu appears in the wrong place
Check whether an ancestor now establishes a containing block for positioned descendants. Review the containing element and the positioning rules for the overlay; if it needs to escape that boundary, reconsider where containment is applied or where the overlay is mounted.
Rank #4
An overlay appears behind another component
Containment may have created a stacking context. A descendant’s z-index cannot by itself place it above content in a different stacking context. Inspect the relevant ancestor contexts and adjust the component boundary or layer structure.
A shadow or decoration is cut off
paint, and shorthands that include it, clip paint outside the containing box. Remove paint containment if that overflow is required, or redesign the decoration to remain within the boundary.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →The component collapses or has an unexpected size
Check for size or strict. Those values make size independent of child content. Supply an appropriate size or use contain-intrinsic-size where a placeholder is useful; otherwise choose a value that does not include size containment.
Best Value
The change does not make updates visibly faster
Containment does not guarantee a measurable gain. Confirm that the targeted updates involve work the chosen containment can isolate, then profile the relevant interaction. Avoid adding stronger containment if it does not serve the component’s behavior.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Or skip the browser setup
If you need a screenshot of a page to inspect a CSS change, ScreenshotNeo can return one with a single request. For example, use cURL to save a WebP capture:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo API documentation for request options. Cookie banners, popups, and chat widgets are removed before the shot; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Sign up for ScreenshotNeo.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Frequently Asked Questions
Can I combine `size` and `inline-size`?
No. The two values are mutually exclusive.
Does `contain: style` scope all CSS rules to the element?
No. It scopes relevant effects such as CSS counters; it is not general style scoping like Shadow DOM or `@scope`.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




