Editor's Draft · css-shadow

CSS Shadow

5 selectors defined by this specification, all known to at least one browser .

Each feature in detail

::part()

Selector Baseline: widely availablesince January 2023

Spec The ::part() pseudo-element allows you to select elements that have been exposed via a part attribute. The syntax is:

MDN The ::part CSS pseudo-element represents any element within a shadow tree that has a matching part attribute.

Syntax

::part( <ident>+ )

Formal grammar from the specification, as webref extracts it: the latest level's definition, sometimes ahead of what browsers implement.

Browser support

First browser version supporting ::part()
Chrome 73
Edge 79
Firefox 72
Safari 13.1
Chrome Android 73
Safari iOS 13.4

First version with support, from MDN browser-compat-data.

::slotted()

Selector Baseline: widely availablesince July 2022

Spec The ::slotted() pseudo-element represents the elements assigned, after flattening, to a slot. This pseudo-element only exists on slots.

MDN The ::slotted() CSS pseudo-element represents any element that has been placed into a slot inside an HTML template (see Using templates and slots for more information).

Live preview Example from MDN
/* This CSS is being applied inside the shadow DOM. */

::slotted(.content) {
  background-color: aqua;
}

h2 ::slotted(span) {
  background: silver;
}

Browser support

First browser version supporting ::slotted()
Chrome 50
Edge 79
Firefox 63
Safari 10
Chrome Android 50
Safari iOS 10

First version with support, from MDN browser-compat-data.

:has-slotted

Selector Limited availability Modern CSS: :has-slotted

Spec The :has-slotted pseudo-class matches slot elements which have a non-empty list of flattened slotted nodes.

MDN The :has-slotted CSS pseudo-class matches when the content of a <slot> element is not empty or not using the default value (see Using templates and slots for more information).

Syntax

:has-slotted

Formal grammar from the specification, as webref extracts it: the latest level's definition, sometimes ahead of what browsers implement.

Browser support

First browser version supporting :has-slotted
Chrome No
Edge No
Firefox 136
Safari No
Chrome Android No
Safari iOS No

First version with support, from MDN browser-compat-data.

:host

Selector Baseline: widely availablesince July 2022

Spec The :host pseudo-class, when evaluated in the context of a shadow tree, matches the shadow tree’s shadow host. In any other context, it matches nothing.

MDN The :host CSS pseudo-class selects the shadow host of the shadow DOM containing the CSS it is used inside — in other words, this allows you to select a custom element from inside its shadow DOM.

Live preview Example from MDN
/* This CSS is being applied inside the shadow DOM. */

:host {
  background-color: aqua;
}

Syntax

:host

Formal grammar from the specification, as webref extracts it: the latest level's definition, sometimes ahead of what browsers implement.

Browser support

First browser version supporting :host
Chrome 54
Edge 79
Firefox 63
Safari 10
Chrome Android 54
Safari iOS 10

First version with support, from MDN browser-compat-data.

:host-context()

Selector Deprecated

Spec The :host-context() functional pseudo-class tests whether there is an ancestor, outside the shadow tree, which matches a particular selector. Its syntax is:

MDN The :host-context() CSS pseudo-class allows you to style elements within a shadow DOM differently based on the selector of the shadow host (the element that has the shadow root) and its DOM ancestors.

Live preview Example from MDN
/* Following CSS is being applied inside the shadow DOM. */

:host-context(.container) {
  border: 5px dashed green;
}

:host-context(h1) {
  color: red;
}

Browser support

First browser version supporting :host-context()
Chrome 54
Edge 79
Firefox No
Safari No
Chrome Android 54
Safari iOS No

First version with support, from MDN browser-compat-data.