::slotted()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since Januar 2020.
Das ::slotted() CSS Pseudo-Element repräsentiert jedes Element, das in einen Slot innerhalb eines HTML-Templates platziert wurde (siehe Verwendung von Templates und Slots für weitere Informationen).
Dies funktioniert nur, wenn es innerhalb von CSS verwendet wird, das im Shadow-DOM platziert ist. Beachten Sie, dass dieser Selektor keinen Textknoten wählt, der in einen Slot platziert wurde; er zielt nur auf tatsächliche Elemente ab.
Probieren Sie es aus
/* This CSS is being applied inside the shadow DOM. */
::slotted(.content) {
background-color: aqua;
}
h2 ::slotted(span) {
background: silver;
}
<template id="card-template">
<div>
<h2><slot name="caption">title goes here</slot></h2>
<slot name="content">content goes here</slot>
</div>
</template>
<my-card>
<span slot="caption">Error</span>
<p class="content" slot="content">Build failed!</p>
</my-card>
customElements.define(
"my-card",
class extends HTMLElement {
constructor() {
super();
const template = document.getElementById("card-template");
const shadow = this.attachShadow({ mode: "open" });
shadow.appendChild(document.importNode(template.content, 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 <span> placed inside a slot */
::slotted(span) {
font-weight: bold;
}
Syntax
::slotted(<compound-selector>) {
/* ... */
}
Beispiele
>Hervorhebung von geslotteten Elementen
In diesem Beispiel verwenden wir ein Template mit drei Slots:
<template id="person-template">
<div>
<h2>Personal ID Card</h2>
<slot name="person-name">NAME MISSING</slot>
<ul>
<li><slot name="person-age">AGE MISSING</slot></li>
<li><slot name="person-occupation">OCCUPATION MISSING</slot></li>
</ul>
</div>
</template>
Wir definieren das benutzerdefinierte Element <person-details>. In diesem Fall fügen wir Stile mit JavaScript hinzu, obwohl wir sie auch in einem <style>-Block innerhalb des <template> mit dem gleichen Effekt hätten hinzufügen können:
customElements.define(
"person-details",
class extends HTMLElement {
constructor() {
super();
const template = document.getElementById("person-template");
const shadowRoot = this.attachShadow({ mode: "open" });
const 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(document.importNode(template.content, true));
}
},
);
Wenn Sie das style-Element mit Inhalt füllen, werden Sie sehen, dass wir alle geslotteten Elemente (::slotted(*)) auswählen und ihnen eine andere Schriftart und Farbe geben. Dies differenziert sie von den Slots, die nicht gefüllt wurden. Wir haben alle geslotteten <span>s (::slotted(span)) gestylt, um die <span>s von den <p>s zu unterscheiden.
Unser Markup umfasst drei benutzerdefinierte Elemente, einschließlich eines benutzerdefinierten Elements mit einem ungültigen Slot-Namen in einer Quellreihenfolge, die vom <template> abweicht:
<person-details>
<p slot="person-name">Wonder Woman</p>
<span slot="person-age">Immortal</span>
<span slot="person-occupation">Superhero</span>
</person-details>
<person-details>
<p slot="person-name">Malala Yousafzai</p>
<span slot="person-age">17</span>
<span slot="person-occupation">Activist</span>
</person-details>
<person-details>
<span slot="person-age">44</span>
<span slot="not-a-slot-name">Time traveler</span>
<p slot="person-name">Dr. Who</p>
</person-details>
Ergebnis
Spezifikationen
| Specification |
|---|
| CSS Scoping Module Level 1> # slotted-pseudo> |
Browser-Kompatibilität
Siehe auch
:host:host():host-context():has-slotted- CSS-Scoping Modul
- HTML
slotAttribut - HTML
<slot>Element - HTML
<template>Element - Web-Komponenten