CSS contain Property
The contain property limits how an element and its descendants affect other parts of the page by applying layout, paint, style, or size containment.
Containment allows the browser to make assumptions about an element's independence from surrounding content. These boundaries can reduce unnecessary rendering work, particularly in pages containing many independent components.
The property supports individual containment types and the combined keywords content and strict. Each type has different effects on sizing, positioning, painting, and document behavior.
contain Syntax
The contain property accepts a predefined keyword or a space-separated combination of containment types.
selector {
contain: layout;
}
General Syntax
selector {
contain: none | strict | content | [ size || inline-size || layout || style || paint ];
}
The double vertical bars (||) indicate that containment types can be combined in any order. However, size and inline-size cannot appear together.
No Containment
.component {
contain: none;
}
Size Containment
.component {
contain: size;
}
Inline-Size Containment
.component {
contain: inline-size;
}
Layout Containment
.component {
contain: layout;
}
Paint Containment
.component {
contain: paint;
}
Style Containment
.component {
contain: style;
}
Combined Containment
.component {
contain: layout paint;
}
Content Containment
.component {
contain: content;
}
Strict Containment
.component {
contain: strict;
}
CSS-Wide Keywords
.component {
contain: inherit;
}
.component {
contain: initial;
}
.component {
contain: unset;
}
.component {
contain: revert;
}
.component {
contain: revert-layer;
}
The keywords none, content, and strict must be used independently rather than combined with individual containment types.
Property Values
The contain property supports the following values:
| Value | Description |
|---|---|
none |
Applies no containment. This is the initial value. |
size |
Prevents descendant content from influencing the element's intrinsic sizing in either dimension. |
inline-size |
Applies size containment only in the inline dimension. |
layout |
Establishes an independent formatting context and limits certain layout interactions with surrounding content. |
paint |
Clips descendant painting at the containment boundary and isolates certain painting effects. |
style |
Scopes specified style effects, including CSS counters and quotation-related behavior. |
content |
Equivalent to layout paint style. |
strict |
Equivalent to size layout paint style. |
layout paint |
Combines layout and paint containment. |
inline-size layout |
Combines inline-size and layout containment. |
CSS-Wide Keywords
| Value | Description |
|---|---|
inherit |
Uses the computed containment value of the parent element. |
initial |
Resets the property to none. |
unset |
Resets the property to none because it is not inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | Elements that generate principal boxes, with restrictions for certain box types and containment modes |
| Computed value | Specified containment keywords |
| Animation type | Discrete |
| Percentages | Not applicable |
| Layout effect | Depends on the selected containment types |
Containment values are not interchangeable. Each type establishes different restrictions, and some values can significantly change the element's layout or visual appearance.
How It Works
Browsers normally calculate layout and rendering using relationships between elements and their descendants. Containment introduces boundaries that limit certain dependencies.
Step 1: Create an Independent Component
<div class="component">
<h3>CSS Reference</h3>
<p>Learn how CSS properties control presentation.</p>
</div>
Step 2: Apply Containment
.component {
contain: layout;
}
Step 3: Establish the Boundary
Layout containment establishes an independent formatting context and prevents certain descendant layout effects from influencing surrounding content.
Step 4: Add Paint Containment
.component {
contain: layout paint;
}
Paint containment adds clipping and painting isolation to the layout boundary.
Step 5: Add Size Containment When Appropriate
.component {
width: 300px;
min-height: 180px;
contain: size layout paint;
}
Size containment prevents descendant content from contributing to intrinsic size calculations. An explicit or otherwise established size helps avoid unexpected collapse.
Important Distinction
Containment does not automatically make every rendering operation faster. It provides boundaries that browsers may use to optimize work, while also enforcing specific layout and painting behavior.
none
The none value disables containment and is the initial value of the property.
.component {
contain: none;
}
Normal Layout Behavior
Without containment, descendant content can influence the element's intrinsic dimensions and participate in normal layout relationships.
Resetting Containment
.component {
contain: layout paint;
}
.component.no-containment {
contain: none;
}
When to Use
Use none when containment is unnecessary or when a component must allow normal sizing, positioning, or painting behavior.
size
The size value applies size containment in both dimensions, preventing descendant content from influencing the element's intrinsic sizing.
.component {
contain: size;
}
How Size Containment Works
For intrinsic sizing purposes, the browser treats the contained element as though its contents do not contribute to its size.
Explicit Dimensions
.component {
width: 320px;
height: 200px;
contain: size;
}
Using Minimum Dimensions
.component {
min-height: 180px;
contain: size;
}
Possible Size Collapse
An auto-sized element with size containment may collapse in one or both dimensions because its descendants no longer determine its intrinsic size.
Content Overflow
Size containment alone does not automatically clip overflowing content. Descendants may still paint outside the element unless another property or containment mode restricts them.
When to Use
Use size containment when a component's dimensions can be determined independently of its contents.
For elements whose block size should still depend on their contents, consider inline-size containment instead.
inline-size
The inline-size value applies size containment only in the inline dimension.
.component {
contain: inline-size;
}
Inline Dimension
In horizontal writing modes, the inline dimension generally corresponds to width. In vertical writing modes, it generally corresponds to height.
Block Dimension
Unlike full size containment, inline-size containment allows descendant content to contribute to the element's intrinsic block size.
Basic Example
.component {
width: 100%;
contain: inline-size;
}
Combining with Layout Containment
.component {
contain: inline-size layout;
}
Writing Modes
.vertical-component {
writing-mode: vertical-rl;
contain: inline-size;
}
The containment dimension follows the element's writing mode rather than always referring to physical width.
When to Use
Use inline-size when the component's inline dimension should be independent of its descendants but its block dimension should remain content-dependent.
Inline-size containment is also associated with size container queries through the container-type property.
layout
The layout value establishes a layout containment boundary around an element's contents.
.component {
contain: layout;
}
Independent Formatting Context
Layout containment establishes an independent formatting context, limiting certain interactions between descendants and surrounding layout.
Containing Block
Layout containment establishes a containing block for absolutely positioned and fixed-position descendants.
.component {
contain: layout;
}
.component .positioned-child {
position: absolute;
top: 10px;
right: 10px;
}
Float Containment
Floats inside a layout-contained element do not escape to influence the layout of surrounding content.
.component {
contain: layout;
}
.component img {
float: left;
margin-right: 15px;
}
Margin Behavior
Layout containment prevents the contained element's margins from collapsing with its descendants' margins.
Stacking Context
Layout containment creates a stacking context, affecting how descendant elements participate in stacking and painting order.
When to Use
Use layout containment for independent interface components that should not affect surrounding layout through floats, margin collapsing, or positioning relationships.
Layout containment does not, by itself, clip descendant painting.
paint
The paint value prevents descendant content from painting outside the element's containment boundary.
.component {
contain: paint;
}
Paint Clipping
Descendant painting is clipped to the element's padding edge, with clipping adjusted by any applicable border-radius.
Basic Example
.component {
width: 300px;
height: 180px;
contain: paint;
}
Overflowing Child
.component {
width: 300px;
height: 180px;
contain: paint;
}
.component .child {
width: 400px;
}
The child's painted content outside the containment boundary is clipped.
Stacking Context
Paint containment creates a stacking context, isolating descendant stacking behavior.
Containing Block
Paint containment also establishes a containing block for absolutely positioned and fixed-position descendants.
Difference from overflow: hidden
Paint containment is not simply a replacement for overflow: hidden. It establishes containment-related layout and painting behavior, including a stacking context and positioning effects.
When to Use
Use paint containment when a component should not visually affect areas outside its own boundary.
Avoid it when menus, tooltips, or other descendants need to extend beyond the component's edges.
style
The style value scopes certain CSS effects that would otherwise influence content outside the contained element.
.component {
contain: style;
}
CSS Counters
Style containment limits the propagation of CSS counter effects across the containment boundary.
.component {
contain: style;
}
.component li {
counter-increment: item;
}
Quotation Behavior
Style containment also isolates quotation-related effects associated with generated content.
What Style Containment Does Not Do
Style containment does not prevent ordinary CSS inheritance, selector matching, or descendant styling.
No General Style Isolation
It does not create a Shadow DOM boundary or prevent external stylesheets from applying to descendants.
Combining with Layout Containment
.component {
contain: layout style;
}
When to Use
Style containment is useful when specific counter or quotation effects should remain local to a component.
Browser implementations of standalone style containment have historically been limited. Test compatibility before relying on its isolation behavior.
content
The content keyword combines layout, paint, and style containment.
.component {
contain: content;
}
Equivalent Containment Types
.component {
contain: layout paint style;
}
Layout Isolation
The component establishes an independent formatting context and limits certain layout interactions with surrounding elements.
Paint Isolation
Descendant painting is clipped to the containment boundary.
Style Effects
Specified style effects, such as CSS counter behavior, are scoped to the contained component.
Content-Dependent Sizing
Unlike strict, the content keyword does not include size containment. The element can continue to size itself based on its contents.
Basic Component
.card {
padding: 20px;
border: 1px solid #cccccc;
contain: content;
}
When to Use
Use content for independent components that should retain content-dependent sizing while benefiting from layout and paint containment.
Check whether any descendant needs to extend visually outside the component before applying this value.
strict
The strict keyword applies size, layout, paint, and style containment together.
.component {
contain: strict;
}
Equivalent Containment Types
.component {
contain: size layout paint style;
}
Size Independence
Descendant content does not contribute to the element's intrinsic size calculations.
Layout Isolation
The element establishes a layout containment boundary and an independent formatting context.
Paint Isolation
Descendant painting is clipped to the containment boundary.
Style Effects
Specified style effects are scoped within the component.
Explicitly Sized Component
.component {
width: 320px;
height: 200px;
contain: strict;
}
Using a Minimum Height
.component {
min-height: 200px;
contain: strict;
}
Risk of Unexpected Sizing
Without suitable dimensions, strict containment may cause an auto-sized element to collapse because its contents do not contribute to its intrinsic size.
When to Use
Use strict when the component's dimensions are independent of its contents and full containment is appropriate.
Avoid strict containment on ordinary content sections that need to expand naturally with their descendants.
Combining Containment Values
Individual containment types can be combined to establish the boundaries needed by a component.
Layout and Paint
.component {
contain: layout paint;
}
Layout and Style
.component {
contain: layout style;
}
Size and Layout
.component {
contain: size layout;
}
Inline-Size and Layout
.component {
contain: inline-size layout;
}
Layout, Paint, and Style
.component {
contain: layout paint style;
}
This combination corresponds to contain: content.
All Four Types
.component {
contain: size layout paint style;
}
This combination corresponds to contain: strict.
Invalid Combination
.component {
contain: size inline-size;
}
The size and inline-size keywords cannot be combined in the same declaration.
Invalid Keyword Combination
.component {
contain: content paint;
}
The content and strict keywords cannot be combined with individual containment values.
Practical Consideration
Apply only the containment types needed by the component. Additional containment can introduce layout, positioning, or clipping behavior that the design does not require.
Containment and Element Sizing
Containment can affect how an element's dimensions are calculated, particularly when size containment is enabled.
Normal Content Sizing
.component {
contain: none;
}
Without size containment, descendants can contribute to the element's intrinsic dimensions.
Full Size Containment
.component {
contain: size;
}
Descendants no longer contribute to intrinsic sizing in either dimension.
Inline-Size Containment
.component {
contain: inline-size;
}
Descendants do not contribute to intrinsic inline sizing, but they may continue to contribute to block sizing.
Explicit Width and Height
.component {
width: 320px;
height: 200px;
contain: size;
}
Intrinsic Size Fallback
.component {
contain: size;
contain-intrinsic-size: 320px 200px;
}
The contain-intrinsic-size property provides intrinsic size values that can be used when size containment prevents descendants from contributing to intrinsic sizing.
Block-Size Fallback
.component {
contain: size;
contain-intrinsic-block-size: 200px;
}
Inline-Size Fallback
.component {
contain: inline-size;
contain-intrinsic-inline-size: 320px;
}
Practical Consideration
When applying size containment, ensure the element has suitable dimensions or intrinsic size fallback values so its layout remains predictable.
Positioning and Stacking Contexts
Layout and paint containment affect positioned descendants and stacking behavior.
Absolutely Positioned Descendants
.component {
contain: layout;
}
.component .badge {
position: absolute;
top: 10px;
right: 10px;
}
The contained element establishes a containing block for the positioned descendant.
Fixed-Position Descendants
.component {
contain: paint;
}
.component .fixed-child {
position: fixed;
top: 0;
right: 0;
}
Paint containment establishes a containing block for fixed-position descendants, so they may be positioned relative to the contained element rather than the viewport.
Stacking Context
.component {
contain: layout;
}
.component .overlay {
position: absolute;
z-index: 100;
}
Layout containment creates a stacking context. A descendant's z-index cannot place it above unrelated stacking contexts that are painted above its containing context.
Paint Containment
.component {
contain: paint;
}
Paint containment also creates a stacking context and clips descendant painting.
Dropdown Menus
Dropdown menus that must extend outside a component may be clipped by paint containment.
Tooltips and Popovers
Test tooltips and popovers carefully when their containing elements use layout or paint containment. Positioning and clipping behavior may change.
Practical Consideration
Containment is not a substitute for position: relative, and its effects on containing blocks and stacking contexts should be considered before applying it to interactive components.
Overflow and Clipping
Paint containment restricts descendant painting outside the element's containment boundary.
Paint Containment
.component {
width: 300px;
height: 180px;
contain: paint;
}
Overflowing Content
.component {
width: 300px;
contain: paint;
}
.component .wide-content {
width: 450px;
}
The overflowing descendant is clipped at the containment boundary.
Layout Containment Without Paint Containment
.component {
contain: layout;
}
Layout containment alone does not impose paint clipping.
Content Containment
.component {
contain: content;
}
The content keyword includes paint containment, so clipping applies.
Strict Containment
.component {
width: 300px;
height: 180px;
contain: strict;
}
Strict containment includes paint clipping and full size containment.
Border Radius
.component {
border-radius: 12px;
contain: paint;
}
Paint clipping follows the containment boundary, adjusted by the element's border radius.
Practical Consideration
Do not apply paint containment to a component if essential content must extend beyond its edges, unless that content uses a mechanism that can escape the clipping boundary.
Performance Considerations
Containment can help browsers avoid unnecessary layout and painting work by limiting dependencies between components.
Independent Components
.dashboard-panel {
contain: layout paint;
}
Independent dashboard panels may benefit when changes inside one panel do not require equivalent work elsewhere.
Repeated Components
.reference-card {
contain: content;
}
Repeated components can be suitable containment candidates when their layout and painting are independent of neighboring elements.
Known-Size Components
.preview-panel {
width: 320px;
height: 220px;
contain: strict;
}
Components with independently determined dimensions may be suitable for full containment.
Avoid Unnecessary Containment
Applying containment everywhere is not a reliable performance strategy. It may introduce extra formatting contexts, stacking contexts, clipping, or sizing restrictions.
Measure Performance
Use browser developer tools to compare layout and rendering behavior before and after applying containment.
Containment Is Not Virtualization
The contain property does not automatically skip rendering off-screen content or remove elements from the document.
Practical Consideration
Containment works best when applied to components that genuinely have limited dependencies on surrounding content.
contain vs. content-visibility
The contain and content-visibility properties are related, but they serve different purposes.
contain
.component {
contain: layout paint;
}
The contain property explicitly establishes containment boundaries.
content-visibility
.component {
content-visibility: auto;
}
The content-visibility: auto value allows the browser to skip rendering work for content that is not currently relevant to the user, while applying associated containment behavior.
Intrinsic Size Fallback
.component {
content-visibility: auto;
contain-intrinsic-size: auto 300px;
}
The intrinsic size fallback can help reserve space while rendering is skipped. The auto form allows a previously remembered size to be used when available.
Key Differences
| Property | Primary Purpose |
|---|---|
contain |
Establishes explicit layout, paint, style, or size containment boundaries. |
content-visibility |
Controls whether the browser can skip rendering work for an element's contents. |
contain-intrinsic-size |
Provides intrinsic size fallback values for contained elements. |
Practical Consideration
Use contain when you need specific containment boundaries. Use content-visibility when the goal is to allow rendering work to be skipped for suitable content.
Practical Uses
The contain property is useful for components that can operate independently of surrounding page content.
Dashboard Panels
.dashboard-panel {
contain: layout paint;
}
Reference Cards
.reference-card {
contain: content;
}
Fixed-Size Previews
.preview {
width: 320px;
height: 220px;
contain: strict;
}
Independent Layout Sections
.layout-section {
contain: layout;
}
Clipped Visual Components
.visual-preview {
width: 300px;
height: 180px;
contain: paint;
}
Inline-Size Containment
.responsive-component {
contain: inline-size;
}
Size Containment with Fallback
.reserved-panel {
contain: size;
contain-intrinsic-size: 300px 200px;
}
When Not to Use
Avoid containment when descendants must influence surrounding layout, determine the element's intrinsic dimensions, or extend visually beyond its boundary in ways the selected containment mode would prevent.
contain Example
The following example compares normal layout behavior, layout containment, and paint containment.
Each example contains a floated element and text. The layout containment example establishes an independent formatting context, while the paint containment example also clips painting outside its boundary.
<div class="contain-demo">
<div class="contain-example">
<h3>contain: none;</h3>
<div class="contain-box contain-none">
<div class="float-box">Float</div>
<p>This component uses normal layout behavior without containment.</p>
</div>
</div>
<div class="contain-example">
<h3>contain: layout;</h3>
<div class="contain-box contain-layout">
<div class="float-box">Float</div>
<p>Layout containment establishes an independent formatting context.</p>
</div>
</div>
<div class="contain-example">
<h3>contain: paint;</h3>
<div class="contain-box contain-paint">
<div class="float-box">Float</div>
<p>Paint containment clips content outside the component boundary.</p>
</div>
</div>
</div>
.contain-demo {
display: grid;
gap: 20px;
}
.contain-demo .contain-example {
padding: 15px;
border: 1px solid #cccccc;
background-color: #f8f9fa;
}
.contain-demo h3 {
margin: 0 0 12px;
font-size: 1.125rem;
}
.contain-demo .contain-box {
padding: 15px;
border: 2px solid #336699;
background-color: #ffffff;
}
.contain-demo .contain-box p {
margin: 0;
}
.contain-demo .float-box {
float: left;
width: 80px;
height: 60px;
margin-right: 15px;
padding: 10px;
background-color: #dceaf7;
border: 1px solid #336699;
text-align: center;
}
.contain-demo .contain-none {
contain: none;
}
.contain-demo .contain-layout {
contain: layout;
}
.contain-demo .contain-paint {
contain: paint;
}
@media (max-width: 600px) {
.contain-demo .float-box {
width: 65px;
height: 55px;
margin-right: 10px;
}
}
contain: none;
This component uses normal layout behavior without containment.
contain: layout;
Layout containment establishes an independent formatting context.
contain: paint;
Paint containment clips content outside the component boundary.
The first example uses contain: none, allowing normal layout behavior. The floated box may extend below its parent if the remaining content is too short to contain it.
The second example uses contain: layout, establishing an independent formatting context that includes the floated element when determining the container's height.
The third example uses contain: paint, which establishes paint containment and associated layout isolation. Descendant painting is clipped to the containment boundary.
The differences are most noticeable when the content is short enough for the floated element to extend beyond the normal text height.
Accessibility
The contain property primarily affects layout and rendering, but its visual consequences can influence accessibility.
Avoid Clipping Essential Content
Paint containment can clip content that extends outside a component, including focus indicators, tooltips, and other interface elements.
Preserve Visible Focus Indicators
Test keyboard navigation to ensure focused elements remain visually identifiable when containment is applied.
Prevent Content Overlap
Size containment may prevent a component from expanding to fit its contents. Provide suitable dimensions or intrinsic size fallbacks to avoid overlap.
Support Text Resizing
Test contained components with enlarged text and increased browser zoom to ensure content remains readable.
Maintain Logical Document Structure
Containment does not replace semantic HTML or change the intended reading order of the document.
Test Interactive Components
Check dropdown menus, dialogs, tooltips, and positioned elements because containment can affect clipping and containing blocks.
Content Visibility Considerations
When combining containment-related techniques with content-visibility, test accessibility behavior to ensure important content remains available when needed.
Practical Consideration
Use containment only when its sizing and painting restrictions are compatible with the component's content and interactions.
Important Considerations
Keep the following points in mind when using contain:
- Purpose: Establishes boundaries for layout, paint, style, and size containment.
- Initial value:
none. - Inheritance: The property is not inherited.
- Animation: Discrete.
- none: Applies no containment.
- size: Prevents descendants from contributing to intrinsic sizing in both dimensions.
- inline-size: Prevents descendants from contributing to intrinsic inline sizing.
- layout: Establishes an independent formatting context and limits layout interactions.
- paint: Clips descendant painting at the containment boundary.
- style: Scopes specified style effects, including counter and quotation behavior.
- content: Equivalent to
layout paint style. - strict: Equivalent to
size layout paint style. - Value combinations: Individual containment types may be combined in any order.
- Invalid combination:
sizeandinline-sizecannot be combined. - Standalone keywords:
none,content, andstrictcannot be mixed with other containment values. - Intrinsic sizing: Size containment can cause unexpected dimensions if no suitable size is established.
- Intrinsic size fallback:
contain-intrinsic-sizecan provide fallback dimensions. - Containing blocks: Layout and paint containment establish containing blocks for positioned descendants.
- Stacking contexts: Layout and paint containment create stacking contexts.
- Clipping: Paint containment can hide content outside the component boundary.
- Floats: Layout containment prevents floats from affecting surrounding layout.
- Margin collapsing: Layout containment prevents margin collapsing between the container and its descendants.
- Performance: Containment can reduce unnecessary rendering work when applied appropriately.
- Not virtualization:
containdoes not automatically skip rendering off-screen content. - Related property:
content-visibilitycan allow the browser to skip rendering work for suitable content. - Compatibility: Individual containment types may have different levels of browser support.
- Accessibility: Test clipping, text resizing, focus indicators, and interactive descendants.
Use the least restrictive containment mode that meets the component's needs. Stronger containment is not automatically better and can introduce unintended layout or painting effects.
Browser Support
The contain property is supported in modern browsers, including Chrome, Edge, Firefox, and Safari, although support for individual containment types and combinations may vary.
Layout, paint, size, and combined containment are broadly available in current browsers. Standalone style containment has historically had more limited implementation support.
Basic Example
.component {
contain: layout paint;
}
Content Containment
.component {
contain: content;
}
Strict Containment
.component {
width: 320px;
height: 200px;
contain: strict;
}
Feature Detection
@supports (contain: layout) {
.component {
contain: layout;
}
}
Feature queries can check whether a browser accepts a containment declaration, but they do not guarantee that every associated behavior is implemented identically.
For current compatibility details, visit Can I Use? and search for CSS containment.
Specifications
The contain property is defined in the CSS Containment specifications.
CSS Containment Module Level 2
CSS Containment Module Level 3
These specifications define containment types, combined keywords, sizing behavior, rendering boundaries, and related containment features.
