::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 å ã«é ç½®ããã CSS ã®ä¸ã§ä½¿ãããæã®ã¿æ©è½ãã¾ãããªãããã®ã»ã¬ã¯ã¿ã¼ã¯ã¹ãããå ã«é ç½®ãããããã¹ããã¼ãã¯é¸æãã¾ãããå®éã®è¦ç´ ã®ã¿ã対象ã«ãã¾ãã
試ãã¦ã¿ã¾ããã
/* 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;
});
}
},
);
/* ã¹ãããå
ã«é
ç½®ãããä»»æã®è¦ç´ ã鏿 */
::slotted(*) {
font-weight: bold;
}
/* ã¹ãããå
ã«é
ç½®ããã <span> è¦ç´ ã鏿 */
::slotted(span) {
font-weight: bold;
}
æ§æ
::slotted(<compound-selector>) {
/* ... */
}
ä¾
>ã¹ãããåãããè¦ç´ ã強調
ãã®ä¾ã§ã¯ã 3 ã¤ã®ã¹ããããæã¤åç´ãªãã³ãã¬ã¼ãã使ç¨ãã¾ãã
<template id="person-template">
<div>
<h2>å人 ID ã«ã¼ã</h2>
<slot name="person-name">æ°å䏿</slot>
<ul>
<li><slot name="person-age">年齢䏿</slot></li>
<li><slot name="person-occupation">è·æ¥ä¸æ</slot></li>
</ul>
</div>
</template>
ã«ã¹ã¿ã è¦ç´ <person-details> ã¯ä»¥ä¸ã®ããã«å®ç¾©ããã¦ãã¾ãããã®å ´åã JavaScript ã§ã¹ã¿ã¤ã«ã追å ãã¦ãã¾ããã <template> å
ã® <style> ãããã¯ã§è¿½å ãã¦ãåã广ãããã¾ãã
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> 㨠<p> ãåºå¥ããããã«ãã¹ãããä»ã <span> ããã¹ã¦ã¹ã¿ã¤ã«è¨å®ãã¾ãã (::slotted(span))ã
ãã®ãã¼ã¯ã¢ããã«ã¯ 3 ã¤ã®ã«ã¹ã¿ã è¦ç´ ãè¨è¼ããã¦ãã¾ãããã®ä¸ã«ã¯ã䏿£ãªã¹ãããåãæã¡ã <template> ã¨ã¯ã½ã¼ã¹é åºãç°ãªãã«ã¹ã¿ã è¦ç´ ãå«ã¾ãã¦ãã¾ãã
<person-details>
<p slot="person-name">ã¯ã³ãã¼ã¦ã¼ãã³</p>
<span slot="person-age">䏿»èº«</span>
<span slot="person-occupation">ã¹ã¼ãã¼ãã¼ãã¼</span>
</person-details>
<person-details>
<p slot="person-name">Malala Yousafzai</p>
<span slot="person-age">17</span>
<span slot="person-occupation">æ´»åå®¶</span>
</person-details>
<person-details>
<span slot="person-age">44</span>
<span slot="not-a-slot-name">ã¿ã¤ã ãã©ãã©ã¼</span>
<p slot="person-name">ãã¯ã¿ã¼ãã¼</p>
</person-details>
çµæ
仿§æ¸
| Specification |
|---|
| CSS Scoping Module Level 1 > # slotted-pseudo > |
ãã©ã¦ã¶ã¼ã®äºææ§
é¢é£æ å ±
:host:host():host-context()- CSS ã¹ã³ã¼ãã¢ã¸ã¥ã¼ã«
- HTML
slot屿§ - HTML
<slot>è¦ç´ - HTML
<template>è¦ç´ - ã¦ã§ãã³ã³ãã¼ãã³ã