::slotted()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
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).
This only works when used inside CSS placed within a shadow DOM. Note that this selector won't select a text node placed into a slot; it only targets actual elements.
Try it
/* This CSS is being applied inside the shadow DOM. */
::slotted(.content) {
background-color: aqua;
}
h2 ::slotted(span) {
background: silver;
}
title goes here
content goes here
Error
Build failed!
customElements.define(
"my-card",
class extends HTMLElement {
constructor() {
super();
const template = document.getElementById("card-template");
const shadow = this.attachShadow({ mode: "open" });
shadow.appendChild(template.content.cloneNode(true));
const elementStyle = document.createElement("style");
elementStyle.textContent = `
div {
width: 200px;
border: 2px dotted red;
border-radius: 4px;
}`;
shadow.appendChild(elementStyle);
const cssTab = document.querySelector("#css-output");
const editorStyle = document.createElement("style");
editorStyle.textContent = cssTab.textContent;
shadow.appendChild(editorStyle);
cssTab.addEventListener("change", () => {
editorStyle.textContent = cssTab.textContent;
});
}
},
);
/* Selects any element placed inside a slot */
::slotted(*) {
font-weight: bold;
}
/* Selects any placed inside a slot */
::slotted(span) {
font-weight: bold;
}
Syntax
::slotted() {
/* ... */
}
Examples
Highlighting slotted elements
In this example, we use a template with three slots:
Personal ID Card
NAME MISSING
AGE MISSING
OCCUPATION MISSING
We define the
custom element. In this case, we add styles with JavaScript, though we could have added them in a block within the
with the same effect:
customElements.define(
"person-details",
class extends HTMLElement {
constructor() {
super();
let template = document.getElementById("person-template");
let templateContent = template.content;
const shadowRoot = this.attachShadow({ mode: "open" });
let style = document.createElement("style");
style.textContent =
"div { padding: 10px; border: 1px solid gray; width: 200px; margin: 10px; }" +
"h2 { margin: 0 0 10px; }" +
"ul { margin: 0; }" +
"p { margin: 10px 0; }" +
"::slotted(*) { color: gray; font-family: sans-serif; } " +
"::slotted(span) {text-decoration: underline;} ";
shadowRoot.appendChild(style);
shadowRoot.appendChild(templateContent.cloneNode(true));
}
},
);
When filling the style
element with content, you'll see that we select all slotted elements (::slotted(*)
) and give them a different font and color. This differentiates them from the slots that haven't been filled. We styled all the slotted s (
::slotted(span)
) to differentiate the s from the
s.
Our markup includes three custom elements, including a custom element with an invalid slot name in a source order that differs from the :
Wonder Woman
Immortal
Superhero
Malala Yousafzai
17
Activist
44
Time traveler
Dr. Who
Result
Specifications
Specification |
---|
CSS Scoping Module Level 1 # slotted-pseudo |
Browser compatibility
See also
:host
:host()
:host-context()
:has-slotted
- CSS scoping module
- HTML
slot
attribute - HTML
element - HTML
element
- Web components