# How does component composition work using slots in Lightning Web Components?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Slots, Component Composition

Read it on the site: https://www.forcechallenges.com/interview-questions/how-does-component-composition-work-using-slots-in-lightning-web-components

## Answer

**Basics:** Properties pass data; slots pass markup. A `c-card` component can define a place for a body and a header, and each parent decides what to put there. This is composition: reusable wrappers (cards, modals, layouts, error boundaries) with parent-supplied content.

**How it works:**
1. Child template: `<slot></slot>` defines the unnamed default slot; `<slot name="header"></slot>` defines a named slot.
2. Parent usage: any content inside `<c-card>` goes to the default slot; elements with `slot="header"` go to the named slot.
3. Fallback content placed inside `<slot>` appears only when the parent provides nothing.
4. The child can listen to `slotchange` to react when the assigned content changes.
5. The projected content belongs to the parent: its events, data bindings and CSS scope are the parent's.

**Advanced / deep dive:** Slotted content is created and owned by the parent, so the parent's styles apply to it, not the child's, and the parent's event handlers fire in the parent's context. The child can inspect slotted nodes through the slot element (`assignedElements()`) or `this.querySelector` for light-tree children, but should not depend on their implementation. A named slot can receive multiple elements; one element can fill only one slot. In synthetic shadow, a few details of slot behavior and `::slotted` styling differ from native shadow, so verify visually. Scoped slots (letting a child pass data back to slotted markup) exist via `lwc:slot-data`; check current release notes for availability and status. With light DOM components, slots behave differently (they use light DOM slotting and cannot use fallback in the same way), so check the documentation when using `lwc:render-mode="light"`. Slots combine well with `errorCallback` for boundary wrappers and with `lwc:if` for conditional regions.

**Code / config example:**
```html
<!-- card.html (child) -->
<template>
    <div class="slds-card">
        <header class="slds-card__header"><slot name="header">Default title</slot></header>
        <div class="slds-card__body"><slot></slot></div>
        <footer class="slds-card__footer"><slot name="footer"></slot></footer>
    </div>
</template>
```
```html
<!-- parent.html -->
<c-card>
    <span slot="header">Account Summary</span>
    <p>Body content goes to the default slot.</p>
    <lightning-button slot="footer" label="Save"></lightning-button>
</c-card>
```

**Hidden gotchas & must-remember points:**
- Slotted content is styled by the parent's CSS, not the child's.
- The `slot` attribute must be on a direct child of the component tag in the parent template.
- Fallback content shows only when nothing is assigned to that slot.
- A child cannot read slotted children as if they were its own template elements; use `slotchange` or `assignedElements()`.
- Duplicate slot names in one child template are not allowed.
- Slots are for markup; use `@api` properties for data and events for upward communication.

**Best practice:** Use slots for layout wrappers, keep named slots few and documented, and let the parent own slotted content.

---
Source: ForceChallenges · last updated 2026-11-02
