Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 38 ยท Events and Slots

Slots and Component Composition in Lit

Compose Lit components with the slot element: default and named slots, fallback content, slotchange events, assignedElements, ::slotted styling and common slot pitfalls.

Properties and attributes pass data into a component. Slots pass markup. A card, a dialog or a tab panel does not know what its content will be: text, an image, a form. With a <slot> element in the shadow DOM, the component marks where the user's own children should appear, while still controlling the surrounding structure and styles.

If you know children from other frameworks, a slot is the browser-native version, with the bonus that the content stays in the page's regular DOM.

The default slot

Children of a custom element, written between its tags, are called light DOM. A <slot> in the component's shadow DOM is the hole where they are displayed.

Default slot

The text and the <strong> element are not moved: they remain children of <info-card> and are only rendered at the slot's position. That has two consequences: page CSS and JavaScript still reach them, and they are styled by the page's stylesheets, not the component's shadow styles.

Named slots

Give a slot a name and send content to it with the slot attribute. Anything without a slot attribute goes to the unnamed (default) slot.

Named slots

The order of children in the HTML does not matter; the layout comes from the order of slots in the template. Several elements may share one named slot, and they appear in source order.

Slots in one picture

Light DOM Children you write in the page
slot element A hole in the shadow DOM
Flattened tree What the browser renders

Children are rendered at the slot but stay in the page DOM.

1 / 2

Fallback content

Anything placed inside a <slot> in the template is fallback content, shown only when nothing is assigned. It is perfect for sensible defaults.

Fallback content

Reacting to slot changes

Slotted content can change after render: items added by script, or children appended by a framework. The slotchange event fires on the <slot> whenever its assigned nodes change, including once at the start if any content is assigned. Use it to count items or hide empty wrappers.

slotchange

assignedElements() returns only elements (no text nodes), assignedNodes() returns everything. { flatten: true } replaces nested <slot> elements by whatever is assigned to them, which matters when components wrap other components.

Styling slotted content

Slotted nodes belong to the outer document, so the component's shadow styles normally do not apply to them. Two tools let a component style what it receives:

  • ::slotted(selector): styles top-level slotted elements matching the selector. It cannot style their descendants.
  • Inheritable CSS properties (color, font, line-height) flow from the host into slotted content automatically.
::slotted

The Shadow DOM and Styling chapter of this track covers styling in depth; here the key point is that ::slotted is the component's way in, while the page's own CSS always has the final say (page styles beat ::slotted rules of the same specificity).

Slots versus properties

Use a slot when Use a property when
The content is markup the author writes The value is data (a string, number, object)
The author needs full control (links, icons, components) The component formats it for them
Content may be rich and unpredictable You need to read or validate it in JavaScript

A common mix: label as a property for simple text and a label slot for rich content, with the slot as an override.

Common mistakes

  • Typing slot="Title" when the slot is named title: names are case-sensitive.
  • Expecting a slot to wrap the content in new elements. A slot adds no element of its own; wrap it in your own <div> if you need a box.
  • Putting slot="x" on a grandchild: only direct children of the host can be slotted.
  • Reading this.children instead of the slot's assignedElements() after render for decisions about display.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where do slotted children live in the DOM?

    Slotting only affects rendering; the nodes remain children of the host, so page CSS and scripts still reach them.
  2. 2. What appears if no content is assigned to a <slot> that has children in the template?

    Content inside a slot is fallback, rendered only when nothing is assigned.
  3. 3. How do you send an element to <slot name="actions">?

    The slot attribute on a direct child of the host selects the named slot.
  4. 4. Which event reports that a slot's assigned nodes changed?

    slotchange fires on the slot element when its assigned nodes change.

Summary

  • A <slot> renders the host's light DOM children inside the shadow DOM; the children stay in the page.
  • Named slots receive children with a matching slot attribute; unlabeled children go to the default slot.
  • Content inside a <slot> is fallback shown when nothing is assigned.
  • Listen to slotchange and use assignedElements({ flatten: true }) to react to content.
  • Use ::slotted() for top-level slotted elements; the page's own CSS wins ties.

Next: Component Communication Patterns