::slotted()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2020年1月.
:slotted() CSS 伪元素用于选定那些被放在 HTML 模板中的元素(更多请查看使用模板和插槽)。
这个伪元素选择器仅仅适用于影子 DOM(shadow DOM)。请注意它只会选择实际的元素节点,而不包括文本节点。
尝试一下
/* 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(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; }); } }, ); css
/* 选择插槽内容任意元素 */ ::slotted(*) { font-weight: bold; } /* 选择插槽内的任意 <span> 元素 */ ::slotted(span) { font-weight: bold; } 语法
css
::slotted(<compound-selector>) { /* ... */ } 示例
>高亮插槽元素
在下面的例子中,我们使用一个带有 3 个插槽的模板:
html
<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> 我们定义 <person-details> 自定义元素,在这个示例中,我们使用 Javascript 添加样式,尽管我们将它们添加到 <template> 中的 <style> 块可达到同样的效果。
js
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)); } }, ); 当填充 style 元素生效时,你会看到我们选择了所有插槽元素(::slotted(*)),并给它们不同的字体和颜色。这将它们与尚未填充的插槽区分开来。我们对所有插槽 <span>(::slotted(span))设置样式,从而区分 <span>元素 和 <p> 元素。
标记三个自定义元素,其中包含一个具有无效槽名的自定义元素,该自定义元素与 <template> 不同:
html
<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 traveller</span> <p slot="person-name">Dr. Who</p> </person-details> 结果
规范
| Specification |
|---|
| CSS Scoping Module Level 1> # slotted-pseudo> |
浏览器兼容性
参见
:host:host():host-context()- CSS 域模块
- HTML
slot属性 - HTML
<slot>元素 - HTML
<template>元素 - Web 组件