CSS Containment Module Level 3

Level: 3
Shortname: css-containment
Status: ED
Work Status: Exploring
Group: csswg
ED: http://dev.w3.org/csswg/css-containment/
Editor: Tab Atkins, Google, http://xanthir.com/contact/
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.
Ignored Terms: scrollWidth, scrollHeight, clientWidth, clientHeight, formatting context
Link Defaults: css-lists-3 (property) counter-increment, css-content-3 (property) string-set


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 flunk the heuristics and fall into a slow mode. 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.

Strong Containment: the 'contain' property

		Name: contain
		Value: none | strict | [ layout || style || paint ]
		Initial: none
		Inherited: no
		Applies to: all elements
		Media: all
		Computed value: specified value
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.
This value indicates that the property has no effect. The element renders as normal, with no containment effects applied.
This value turns on all forms of containment for the element. In other words, it behaves the same as ''contain: layout style paint;'', so that its contents are guaranteed to have no effect on the rest of the page outside the element's bounds.
This value turns on layout containment for the element. The ensures that the containing element "traps" any layout-related effects of its descendants, so the rest of the document is guaranteed to not be affected by the layout of the containing element's descendants.
This value turns on style containment for the element. This ensures that, for properties which can have effects on more than just an element and its descendants, those effects dont' escape the containing element.
This value turns on paint containment for the element. This ensures that the descendants of the containing element 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' 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:
				<aside class='sidebar'>...</aside>
				<article class='messages'>
					<section class='message'>
						Lol, check out this dog: images.example.com/jsK3jkl
					<section class='message'>
						I had a ham sandwich today. #goodtimes
					<section class='message'>
						I have political opinions that you need to hear!
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: strict'' 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.

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 an effect a given change can have on a document. Issue: What other effects need to be built into the containment types?

Layout Containment

Giving an element layout containment has the following effects: 1. When laying out the containing element, it must be treated as having no contents. After layout of the element is complete, its contents must then be laid out into the containing element's resolved size. 2. The element must be a formatting context. 3. If a fragmentation context participates in layout containment, the first element with layout containment affecting the fragmentation context 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. Specifically: - CSS Regions following the one which traps the content are still considered part of the region chain as returned by the {{NamedFlow/getRegions()}} method of the {{NamedFlow}} interface. - the {{Region/regionOverset}} attribute of the {{Region}} interface of the region which traps the content is set to ''overset'' if the content doesn't fit, even if it is not the last region in the region chain. - If the computed value of the 'continue' property on an element with layout containment would otherwise have been ''continue/auto'' or ''continue/fragments'', it must instead compute to ''continue/overflow''. Possible optimizations that can be enabled by layout containment include (but are not limited to): 1. When the style or contents of a descendant of the containing element is changed, calculating what part of the DOM tree is "dirtied" and might need to be re-laid out can stop at the containing element. 2. When laying out the page, if the containing element is off-screen or obscured, the layout of its contents can be delayed or done at a lower priority. Issue: This actually requires paint containment as well, since an abspos or negative-margin element can protrude out and cause a scrollbar to appear on a containing element, thus affecting layout. This isn't an issue with overlay scrollbars. 3. When laying out the page, the contents of separate containing elements can be laid out in parallel, as they're guaranteed not to affect each other.

Style Containment

Giving an element style containment has the following effects: 1. The following properties must have no effect on descendants of the element: * 'break-*' (and the related aliases) * 'bookmark-*' * 'string-set' 2. The 'counter-increment', 'counter-set', 'flow-from', 'flow-into', and 'content' (for the purpose of ''open-quote''/etc values) properties must be scoped to the element's sub-tree. A scoped property has its effects scoped to a particular element or subtree. It must act as if the scoping element was the root of the document for the purpose of evaluating the property's effects: any uses of the property outside the scoping element must have no effect on the uses of the property on or in the scoping element, and vice versa. If scoped to a sub-tree, it's the same, except the scoping element itself is counted as "outside" the tree, like the rest of the document. For example, if 'counter-increment' is scoped to an element, the first use of it within the subtree acts as if the named counter were set to 0 at the scoping element, regardless of whether the counter had been used outside the scoping element. Any increments made within the subtree have no effect on counters of the same name outside the scoping element. Possible optimizations that can be enabled by style containment include (but are not limited to): 1. Whenever a property is changed on a descendant of the containing element, calculating what part of the DOM tree is "dirtied" and might need to have its style recalculated can stop at the containing element.

Paint Containment

Giving an element paint containment has the following effects: 1. If the computed value of overflow-x or overflow-y would otherwise have been ''overflow/visible'', it must instead compute to ''overflow/clip''. [[!CSS-OVERFLOW-3]] This means that regardless of the specified value of 'overflow', the contents of the element is clipped to the element's content box, including both the paint of the descendants and their geometry 2. The element must act as a containing block for absolutely positioned and fixed positioned descendants. 3. The element must be a stacking context. 4. The element must be a formatting context. Possible optimizations that can be enabled by paint containment include (but are not limited to): 1. If the containing element 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. If the containing element's overflow has been changed to ''overflow/clip'' (because it was originally ''overflow/visible''), the UA can reserve "canvas" space for the element exactly the element'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.