๐ธslotted nodes
a.k.a distributed nodes.
Last updated
Was this helpful?
a.k.a distributed nodes.
Last updated
Was this helpful?
Was this helpful?
web โฉ component โฉ slot โฉ slotted nodes
Styling in Shadow DOM โฉ ::slotted()
Only custom element's top-level children (in the light DOM) may have [slot]
attributeโ๏ธ ๐ See "light DOM" tab in example code in Using Slots.
::slotted() only select slotted element itself, not its descendants (content)โ๏ธ
slotted content come from light DOM, they use document stylesโ๏ธ
shadow DOM styles do not affect slotted contentโ๏ธ
::slotted() only select slotted element itself, not its descendants (content)โ๏ธ
::slotted() can't go inside light DOM, for example: ::slotted(div) p is invalidโ๏ธ
::slotted() can't be used in .querySelector()โ๏ธ
ๅจไธ้ข็็ฏไพ็ขผไธญ๏ผๆๅๅๅฅๅจๅค้จๅๅ
ง้จ CSS ๅฐ <span>
ๅ
็ด ๅใ็ฒ้ซใ่ใ็ด
่ฒใๆ ผๅผ่จญๅฎ๏ผๅฐๆผๆๅ
ฅ shadow DOM<slot>
็ <span>
่่จ๏ผๅชๆๅค้จ็ใ็ฒ้ซใ่จญๅฎๆๆ (๐ see: "codepen" tab below)ใ ๐พ replit
๐พ ::slotted() lab
use document (outer) styles (in light DOM).
style <slot> (in shadow DOM) and rely on CSS inheritance.
use ::slotted(ยซselectorยป) in shadow DOM.
<!-- โญ๏ธ document (outer) styles -->
<style>
span {
font-weight: bold
}
</style>
<!-- โญ๏ธ custom element -->
<user-card>
<!-- โญ๏ธ slotted element -->
<div slot="username">
<!--
โญ๏ธ slotted content is affected by document (outer) styles,
not by local (shadow DOM) stylesโ๏ธ
-->
<span>John Smith</span>
</div>
</user-card>
<script>
customElements.define('user-card', class extends HTMLElement {
connectedCallback() {
this.attachShadow({mode: 'open'});
// โญ๏ธ shadow tree (with named <slot>)
// 1. slotted elements come from light DOM, they use document styles.
// 2. local styles do not affect slotted content.
this.shadowRoot.innerHTML = `
<style>
span { background: red; }
</style>
Name: <slot name="username"></slot>
`;
}
});
</script>
<!-- โญ๏ธ document (outer) styles -->
<style>
span {
font-weight: bold
}
/* โญ๏ธ select <span> in slotted <p> element */
p[slot] span {
background: hsl(90, 90%, 80%); /* light green */
padding: 0 8px;
}
</style>
<!-- โญ๏ธ custom element -->
<user-card>
<!-- โญ๏ธ slotted element -->
<div slot="username">
<!--
โญ๏ธ slotted content is affected by document (outer) styles,
not by local (shadow DOM) stylesโ๏ธ
-->
<span>John Smith</span>
</div>
</user-card>
<user-card>
<span slot="username">Jane Smith</span>
</user-card>
<user-card>
<p slot="username">
<span>Doctor Who</span>
</p>
</user-card>
<script>
customElements.define('user-card', class extends HTMLElement {
connectedCallback() {
this.attachShadow({mode: 'open'});
// โญ๏ธ shadow tree (with named <slot>)
// 1. slotted elements come from light DOM, they use document styles.
// 2. local styles do not affect slotted content.
// 3. ::slotted() only select slotted element itself, not its descendantsโ๏ธ
this.shadowRoot.innerHTML = `
<style>
:host {
border: 1px solid black;
margin: 4px;
padding: 4px;
display: inline-block;
box-shadow:
0 4px 8px 0 rgba(0, 0, 0, 0.4),
0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
:host(:hover) {
box-shadow:
0 2px 4px 0 rgba(0, 0, 0, 0.6),
0 3px 10px 0 rgba(0, 0, 0, 0.35);
}
::slotted(*) {
border: 1px solid black;
}
::slotted(span) {
background: pink;
padding: 0 6px;
}
::slotted(div) {
padding: 0 6px;
background: hsl(225, 80%, 90%);
}
</style>
Name: <slot name="username"></slot>
`;
}
});
</script>
<style>
/* โญ๏ธ 2. set css var with custom value */
user-card {
--user-card-field-color: green;
}
</style>
<template id="tmpl">
<style>
/* โญ๏ธ 1. use css var with default value */
.field {
color: var(--user-card-field-color, black);
}
</style>
<div class="field">Name: <slot name="username"></slot></div>
<div class="field">Birthday: <slot name="birthday"></slot></div>
</template>