CSS Portal

CSS contain Property

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The contain property is a way to tell the browser that an element and its subtree can be treated as a self-contained unit for layout, style, paint, and intrinsic sizing decisions. By marking an element as contained, you signal that changes inside that element should not force the UA (user agent) to re-evaluate or re-paint outside the element, and likewise that the outside should not be used when computing certain properties for the inside. This lets the rendering engine make stronger assumptions about boundaries and dependencies, which it can exploit to avoid expensive cross-tree work.

One of the main practical effects of using contain is improved rendering performance: the browser can restrict reflows, paints, and size calculations to the element’s subtree instead of propagating them through ancestor and sibling trees. Because of that, containment is often used on complex widgets, isolated UI components, or large lists where you want localized updates to remain cheap. It is complementary to hints like will-change (which signals upcoming changes) but is declarative about isolation rather than predictive about specific upcoming changes.

Containment also changes how certain visual and layout interactions behave. For example, clipping and paint boundaries may be constrained (so content can’t visually leak out of the element), stacking contexts and compositing boundaries can be established, and intrinsic size calculations may be done using only the element’s subtree. Because of those effects, containment can influence behaviors tied to positioning and overflow - see position and overflow - as well as interactions with transforms and compositing, such as when you use transform to create a new rendering context.

In practice, use containment when you want clear component boundaries and predictable, efficient rendering for a subtree. It’s particularly useful for self-contained widgets, virtualized content, or elements that do not rely on external layout or style information. Be mindful that making an element contained can change how descendants relate to ancestors (for example, their ability to affect outside layout, to overflow visibly, or to participate in outside stacking contexts), so test visual and interactive behavior after applying containment.

Definition

Initial value
none
Applies to
all elements
Inherited
no
Computed value
as specified
Animatable
no
JavaScript syntax
object.style.contain

Syntax

contain: none | strict | content | [ size || layout || style || paint ]

Values

  • noneThis value indicates that the property has no effect. The element renders as normal, with no containment effects applied.
  • strictThis value computes to size layout paint style, and thus turns on all forms of containment for the element.
  • contentThis value computes to layout paint style, and thus turns on all forms of containment except size containment for the element.

Example

<div class='wrap'>
<h1>CSS contain property - examples</h1>

<div class='panel size'>
<h2>contain: size</h2>
<div class='box large'>
This is very wide content that would normally expand the panel.
</div>
</div>

<div class='panel layout'>
<h2>contain: layout</h2>
<div class='box offset'>
Absolutely positioned child (moved outside).
</div>
</div>

<div class='panel paint'>
<h2>contain: paint</h2>
<div class='box shadow'>
Heavy shadows should be isolated.
</div>
</div>
</div>
* {
    box-sizing: border-box;
}

body {
    font-family: 'system-ui', -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
    background: #f6f8fa;
    color: #222;
    padding: 20px;
}

.wrap {
    display: flex;
    gap: 20px;
    align-items: flex-start;
    flex-wrap: wrap;
}

.panel {
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 8px;
    padding: 12px;
    width: 280px;
    height: 140px;
    overflow: visible;
    position: relative;
    box-shadow: 0 1px 0 rgba(0, 0, 0, 0.03);
}

.panel h2 {
    font-size: 14px;
    margin: 0 0 8px 0;
}

.box {
    background: #eef6ff;
    padding: 8px 10px;
    border-radius: 6px;
}

/* Size containment: the panel's used size is independent from its children's size */
.panel.size {
    contain: size;
}

.panel.size .box.large {
    width: 600px;
}

/* Layout containment: internal layout won't affect outside layout */
.panel.layout {
    contain: layout;
}

.panel.layout .box.offset {
    position: absolute;
    right: -40px;
    top: 40px;
    background: #fff7e6;
}

/* Paint containment: painting effects (shadows, overflow) are isolated */
.panel.paint {
    contain: paint;
}

.panel.paint .box.shadow {
    box-shadow: 0 40px 60px rgba(0, 0, 0, 0.25);
    transform: translateY(0);
}

Browser Support

The following information will show you the current browser support for the CSS contain property. Hover over a browser icon to see the version that first introduced support for this CSS property.

This property is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!