CSS Containment Module Level 1

W3C Working Draft,

This version:
https://www.w3.org/TR/2017/WD-css-contain-1-20170419/
Latest published version:
https://www.w3.org/TR/css-contain-1/
Editor's Draft:
https://drafts.csswg.org/css-contain/
Previous Versions:
https://www.w3.org/TR/2017/WD-css-contain-1-20170221/
Test Suite:
http://test.csswg.org/suites/css-contain-1_dev/nightly-unstable/
Editors:
Tab Atkins (Google)
Florian Rivoal (On behalf of Bloomberg)
Issue Tracking:
GitHub Issues

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.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, etc.

Status of this document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.

Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

GitHub Issues are preferred for discussion of this specification. When filing an issue, please put the text “css-contain” in the title, preferably like this: “[css-contain] …summary of comment…”. All issues and comments are archived, and there is also a historical archive.

This document was produced by the CSS Working Group (part of the Style Activity).

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This document is governed by the 1 March 2017 W3C Process Document.

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.
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.

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 an effect a given change can have on a document.

3.1. Size Containment

Giving an element size containment has the following effect:

  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.

    Replaced elements must be treated as having an intrinsic width and height of 0.

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):

  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.

3.2. Layout Containment

Giving an element layout containment has the following effects:

  1. The element becomes a formatting context.

  2. 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:

  3. If the contents of the element overflow the element, they must be treated as ink overflow.

  4. The element acts as a containing block for absolutely positioned and fixed positioned descendants.

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 containing elements can be laid out in parallel, as they’re guaranteed not to affect each other.

  2. 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:

  1. The following properties must have no effect on descendants of the element:

  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.

3.4. Paint Containment

Giving an element paint containment has the following effects:

  1. The contents of the element must be clipped to the element’s content box, including both the paint of the descendants and their geometry. This is similar to overflow: clip, but has no effect or dependency on the computed value of the overflow property. 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.

  2. The element acts as a containing block for absolutely positioned and fixed positioned descendants.

  3. The element creates a stacking context.

  4. The element becomes 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. 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.)

  3. Because they are guaranteed to be stacking contexts, scrolling elements can be painted into a single GPU layer.

4. Becoming a formatting context

When required by layout containment and paint containment, an element may need to become a formatting context. If the element already establishes a formatting context of any kind, this condition is satisfied. Otherwise, it is made to establish a BFC by changing its inner display type to flow-root.

5. Privacy and Security Considerations

This specification introduces no new privacy or security considerations.

Appendix A. Changes

This appendix is informative.

This appendix describes changes from the First Public Working Draft of 21 February 2017.

Conformance

Document conventions

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

Examples in this specification are introduced with the words “for example” or are set apart from the normative text with class="example", like this:

This is an example of an informative example.

Informative notes begin with the word “Note” and are set apart from the normative text with class="note", like this:

Note, this is an informative note.

Advisements are normative sections styled to evoke special attention and are set apart from other normative text with <strong class="advisement">, like this: UAs MUST provide an accessible alternative.

Conformance classes

Conformance to this specification is defined for three conformance classes:

style sheet
A CSS style sheet.
renderer
A UA that interprets the semantics of a style sheet and renders documents that use them.
authoring tool
A UA that writes a style sheet.

A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module.

A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.

Requirements for Responsible Implementation of CSS

The following sections define several conformance requirements for implementing CSS responsibly, in a way that promotes interoperability in the present and future.

Partial Implementations

So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported property values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

Implementations of Unstable and Proprietary Features

To avoid clashes with future stable CSS features, the CSSWG recommends following best practices for the implementation of unstable features and proprietary extensions to CSS.

Implementations of CR-level Features

Once a specification reaches the Candidate Recommendation stage, implementers should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec, and should avoid exposing a prefixed variant of that feature.

To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.

Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at https://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[CSS-BREAK-3]
Rossen Atanassov; Elika Etemad. CSS Fragmentation Module Level 3. URL: https://www.w3.org/TR/css-break-3/
[CSS-CONTENT-3]
Elika Etemad; Dave Cramer. CSS Generated Content Module Level 3. URL: https://www.w3.org/TR/css-content-3/
[CSS-DISPLAY-3]
Elika Etemad. CSS Display Module Level 3. URL: https://www.w3.org/TR/css-display-3/
[CSS-LISTS-3]
Tab Atkins Jr.. CSS Lists and Counters Module Level 3. URL: https://www.w3.org/TR/css-lists-3/
[CSS-OVERFLOW-3]
David Baron; Florian Rivoal. CSS Overflow Module Level 3. URL: https://www.w3.org/TR/css-overflow-3/
[CSS-OVERFLOW-4]
CSS Overflow Module Level 4 URL: https://drafts.csswg.org/css-overflow-4/
[CSS-POSITION-3]
Rossen Atanassov; Arron Eicholz. CSS Positioned Layout Module Level 3. URL: https://www.w3.org/TR/css-position-3/
[CSS-REGIONS-1]
Rossen Atanassov; Alan Stearns. CSS Regions Module Level 1. URL: https://www.w3.org/TR/css-regions-1/
[CSS-UI-3]
Tantek Çelik; Florian Rivoal. CSS Basic User Interface Module Level 3 (CSS3 UI). URL: https://www.w3.org/TR/css-ui-3/
[CSS-UI-4]
Florian Rivoal. CSS Basic User Interface Module Level 4. URL: https://www.w3.org/TR/css-ui-4/
[CSS-VALUES-3]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 3. URL: https://www.w3.org/TR/css-values-3/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119

Property Index

Name Value Initial Applies to Inh. %ages Media Anim­ation type Canonical order Com­puted value
contain none | strict | content | [ size || layout || style || paint ] none all elements no n/a all discrete per grammar specified value