1. 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 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.
2. Strong Containment: the contain property
Name: | contain |
---|---|
Value: | none | strict | content | [ size || layout || style || paint ] |
Initial: | none |
Applies to: | all elements |
Inherited: | no |
Percentages: | n/a |
Media: | all |
Computed value: | specified value |
Canonical order: | per grammar |
Animation type: | discrete |
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 turns on all forms of containment for the element. In other words, it behaves the same as contain: size layout style paint;, so that its contents are guaranteed to have no effect on the rest of the page outside the element’s bounds.
- content
-
This value turns on all forms of containment except size containment for the element.
In other words, it behaves the same as contain: layout style paint;.
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 containing element can be laid out without needing to examine its descendants.
- layout
- This value turns on layout containment for the element. This ensures that the containing element is totally opaque for layout purposes; nothing outside can affect its internal layout, and vice versa.
- style
- 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 don’t escape the containing element.
- paint
- 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.
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.
3. 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.
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.
3.1. Size Containment
If the element does not generate a principal box (as is the case with display: contents or display: none), or if the element is an internal table element, or if the element is an internal ruby element, or if the element’s principal box is a non-atomic inline-level box, size containment has no effect. Otherwise, giving an element size containment has the following effects:
-
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.
Replaced elements must be treated as having an intrinsic width and height of 0.
-
Elements with size containment are monolithic (See CSS Fragmentation Module Level 3 §possible-breaks).
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 containing element’s contents based on the containing element’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 containing element causes the containing element’s size to change as well, possibly triggering further changes in how the child sizes itself and possibly thus more changes to the containing element’s size, ad infinitum.
When paired with layout containment, though, possible optimizations that can be enabled include (but are not limited to):
-
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.
-
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.
3.2. Layout Containment
If the element does not generate a principal box (as is the case with display: contents or display: none), or if the element is an internal table element other than display: table-cell, or if the element is an internal ruby element, or if the element’s principal box is a non-atomic inline-level box, layout containment has no effect. Otherwise, giving an element layout containment has the following effects:
-
The element establishes an independent formatting context.
-
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.
Note: [CSS-REGIONS-1] has details over how layout containment affects regions.
-
If the contents of the element overflow the element, they must be treated as ink overflow.
-
The element acts as a containing block for absolutely positioned and fixed positioned descendants.
-
Forced breaks are allowed within elements with layout containment, but do not propagate to the parent as otherwise described in CSS Fragmentation Module Level 3 §break-between.
Note: This introduces the previously non-existent possibility that forced breaks may occur between a box and its container (See CSS Fragmentation Module Level 3 §possible-breaks).
Possible optimizations that can be enabled by layout containment include (but are not limited to):
-
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.
-
When laying out the page, if the containing element is off-screen or obscured and the layout of the visible parts of the screen do not depend on the size of the containing element (for example, if the containing element is near the end of a block container, and you’re viewing the beginning of the block container), the layout of the containing elements' contents can be delayed or done at a lower priority.
(When paired with size containment, this optimization can be applied more liberally.)
3.3. Style Containment
Giving an element style containment has the following effects:
-
The counter-increment and counter-set properties must be scoped to the element’s sub-tree and create a new counter.
-
The effects of the content property’s open-quote, close-quote, no-open-quote and no-close-quote must be scoped to the element’s sub-tree.
Note: This implies that the depth of quote nesting in the subtree is unchanged and starts at the value that its context normally implies, but that changes to the depth of quote nesting by these values inside the subtree do not affect the depth of quote nesting outside the subtree.
Note: [CSS-REGIONS-1] has normative requirements on how style containment affects regions.
A scoped property has its effects scoped to a particular element or subtree. If scoped to an element, 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, and the effects of the property on that element are unaffected by scoping. When considering the effects of the scoped property on elements inside the subtree, the element at the base of the subtree is treated as if it was the root of the document.
1 1.2being displayed:
<div></div>
div {
contain: style;
counter-increment: n;
}
div::before, div::after {
content: counters(n, '.') " ";
}
div::after {
counter-increment: n 2;
}
Possible optimizations that can be enabled by style containment include (but are not limited to):
-
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.
3.4. Paint Containment
If the element does not generate a principal box (as is the case with display: contents or display: none), or if the element is an internal table element other than display: table-cell, or if the element is an internal ruby element, or if the element’s principal box is a non-atomic inline-level box, paint containment has no effect. Otherwise, giving an element paint containment has the following effects:
-
The contents of the element including both the paint of the descendants and their geometry must be clipped to the padding edge of the element’s principal 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. This is as if to overflow: visible was changed to overflow: clip at used value.
-
The element acts as a containing block for absolutely positioned and fixed positioned descendants.
-
The element creates a stacking context.
-
The element establishes an independent formatting context.
-
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.
-
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 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.)
-
Because they are guaranteed to be stacking contexts, scrolling elements can be painted into a single GPU layer.
4. Privacy and Security Considerations
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 questionaire 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 Candidate Recommendation of 8 August 2017
- Clarify to which box paint containment clips (see tests).
- Move the interaction between containment and the
bookmark-*
andstring-set
properties to [CSS-CONTENT-3] (additional tests not needed, no change in behavior). - Remove the effects of style containment on the "break-*" properties (see tests).
- Move the description of the effects of containement on regions from this specification to [CSS-REGIONS-1] (additional tests not needed, no change in behavior).
- Clarify the effects of style scoping on counter-set and counter-increment (see tests)
- Size layout and paint containment don’t apply to internal ruby elements (see tests)
- Layout, Paint, and size containments do not apply to non-atomic inlines (see tests here and one more test here)
- Align paint containment’s behavior with overflow:clip (see test)
- Elements with size containment are monolithic (see test)
- Forced breaks area allowed in elements with layout containments, but do not propagate (see tests)
- Clarify the effects of scoping to a subtree (see test)
- Clarify the effects of scoping on open/close quotes (see tests)
- Editorial clarification: replace "Becoming a formatting context" (aka "Becoming a formatting context root") with "Establish a FC" (additional tests not needed, no change in behavior)
Changes from the Working Draft of 19 April 2017
- Clarify the interaction with display: contents
- Clarify how containment works on table parts
- Move the interaction between containment and fragmentation of overflow from this specification to CSS-OVERFLOW-4
Changes from the First Public Working Draft of 21 February 2017
- Specify handling of replaced elements for size containment
- Layout containment makes element act as a containing block for absolutely positioned and fixed positioned descendants.