Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 39 ยท Templates and Slots

Styling Slotted Content

Style slotted elements from three places: the page, the component with ::slotted(), and with inheritance. Learn ::slotted limits, specificity, and the slot pseudo-class.

Slotted nodes live in two worlds. They belong to the light DOM, so the page's CSS applies to them. But they are rendered inside the component's shadow tree, so the component can also style them, with a special pseudo-element. Understanding who can style what, and who wins, avoids hours of "why is my CSS ignored?" debugging.

Who can style slotted nodes?

Source How Notes
The page Normal selectors such as my-card h3 { ... } Works because slotted nodes are in the page's tree
The component ::slotted(selector) inside its shadow CSS Only matches the slotted nodes themselves
Inheritance Inherited properties flow from the slot's ancestors in the shadow tree color, font-family, etc.

::slotted()

Inside the shadow root's <style>, ::slotted(selector) matches elements assigned to a slot, provided they match the selector in parentheses. The selector must be a compound selector (one element, class, attribute or pseudo-class sequence, no combinators).

CSS
::slotted(*)          { margin: 0; }           /* every slotted element */
::slotted(h3)         { color: #7c3aed; }
::slotted(.lead)      { font-size: 1.2em; }
::slotted([slot="actions"]) { display: flex; gap: .5rem; }
slot[name="icon"]::slotted(img) { width: 1.5rem; }

You can scope to a particular slot by writing the slot selector before it, as in the last line.

::slotted in action

Both sources styled the paragraphs: the page's dotted underline and the component's margin and weight. The heading color came from ::slotted(h3).

Limitations of ::slotted

  1. Only the slotted node itself, not its descendants. ::slotted(p) span does not work, and ::slotted(div) p neither. To style inside a slotted node, ask the page author to style it, or use inherited properties.
  2. Compound selector only: ::slotted(.a .b) is invalid.
  3. Lowest priority against the page. Page rules from the document override ::slotted() rules when they target the same property, regardless of specificity, because outer-scope styles win over inner-scope normal declarations (with !important the order reverses). So ::slotted is another way for a component to set defaults the page can override.
The page wins over ::slotted

The first span is crimson even though ::slotted(span) is more "specific" in intent. Document styles win. Use this to design: choose components' slot styles as sensible defaults and let sites override them.

Inheritance through slots

Slotted nodes inherit from the slot's parent chain in the flat tree, not from the host's light DOM parent. If the slot sits inside a <div style="color: teal"> in the shadow tree, slotted text inherits teal. A color set on the host's light DOM ancestors is not consulted for the slotted nodes, because the slot's shadow-tree ancestors come first in the flat tree; only a color set directly on the slotted element itself would override teal.

Which parent do slotted nodes inherit from?

The text is seagreen: the slot's shadow-tree parent decides, not the crimson light-DOM ancestor, because inheritance follows the flat tree.

Styling the slot element itself

The <slot> element generates no box by default: it is display: contents. Styling its display is possible, but most of its look should come from its parent wrapper inside the shadow tree. Use classes on the wrapper (<div class="body"><slot></slot></div>) and style that.

Detecting empty slots for styling

To hide a region when nothing was slotted, combine slotchange and a state attribute (from the previous lesson):

JavaScript
slot.addEventListener("slotchange", () => {
  this.toggleAttribute("has-footer", slot.assignedNodes().length > 0);
});
CSS
:host(:not([has-footer])) footer { display: none; }

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which pseudo-element lets a component style the nodes assigned to its slots?

    ::slotted() matches the slotted elements themselves from inside the shadow tree.
  2. 2. Does ::slotted(p) span style spans inside slotted paragraphs?

    The pseudo-element cannot be followed by descendant selectors, so only the top-level slotted element is selected.
  3. 3. If the page and ::slotted() set the same property without !important, who wins?

    Normal outer-scope declarations override inner-scope ones, so ::slotted rules act as defaults.
  4. 4. Slotted text inherits properties from...

    Inheritance follows the flat tree, so the slot's ancestors inside the shadow tree are the parents.

Summary

  • Slotted nodes can be styled by the page (light DOM) and by the component with ::slotted().
  • ::slotted() takes one compound selector and matches only the slotted node, not its children.
  • Page styles override ::slotted rules, so use them for defaults.
  • Slotted nodes inherit through the flat tree, from the slot's shadow-tree ancestors.
  • Hide empty regions with slotchange and a state attribute on the host.

Next: Attributes vs Properties in Custom Elements