CSS Scoping Module Level 2

Editor’s Draft,

More details about this document
This version:
Latest published version:
CSSWG Issues Repository
Inline In Spec
Tab Atkins Jr. (Google)
Miriam E. Suzanne (Invited Expert)
Suggest an Edit for this Spec:
GitHub Editor


This specification defines scoping/encapsulation mechanisms for CSS.

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

Status of this document

This is a public copy of the editors’ draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don’t cite this document other than as work in progress.

Please send feedback by filing issues in GitHub (preferred), including the spec code “css-scoping” in the title, like this: “[css-scoping] …summary of comment…”. All issues and comments are archived. Alternately, feedback can be sent to the (archived) public mailing list www-style@w3.org.

This document is governed by the 2 November 2021 W3C Process Document.

1. Introduction

This is a diff spec over CSS Scoping Module Level 1. It is currently an Exploratory Working Draft: if you are implementing anything, please use Level 1 as a reference. We will merge the Level 1 text into this draft once it reaches CR.

2. Default Styles for Custom Elements

CSS Scoping 1 § 2 Default Styles for Custom Elements

3. Shadow Encapsulation

CSS Scoping 1 § 3 Shadow Encapsulation

4. Scoped Styles

A scope is a subtree or fragment of a document, which can be used by selectors for more targeted matching. Scopes are described in CSS through a combination of two selector lists:

Note: This means that the :scope pseudo-class in a lower boundary (<scope-end>) selector will always refer to the scope being described, rather than the outer scope context.

For example, scope boundaries can be contextual:
/* .content is only a boundary when the :scope is inside .sidebar */
@scope (.media-object) to (.sidebar :scope .content) {
  img { border-radius: 50%; }

Or based on a specific relationship to the scoping root:

/* .content is only a boundary when it is a direct child of the :scope */
@scope (.media-object) to (:scope > .content) {
  img { border-radius: 50%; }

Each resulting scope includes a scoping root and all its descendants, up to and including any scope boundary elements, but not the descendants of those boundaries.

In contrast to Shadow Encapsulation, which describes a persistent one-to-one relationship in the DOM between a shadow host and its nested shadow tree, multiple overlapping scopes can be defined in relation to the same elements.

For example, An author might have wide-reaching color-scheme scopes, which overlap more narrowly defined design patterns such as a media object:
@scope (.light-scheme) {
  a { color: darkmagenta; }

@scope (.dark-scheme) {
  a { color: plum; }

@scope (.media-object) {
  .media-image { border-radius: 50%; }
  .media-content { padding: 1em; }
By providing scope boundaries, an author can limit matching more deeply nested descendants. For example:
@scope (.media-object) to (.content) {
  img { border-radius: 50%; }

  /* it is also possible to style the lower boundary itself */
  .content { padding: 1em; }

The img selector will only match image tags that are inside a .media-object, without any intervening .content class between it and the scoping root.

4.1. The in-scope pseudo-class :in()

The in-scope pseudo-class, :in(), is a functional pseudo-class with the following syntax:

:in(<scope-start> [/ <scope-end>]?)

If, after parsing, <scope-start> is an empty list, the pseudo-class is valid but matches nothing, and defines no scopes. Otherwise, the pseudo-class matches any element that is in a scope described by the given <scope-start> and <scope-end> selectors.

Note: This does not effect the :scope elements for the selector.

The specificity of the :in() pseudo-class is replaced by the specificity of the most specific complex selector in its <scope-start> argument.

The purpose of the in-scope pseudo-class is to allow adding scope boundaries to a selector:
.title:in(.post / .comments) { font-size: 2em; }

Without any such lower boundaries, the in() pseudo-class is similar to other existing selectors. These three selectors will all select the same elements, with the same specificity:

.child:in(.ancestor) { color: darkmagenta; }
.child:is(.ancestor, .ancestor *) { color: darkmagenta; }
.ancestor.child, .ancestor .child { color: darkmagenta; }

4.2. Scoping Styles in CSS: the @scope rule

The @scope block at-rule allows authors to create scoped stylesheets in CSS, with the addition of scope proximity weighting in the cascade. The syntax of the @scope rule is:

@scope (<scope-start>) [to (<scope-end>)]? {

The @scope rule has three primary effects on the style rules in <stylesheet>. For each scope that is described by the given <scope-start> and <scope-end>:

The following selectors have the same specificity (1,0,1):
#hero img { border-radius: 50%; }

@scope (#hero) {
  img { border-radius: 50%; }
Many existing tools implement "scoped styles" by applying a unique class or attribute to every element in a given scope or "single file component." In this example there are two scopes (main-component and sub-component) and every element is marked as part of one or both scopes using the data-scope attribute:
<section data-scope="main-component">
  <p data-scope="main-component">...<p>

  <!-- sub-component root is in both scopes -->
  <section data-scope="main-component sub-component">
    <!-- children are only in the inner scope -->
    <p data-scope="sub-component">...<p>

Those custom scope attributes are then appended to every single selector in CSS:

p[data-scope~='main-component'] { color: red; }
p[data-scope~='sub-component'] { color: blue; }

/* both sections are part of the outer scope */
section[data-scope~='main-component'] { background: snow; }

/* the inner section is also part of the inner scope */
section[data-scope~='sub-component'] { color: ghostwhite; }

Using the @scope rule, authors and tools can replicate similar behavior with the unique attribute or class applied only to the scoping roots:

<section data-scope="main-component">
  <section data-scope="sub-component">
    <!-- children are only in the inner scope -->

Then the class or attribute can be used for establishing both upper and lower boundaries, such that scopes only overlap at those boundaries:

@scope ([data-scope='main-component']) to ([data-scope]) {
  p { color: red; }

  /* both sections are part of the outer scope */
  section { background: snow; }

@scope ([data-scope='sub-component']) to ([data-scope]) {
  p { color: blue; }

  /* the inner section is also part of the inner scope */
  section { color: ghostwhite; }

@scope rules can be nested. In this case, just as with the nested style rules, the selectors of the inner @scope are scoped by the selectors of the outer one.

4.2.1. Scope Proximity in the Cascade

This likely belongs in the css-cascade specification.

Scope proximity is considered in the cascade sort order after specificity, and before order of appearance.

If the :scope elements of two declarations have an ancestor/descendant relationship, then the declaration whose :scope element is the descendant wins.

Note: When the :scope element is not otherwise defined for a declaration, it is the document root element.

When scoped declarations overlap—applying to the same elements with the same cascade origin, importance, layer, and specificity—then one with closer scope proximity takes precedence. For example, this light-scheme and dark-scheme scopes:
@scope (.light-scheme) {
  a { color: darkmagenta; }

@scope (.dark-scheme) {
  a { color: plum; }

If light-scheme and dark-scheme classes are nested in the DOM, whichever is closer to a given link in the DOM tree will take precedence for styling that link, regardless of their order of appearance in CSS:

<section class="light-scheme">
  <a href="#">
    light scope:
    darkmagenta link color
  <aside class="dark-scheme">
    <a href="#">
      both scopes, but dark-scheme is a closer ancestor:
      plum link color

5. Changes

5.1. Additions Since Level 1

The following features have been added since Level 1:


Elika J. Etemad / fantasai, Giuseppe Gurgone, Keith Grant, Lea Verou, Nicole Sullivan, and Theresa O’Connor contributed to this specification.

6. Privacy and Security Considerations

This specification introduces Shadow DOM and some shadow-piercing capabilities, but this does not introduce any privacy or security issues—shadow DOM, as currently specified, is intentionally not a privacy/security boundary (and the parts of the UA that use shadow DOM and do have a privacy/security boundary implicitly rely on protections not yet specified, which protect them from the things defined in this specification).


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.


Tests relating to the content of this specification may be documented in “Tests” blocks like this one. Any such block is non-normative.

Conformance classes

Conformance to this specification is defined for three conformance classes:

style sheet
A CSS style sheet.
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.

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

Non-experimental implementations

Once a specification reaches the Candidate Recommendation stage, non-experimental implementations are possible, and implementors should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec.

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 http://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.


Terms defined by this specification

Terms defined by reference


Normative References

Tab Atkins Jr.; Simon Sapin. CSS Syntax Module Level 3. 24 December 2021. CR. URL: https://www.w3.org/TR/css-syntax-3/
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 4. 16 December 2021. WD. URL: https://www.w3.org/TR/css-values-4/
Anne van Kesteren. DOM Standard. Living Standard. URL: https://dom.spec.whatwg.org/
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://datatracker.ietf.org/doc/html/rfc2119
Elika Etemad; Tab Atkins Jr.. Selectors Level 4. 21 November 2018. WD. URL: https://www.w3.org/TR/selectors-4/

Issues Index

This is a diff spec over CSS Scoping Module Level 1. It is currently an Exploratory Working Draft: if you are implementing anything, please use Level 1 as a reference. We will merge the Level 1 text into this draft once it reaches CR.
CSS Scoping 1 § 2 Default Styles for Custom Elements
CSS Scoping 1 § 3 Shadow Encapsulation
This likely belongs in the css-cascade specification.