October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Use the CSS `contain` Property

CSS `contain` can isolate parts of a component from the rest of the page. Learn which value to choose, how to apply it, and what behavior to test.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose 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
Sale
HTML and CSS: Design and Build Websites
  • 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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
.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-index layer 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 size or strict, 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
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Browser 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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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`.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.