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

Styling Slotted Content with ::slotted()

Applying Shadow DOM styles to projected Light DOM elements using the ::slotted() pseudo-element with selector restrictions.

Styling Slotted Content with ::slotted()

You'll learn how to apply Shadow DOM styles to Light DOM elements that have been projected into slots using the ::slotted() pseudo-element.

What Is ::slotted()?

When you use slots to project content from the Light DOM into your Shadow DOM, that content normally keeps its original styling context. The ::slotted() pseudo-element lets you apply Shadow DOM styles to those projected elements—but with important restrictions.

Think of it like a doorman at an exclusive club: you can check what guests are wearing as they enter (the top-level slotted elements), but you can't dictate what their friends inside are wearing (no access to descendants).

Selector Restrictions

The ::slotted() pseudo-element only targets direct children assigned to slots. You can use simple selectors like tag names, classes, or attributes, but you cannot select nested elements inside the slotted content.

/* Inside Shadow DOM styles */

/* ✅ Valid: targets top-level <p> elements in slots */
::slotted(p) {
  color: blue;
  margin: 1em 0;
}

/* ✅ Valid: targets elements with specific class */
::slotted(.highlight) {
  background: yellow;
}

/* ❌ Invalid: cannot target descendants */
::slotted(div span) {
  /* This won't work! */
}

Why the Restrictions?

This limitation maintains the separation between Shadow and Light DOM. The component can style the wrapper of slotted content, but the content's internal structure remains under the control of whoever provided it. This respects encapsulation boundaries while allowing reasonable styling control.

Key Takeaway: Use ::slotted(selector) to style only the top-level elements projected into your slots; you cannot reach into their descendants due to Shadow DOM's encapsulation rules.