Spec The ::slotted() pseudo-element represents the elements assigned, after flattening, to a slot. This pseudo-element only exists on slots.
MDN The ::slotted()CSSpseudo-element represents any element that has been placed into a slot inside an HTML template (see Using templates and slots for more information).
Live previewExample from MDN
/* This CSS is being applied inside the shadow DOM. */::slotted(.content) { background-color: aqua;}h2 ::slotted(span) { background: silver;}
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 :hostCSSpseudo-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 previewExample from MDN
/* This CSS is being applied inside the shadow DOM. */:host { background-color: aqua;}
<h1 id="shadow-dom-host"></h1>
const shadowDom = init();// add a <span> element in the shadow DOMconst span = document.createElement("span");span.textContent = "Inside shadow DOM";shadowDom.appendChild(span);// attach shadow DOM to the #shadow-dom-host elementfunction init() { const host = document.getElementById("shadow-dom-host"); const shadowDom = host.attachShadow({ mode: "open" }); const cssTab = document.querySelector("#css-output"); const shadowStyle = document.createElement("style"); shadowStyle.textContent = cssTab.textContent; shadowDom.appendChild(shadowStyle); cssTab.addEventListener("change", () => { shadowStyle.textContent = cssTab.textContent; }); return shadowDom;}
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.
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()CSSpseudo-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 previewExample from MDN
/* Following CSS is being applied inside the shadow DOM. */:host-context(.container) { border: 5px dashed green;}:host-context(h1) { color: red;}
<!-- elements outside shadow dom --><div class="container"> <h1 id="shadow-dom-host"></h1></div>
const shadowDom = init();// add a <span> element in the shadow DOMconst span = document.createElement("span");span.textContent = "Inside shadow DOM";shadowDom.appendChild(span);// attach shadow DOM to the #shadow-dom-host elementfunction init() { const host = document.getElementById("shadow-dom-host"); const shadowDom = host.attachShadow({ mode: "open" }); const cssTab = document.querySelector("#css-output"); const shadowStyle = document.createElement("style"); shadowStyle.textContent = cssTab.textContent; shadowDom.appendChild(shadowStyle); cssTab.addEventListener("change", () => { shadowStyle.textContent = cssTab.textContent; }); return shadowDom;}
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.