Title: CSS Containment Module Level 1
Level: 1
Shortname: css-contain
Status: ED
Work Status: completed
Group: csswg
ED: https://drafts.csswg.org/css-contain-1/
TR: https://www.w3.org/TR/css-contain-1/
Previous Version: https://www.w3.org/TR/2019/PR-css-contain-1-20191015/
Previous Version: https://www.w3.org/TR/2019/CR-css-contain-1-20190430/
Previous Version: https://www.w3.org/TR/2018/CR-css-contain-1-20181108/
Previous Version: https://www.w3.org/TR/2018/CR-css-contain-1-20180524/
Previous Version: https://www.w3.org/TR/2017/CR-css-contain-1-20170808/
Previous Version: https://www.w3.org/TR/2017/WD-css-contain-1-20170419/
Previous Version: https://www.w3.org/TR/2017/WD-css-contain-1-20170221/
Editor: Tab Atkins, Google, http://xanthir.com/contact/, w3cid 42199
Editor: Florian Rivoal, On behalf of Bloomberg, https://florian.rivoal.net/, w3cid 43241
Abstract: This CSS module describes the 'contain' property, which indicates that the element's subtree is independent of the rest of the page. This enables heavy optimizations by user agents when used well.
Status Text: A Disposition of Comments and an Implementation Report are available.
WPT Path Prefix: css/css-contain/
Include MDN Panels: no
spec: css2; urlPrefix: https://www.w3.org/TR/CSS2/
	type: property; url: visudet.html#propdef-vertical-align; text: vertical-align
	type: dfn; url: box.html#padding-edge; text: padding edge

spec: css-backgrounds-3;
	type: dfn; url: https://drafts.csswg.org/css-backgrounds-3/#corner-clipping; text: corner clipping

Introduction

Efficiently rendering a website relies on the User Agent being able to detect what parts of the page are being displayed, which parts might affect the currently-displayed section, and what can be ignored. There are various heuristics that can be used to guess when a given sub-tree is independent of the rest of the page in some manner, but they're fragile, so innocuous changes to a page may inadvertently make it fail such heuristic tests, causing rendering to fall into a slow code path. There are also many things that would be good to isolate which are difficult or impossible to detect in a heuristic manner. To alleviate these problems and allow strong, predictable isolation of a subtree from the rest of the page, this specification defines a 'contain' property.

Value Definitions

This specification follows the CSS property definition conventions from [[!CSS2]] using the value definition syntax from [[!CSS-VALUES-3]]. Value types not defined in this specification are defined in CSS Values & Units [[!CSS-VALUES-3]]. Combination with other CSS modules may expand the definitions of these value types. In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords as their property value. For readability they have not been repeated explicitly.

Strong Containment: the 'contain' property

		Name: contain
		Value: none | strict | content | [ size || layout || paint ]
		Initial: none
		Inherited: no
		Applies to: See below
		Computed value: specified keyword(s), except ''strict'' and ''content'' (see prose)
		Animation type: not animatable
	
contain-animation-001.html inheritance.html parsing/contain-computed.html parsing/contain-invalid.html parsing/contain-valid.html contain-layout-size-003.html contain-paint-size-001.html contain-paint-size-002.html contain-paint-size-003.html contain-chrome-thcrash-001.html

User Agents are expected to support this property on all media, including non-visual ones.

The 'contain' property allows an author to indicate that an element and its contents are, as much as possible, independent of the rest of the document tree. This allows user agents to utilize much stronger optimizations when rendering a page using 'contain' properly, and allows authors to be confident that their page won't accidentally fall into a slow code path due to an innocuous change.
none
This value indicates that the property has no effect. The element renders as normal, with no containment effects applied.
strict
This value computes to ''size layout paint'', and thus turns on all forms of containment for the element. contain-strict-001.html contain-strict-002.html contain-strict-003.html
content
This value computes to ''layout paint'', and thus turns on all forms of containment except size containment for the element. contain-content-001.html contain-content-002.html contain-content-003.html contain-content-004.html Note: ''contain: content'' is reasonably "safe" to apply widely; its effects are fairly minor in practice, and most content won't run afoul of its restrictions. However, because it doesn't apply size containment, the element can still respond to the size of its contents, which can cause layout-invalidation to percolate further up the tree than desired. Use ''contain: strict'' when possible, to gain as much containment as you can.
size
The value turns on size containment for the element. This ensures that the [=size containment box|containment box=] can be laid out without needing to examine its descendants. contain-size-001.html contain-size-002.html contain-size-003.html contain-size-004.html contain-size-005.html contain-size-006.html contain-size-007.html contain-size-008.html contain-size-009.html contain-size-010.html contain-size-011.html contain-size-012.html contain-size-012b.html contain-size-013.html contain-size-021.html contain-size-023.html contain-size-025.html contain-size-027.html contain-size-041.html contain-size-042.html contain-size-051.html contain-size-052.html contain-size-056.html contain-size-061.html contain-size-062.html contain-size-063.html contain-size-baseline-001.html contain-size-borders.html contain-size-breaks-001.html contain-size-button-001.html contain-size-fieldset-001.html contain-size-fieldset-002.html contain-size-flexbox-001.html contain-size-flexbox-002.html contain-size-grid-001.html contain-size-grid-002.html contain-size-monolithic-001.html contain-size-multicol-001.html contain-size-multicol-as-flex-item.html contain-size-replaced-001.html contain-size-replaced-002.html contain-size-replaced-003a.html contain-size-replaced-003b.html contain-size-replaced-003c.html contain-size-replaced-004.html contain-size-replaced-005.html contain-size-replaced-006.html contain-size-select-001.html contain-size-select-002.html contain-size-scrollbars-001.html contain-size-scrollbars-002.html contain-size-scrollbars-003.html contain-size-scrollbars-004.html contain-layout-size-003.html contain-paint-size-001.html contain-paint-size-002.html contain-paint-size-003.html
layout
This value turns on layout containment for the element. This ensures that the [=layout containment box|containment box=] is totally opaque for layout purposes; nothing outside can affect its internal layout, and vice versa. contain-layout-001.html contain-layout-002.html contain-layout-003.html contain-layout-004.html contain-layout-005.html contain-layout-006.html contain-layout-007.html contain-layout-009.html contain-layout-010.html contain-layout-011.html contain-layout-012.html contain-layout-013.html contain-layout-014.html contain-layout-016.html contain-layout-017.html contain-layout-018.html contain-layout-baseline-001.html contain-layout-baseline-002.html contain-layout-baseline-003.html contain-layout-baseline-004.html contain-layout-baseline-005.html contain-layout-breaks-001.html contain-layout-breaks-002.html contain-layout-button-001.html contain-layout-cell-001.html contain-layout-cell-002.html contain-layout-flexbox-001.html contain-layout-grid-001.html contain-layout-ifc-022.html contain-layout-independent-formatting-context-001.html contain-layout-independent-formatting-context-002.html contain-layout-independent-formatting-context-003.html contain-layout-ink-overflow-013.html contain-layout-ink-overflow-014.html contain-layout-ink-overflow-015.html contain-layout-ink-overflow-016.html contain-layout-ink-overflow-017.html contain-layout-ink-overflow-018.html contain-layout-ink-overflow-019.html contain-layout-ink-overflow-020.html contain-layout-size-003.html contain-subgrid-001.html
paint
This value turns on paint containment for the element. This ensures that the descendants of the [=paint containment box|containment box] don't display outside its bounds, so if an element is off-screen or otherwise not visible, its descendants are also guaranteed to be not visible. contain-paint-001.html contain-paint-002.html contain-paint-004.html contain-paint-005.html contain-paint-006.html contain-paint-007.html contain-paint-008.html contain-paint-009.html contain-paint-010.html contain-paint-011.html contain-paint-012.html contain-paint-014.html contain-paint-015.html contain-paint-016.html contain-paint-017.html contain-paint-018.html contain-paint-019.html contain-paint-020.html contain-paint-021.html contain-paint-022.html contain-paint-023.html contain-paint-024.html contain-paint-025.html contain-paint-047.html contain-paint-048.html contain-paint-baseline-001.html contain-paint-cell-001.html contain-paint-cell-002.html contain-paint-clip-011.html contain-paint-clip-012.html contain-paint-clip-013.html contain-paint-clip-014.html contain-paint-clip-015.html contain-paint-clip-016.html contain-paint-clip-017.html contain-paint-clip-018.html contain-paint-clip-019.html contain-paint-ifc-011.html contain-paint-independent-formatting-context-001.html contain-paint-independent-formatting-context-002.html contain-paint-independent-formatting-context-003.html contain-paint-size-001.html contain-paint-size-002.html contain-paint-size-003.html contain-paint-table-001.html contain-paint-table-002.html contain-subgrid-001.html
This property generally applies to all elements (including [[css-pseudo-4#generated-content]]), although some types of containment have no effect on some elements, as detailed in [[#containment-types]]. In addition, in the case of [[SVG2]], the 'contain' property only applies to <{svg}> elements that have an associated CSS layout box.
'contain' is useful when used widely on a page, particularly when a page contains a lot of "widgets" which are all independent. For example, assume a micropost social network had markup something like this:

			<body>
				<aside>...</aside>
				<section>
					<h2>Messages</h2>
					<article>
						Lol, check out this dog: images.example.com/jsK3jkl
					</article>
					<article>
						I had a ham sandwich today. #goodtimes
					</article>
					<article>
						I have political opinions that you need to hear!
					</article>
					…
				</section>
			</body>
		
There are probably a lot of messages displayed on the site, but each is independent and won't affect anything else on the site. As such, each can be marked with ''contain: content'' to communicate this to the user agent, so it can optimize the page and skip a lot of computation for messages that are off-screen. If the size of each message is known ahead of time, ''contain: strict'' can be applied to communicate further restrictions.

Types of Containment

There are several varieties of containment that an element can be subject to, restricting the effects that its descendants can have on the rest of the page in various ways. Containment enables much more powerful optimizations by user agents, and helps authors compose their page out of functional units, as it limits how widely a given change can affect a document. Advisement: Specification authors introducing new properties or mechanisms need to consider whether and how the various types of containment affect what they are introducing, and include in their specification any effect not described here.

Size Containment

If the element does not generate a principal box (as is the case with ''display: contents'' or ''display: none''), or its [=inner display type=] is ''display/table'', or its principal box is an internal table box, or an internal ruby box, or a non-atomic inline-level box, size containment has no effect. Otherwise, giving an element size containment makes its [=principal box=] a size containment box and has the following effects: contain-size-001.html contain-size-002.html contain-size-003.html contain-size-004.html contain-size-005.html contain-size-006.html contain-size-007.html contain-size-008.html contain-size-009.html contain-size-010.html contain-size-012.html contain-size-012b.html contain-size-051.html contain-size-052.html Note: Internal table boxes, which do not include table captions, are excluded, because the table layout algorithm does not allow boxes to become smaller than their inflow content. Sizing a table cell as if it was empty and then layout out its content inside without changing the size is effectively an undefined operation. Manually setting the 'width' or 'height' properties to ''0'' cannot make it smaller than its content. This concern does not apply to table captions, which are perfectly capable of having a fixed size that is independent of their content. contain-size-011.html contain-size-056.html 1. When calculating the size of the [=size containment box=], including when computing its [=intrinsic size=], it must be treated as having no contents. contain-size-021.html contain-size-023.html contain-size-025.html contain-size-027.html contain-size-061.html contain-size-062.html contain-size-063.html contain-size-borders.html contain-size-fieldset-001.html contain-size-fieldset-002.html contain-size-select-001.html contain-size-select-002.html contain-size-scrollbars-001.html contain-size-scrollbars-002.html contain-size-scrollbars-003.html contain-size-scrollbars-004.html contain-size-button-001.html contain-size-flexbox-001.html contain-size-flexbox-002.html contain-size-grid-001.html Note: Even when the element's [=sizing properties=] are ''height/auto'' this does not necessarily make the element zero-sized: properties set on the element itself, such as the 'columns' property or the 'grid' property, continue to be taken into account. contain-size-grid-002.html contain-size-grid-003.html contain-size-multicol-001.html contain-size-multicol-as-flex-item.html Then, its contents must then be laid out into the [=size containment box=]'s resolved size normally (including any pseudo-elements), i.e. [=size containment=] has no effect in this phase. Note: [=size containment=] does not suppress baseline alignment. See [=layout containment=] for that. contain-size-baseline-001.html Replaced elements must be treated as having an intrinsic width and height of 0. contain-size-013.html contain-size-041.html contain-size-042.html contain-size-replaced-001.html contain-size-replaced-002.html contain-size-replaced-003a.html contain-size-replaced-003b.html contain-size-replaced-003c.html contain-size-replaced-004.html contain-size-replaced-005.html contain-size-replaced-006.html 2. [=Size containment boxes=] must be treated as if they had no [=intrinsic aspect ratio=]. Note: Size containment only suppresses the [=intrinsic aspect ratio=], not [=preferred aspect ratio=], so properties like 'aspect-ratio' which affect that [=preferred aspect ratio=] directly are honored. replaced-element-023.html replaced-element-025.html replaced-element-027.html
Given the following markup and style, the image would be sized to 100px by 100px, as the aspect ratio set by the 'aspect-ratio' property takes effect.

			img {
				width: 100px;
				aspect-ratio: 1/1;
				contain: size;
			}
			
			<img src="https://www.example.com/300x100.jpg">
			
If the 'aspect-ratio' property had not been declared, the image would have been 100px by 0px, as its intrinsic aspect ratio is suppressed, and its intrinsic height is treated as 0.
3. [=Size containment boxes=] are monolithic (See [[CSS-BREAK-3#possible-breaks]]). contain-size-breaks-001.html contain-size-monolithic-001.html By itself, size containment does not offer much optimization opportunity. Its primary benefit on its own is that tools which want to lay out the [=size containment box|containment box=]'s contents based on the [=size containment box|containment box=]'s size (such as a JS library implementing the "container query" concept) can do so without fear of "infinite loops", where having a child's size respond to the size of the [=size containment box|containment box=] causes the [=size containment box|containment box=]'s size to change as well, possibly triggering further changes in how the child sizes itself and possibly thus more changes to the [=size containment box|containment box=]'s size, ad infinitum. When paired with layout containment, though, possible optimizations that can be enabled include (but are not limited to): 1. When the style or contents of a descendant of the [=size containment box|containment box=] is changed, calculating what part of the DOM tree is "dirtied" and might need to be re-laid out can stop at the [=size containment box|containment box=]. 2. When laying out the page, if the [=size containment box|containment box=] is off-screen or obscured, the layout of its contents can be delayed or done at a lower priority.

Layout Containment

If the element does not generate a principal box (as is the case with 'display' values of ''display/contents'' or ''display/none''), or its [=principal box=] is an internal table box other than ''display/table-cell'', or an internal ruby box, or a non-atomic inline-level box, layout containment has no effect. Otherwise, giving an element layout containment makes its [=principal box=] a layout containment box and has the following effects: contain-layout-001.html contain-layout-002.html contain-layout-003.html contain-layout-004.html contain-layout-005.html contain-layout-009.html contain-layout-010.html contain-layout-011.html contain-layout-012.html contain-layout-013.html contain-layout-014.html contain-layout-independent-formatting-context-003.html 1. The [=layout containment box=] [=establishes an independent formatting context=]. contain-layout-ifc-022.html contain-layout-independent-formatting-context-001.html contain-layout-independent-formatting-context-002.html contain-subgrid-001.html 2. If at least one [=fragmentation container=] of a [=fragmentation context=] has [=layout containment=], or if at least one [=fragmentation container=] of a [=fragmentation context=] is a descendant of [=layout containment box=] and at least one subsequent [=fragmentation container=] of the same [=fragmentation context=] is not a descendant of that same element with layout containment, then the first [=layout containment box=] which is either a [=fragmentation container=] itself or is an ancestor of a [=fragmentation container=] must “trap” the remainder of the [=fragmented flow=]: [=fragmentation=] must not continue past the [=layout containment=] boundary, and the last [=fragmentation container=] within the first [=layout containment=] boundary is treated as if it is the last [=fragmentation container=] in its [=fragmentation context=]. If subsequent [=fragmentation containers=] in the [=fragmentation context=] are only generated when more content remains in the [=fragmented flow=], then they are not generated. If they would exist regardless, they remain part of the [=fragmentation context=], but do not receive any content from the [=fragmented flow=]. Note: At the time of writing, no stable specification is affected by this point. Only specifications that would enable some (but not all) fragmentation containers of a fragmentation context to be layout-contained (or descendants of a layout contained element) are concerned. This is not the case of [[CSS-PAGE-3]] nor of [[CSS-MULTICOL-1]]. This requirement is nonetheless included because several mechanisms that would make this a possibility have been considered (e.g.: [[CSS-REGIONS-1]], ''::nth-fragment()'', a hypothetical selector for individual columns of a multicol…), and the guarantees that layout containment is intended to offer would not be realized if such mechanisms did not abide by this rule. [[CSS-REGIONS-1]] has details over how layout containment affects regions.

				<article>Lorem ipsum…</article>
				<div id=a></div>
				<aside>
				  <div id=b></div>
				  <div id=c></div>
				</aside>
				<aside>
				  <div id=d></div>
				  <div id=e></div>
				</aside>
				<div id=f></div>
			

				article {flow-into: foo;}
				#a, #b, #c, #d, #e, #f {flow-from: foo;}
				aside {contain: layout}
			
In this [[CSS-REGIONS-1]] example, content can flow from #a to #b, from #b to #c. However as #c is the last fragment container in the first [=layout containment box=] it traps all the remaining content, and nothing gets flowed into #d, #e, or #f.
3. If the computed value of the 'overflow' property is either ''overflow/visible'' or ''overflow/clip'' or a combination thereof, any overflow must be treated as [=ink overflow=]. contain-layout-ink-overflow-013.html contain-layout-ink-overflow-014.html contain-layout-ink-overflow-015.html contain-layout-ink-overflow-016.html contain-layout-ink-overflow-017.html contain-layout-ink-overflow-018.html contain-layout-ink-overflow-019.html contain-layout-ink-overflow-020.html 4. The [=layout containment box=] acts as a containing block for absolutely positioned and fixed positioned descendants. contain-layout-006.html contain-layout-007.html contain-layout-cell-001.html contain-layout-cell-002.html 5. The [=layout containment box=] creates a stacking context. contain-layout-016.html contain-layout-017.html contain-layout-018.html 6. [=Forced breaks=] are allowed within [=layout containment boxes=] but do not propagate to the parent as otherwise described in [[CSS-BREAK-3#break-between]]. contain-layout-breaks-001.html contain-layout-breaks-002.html Note: This introduces the previously non-existent possibility that [=forced breaks=] may occur between a box and its container (See [[CSS-BREAK-3#possible-breaks]]). 7. For the purpose of the 'vertical-align' property, or any other property whose effects need to relate the position of the [=layout containment box=]'s baseline to something other than its descendants, the [=layout containment box|containment box=] is treated as having no baseline. contain-layout-baseline-001.html contain-layout-button-001.html contain-layout-flexbox-001.html contain-layout-grid-001.html contain-layout-baseline-002.html contain-layout-baseline-003.html contain-layout-baseline-004.html contain-layout-baseline-005.html Possible optimizations that can be enabled by layout containment include (but are not limited to): 1. When laying out the page, the contents of separate [=layout containment boxes|containment boxes=] can be laid out in parallel, as they're guaranteed not to affect each other. 2. When laying out the page, if the [=layout containment box|containment box] is off-screen or obscured and the layout of the visible parts of the screen do not depend on the size of the [=layout containment box|containment box=] (for example, if the [=layout containment box|containment box=] is near the end of a block container, and you're viewing the beginning of the block container), the layout of the [=layout containment box|containment box]' contents can be delayed or done at a lower priority. (When paired with size containment, this optimization can be applied more liberally.)

Paint Containment

If the element does not generate a principal box (as is the case with 'display' values of ''display/contents'' or ''display/none''), or its [=principal box=] is an internal table box other than ''display/table-cell'', or an internal ruby box, or a non-atomic inline-level box, paint containment has no effect. Otherwise, giving an element paint containment makes its [=principal box=] a paint containment box and has the following effects: contain-paint-002.html contain-paint-005.html contain-paint-006.html contain-paint-007.html contain-paint-008.html contain-paint-011.html contain-paint-012.html contain-paint-014.html contain-paint-015.html contain-paint-016.html contain-paint-017.html contain-paint-018.html contain-paint-019.html contain-paint-independent-formatting-context-003.html 1. The contents of the element including any [=ink overflow|ink=] or [=scrollable overflow=] must be clipped to the padding edge of the [=paint containment box=], taking corner clipping into account. This does not include the creation of any mechanism to access or indicate the presence of the clipped content; nor does it inhibit the creation of any such mechanism through other properties, such as 'overflow', 'resize', or 'text-overflow'. Note: The next level of this specification [[CSS-CONTAIN-2]] refines this effect to apply to the [=overflow clip edge=] rather than the [=padding edge=], in order to take the new 'overflow-clip-margin' property into account. For implementations that do not support 'overflow-clip-margin', the effect is identical. Note: The behavior is described in this paragraph is equivalent to changing ''overflow-x: visible'' into ''overflow-x: clip'' and ''overflow-y: visible'' into ''overflow-y: clip'' at used value time, while leaving other values of 'overflow-x' and 'overflow-y' unchanged. contain-paint-001.html contain-paint-004.html contain-paint-047.html contain-paint-048.html contain-paint-cell-001.html contain-paint-cell-002.html contain-paint-clip-011.html contain-paint-clip-012.html contain-paint-clip-013.html contain-paint-clip-014.html contain-paint-clip-015.html contain-paint-clip-016.html contain-paint-clip-017.html contain-paint-clip-018.html contain-paint-clip-019.html contain-paint-table-001.html contain-paint-table-002.html 2. The [=paint containment box=] acts as a containing block for absolutely positioned and fixed positioned descendants. contain-paint-009.html contain-paint-010.html contain-paint-022.html contain-paint-023.html contain-paint-024.html 3. The [=paint containment box=] creates a stacking context. contain-paint-020.html contain-paint-021.html contain-paint-025.html contain-subgrid-001.html 4. The [=paint containment box=] [=establishes an independent formatting context=]. contain-paint-ifc-011.html contain-paint-independent-formatting-context-001.html contain-paint-independent-formatting-context-002.html contain-paint-baseline-001.html
Possible optimizations that can be enabled by paint containment include (but are not limited to): 1. If the [=paint containment box|containment box=] is off-screen or obscured, the UA can directly skip trying to paint its contents, as they're guaranteed to be off-screen/obscured as well. 2. Unless the clipped content is made accessible via a separate mechanism such as the 'overflow', 'resize', or 'text-overflow' properties, the UA can reserve "canvas" space for the box exactly the box's size. (In similar, scrollable, situations, like ''overflow: hidden'', it's possible to scroll to the currently-clipped content, so UAs often predictively overpaint somewhat so there's something to see as soon as the scroll happens, rather than a frame later.) 3. Because they are guaranteed to be stacking contexts, scrolling elements can be painted into a single GPU layer.
Privacy and Security Considerations {#privsec} ============================================== This specification introduces no new privacy or security considerations. Like any other CSS specification, it affects the rendering of the document, but does not introduce any special ability to present content in a misleading way that was not previously available through other CSS modules and that isn't inherent to the act of formatting the document. The TAG has developed a self-review questionnaire to help editors and Working Groups evaluate the risks introduced by their specifications. Answers are provided below.
Does this specification deal with personally-identifiable information?
No.
Does this specification deal with high-value data?
No.
Does this specification introduce new state for an origin that persists across browsing sessions?
No.
Does this specification expose persistent, cross-origin state to the web?
No.
Does this specification expose any other data to an origin that it doesn’t currently have access to?
No.
Does this specification enable new script execution/loading mechanisms?
No.
Does this specification allow an origin access to a user’s location?
No.
Does this specification allow an origin access to sensors on a user’s device?
No.
Does this specification allow an origin access to aspects of a user’s local computing environment?
No.
Does this specification allow an origin access to other devices?
No.
Does this specification allow an origin some measure of control over a user agent’s native UI?
No.
Does this specification expose temporary identifiers to the web?
No.
Does this specification distinguish between behavior in first-party and third-party contexts?
No.
How should this specification work in the context of a user agent’s "incognito" mode?
No difference in behavior is needed.
Does this specification persist data to a user’s local device?
No.
Does this specification have a "Security Considerations" and "Privacy Considerations" section?
Yes, this is the section you are currently reading.
Does this specification allow downgrading default security characteristics?
No.

Appendix A. Changes

This appendix is informative.

Changes from the Recommendation of 21 November 2019

Changes from the Candidate Recommendation of 30 April 2019