Slotted Nodes
a.k.a distributed nodes.
Web Components ⟩ Shadow DOM ⟩ Using Slots
📗 JS.info ⟩ Styling slotted content
👉 Styling in Shadow DOM - :host, :host(), :host-context(), ::slotted()
Nodes (in light DOM) that can be inserted into <slot> are known as slottable nodes; when a node has been inserted in a <slot>, it is said to be slotted.
An unnamed
<slot>
will be filled with all of the custom element's top-level child nodes that do not have theslot
attribute. This includes text nodes. 📘 Using templates and slots
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 Contents
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
Styling Slotted Content
use document (outer) styles (in light DOM).
style <slot> (in shadow DOM) and rely on CSS inheritance.
use ::slotted(«selector») in shadow DOM.
⭐️ use CSS variables: CSS variables exist on all levels, both in light and shadow. 👉 CSS hooks with custom properties
Last updated