Course contentsShow
← Web Development
Lesson 2467 of 2,98244. Web ComponentsFree lesson

Styling Slotted Content with ::slotted()

Targeting light DOM elements distributed into slots from within shadow DOM styles.

Styling Slotted Content with ::slotted()

You'll learn: How to style light DOM elements that are distributed into your shadow DOM's <slot> elements using the ::slotted() pseudo-element.

The Problem with Slot Content

When you use slots in shadow DOM, you're allowing users to pass their own content (light DOM) into your component. Normally, shadow DOM styles can't reach out and style light DOM elements—that would break encapsulation. But what if you want to apply some basic styling to slotted content from within your component?

Enter ::slotted()

The ::slotted() pseudo-element gives you a limited, safe way to style light DOM elements that have been distributed into a slot. Think of it as a one-way styling window: your shadow DOM can apply styles to slotted elements, but only to the direct children of the slot.

Example:

/* Inside your shadow DOM's <style> */
::slotted(*) {
  color: blue;
  margin: 0.5rem;
}

::slotted(p) {
  font-style: italic;
}

This targets any element slotted in (*), or specifically <p> tags that are slotted.

Important Limitations

::slotted() can only select the direct children that are placed into the slot—not their descendants. So ::slotted(p span) won't work. It's a shallow selector by design to maintain encapsulation boundaries. The slotted elements still live in the light DOM, so external styles continue to apply. ::slotted() styles have lower specificity than styles from the light DOM's own stylesheets.

Practical Use Case

Use ::slotted() to provide default spacing, colors, or alignment for user-provided content while still allowing them to override it with their own styles.

Key Takeaway: ::slotted() lets you style direct children distributed into slots from within shadow DOM, providing limited, encapsulation-safe styling control over slotted light DOM content.