Sub topic 2
The Default Slot
A slot is a placeholder in a child component's template. A parent component fills it with markup. The default slot is the simplest kind: one unnamed placeholder that receives everything the parent puts between the child's opening and closing tags, unless that content is directed elsewhere.
This chapter assumes you can already:
- Build a Lightning web component with an HTML template, a JavaScript class, and a CSS file.
- Use a child component in a parent template (for example
<c-info-box>). - Expose a public property with
@api. - Handle DOM events with
onclick={handler}.
Why the default slot exists
Suppose you build a bordered panel that you want to use on many pages. Each page puts different things inside it. With public properties alone, you would need a property for every kind of content: title, message, buttonLabel, and so on. Properties pass values, not structure. They cannot easily carry a paragraph, a button with its own handler, and a list in one go.
A slot reverses the arrangement:
- The child decides the layout: the border, padding, and surrounding elements.
- The parent decides the content: whatever markup it wants inside.
The default slot is the entry point for this pattern. It covers the common case of a wrapper around "whatever the parent gives me."
A first example
The child component infoBox has a wrapper element and one <slot> element.
<!-- infoBox/infoBox.html -->
<template>
<section class="box">
<div class="body">
<slot></slot>
</div>
</section>
</template>/* infoBox/infoBox.css */
.box {
border: 1px solid #c9c9c9;
border-radius: 0.25rem;
}
.body {
padding: 1rem;
}// infoBox/infoBox.js
import { LightningElement } from 'lwc';
export default class InfoBox extends LightningElement {}The parent component orderPage places content between the <c-info-box> tags.
<!-- orderPage/orderPage.html -->
<template>
<c-info-box>
<p class="note">Items in cart: {count}</p>
<button onclick={handleAdd}>Add item</button>
</c-info-box>
</template>/* orderPage/orderPage.css */
.note {
color: #0b5cab;
font-weight: bold;
}// orderPage/orderPage.js
import { LightningElement } from 'lwc';
export default class OrderPage extends LightningElement {
count = 0;
handleAdd() {
this.count += 1;
}
}When the page renders, the paragraph and the button appear inside the div with class body, in the position where <slot></slot> sits. The border and padding come from infoBox. The blue bold text comes from orderPage. Clicking the button increments count, and the paragraph updates.
Note that infoBox has no @api properties and no knowledge of what it contains. That is the point of the default slot.
How content is matched to the default slot
The browser, through LWC's slot implementation, assigns each direct child of the child's host element to a slot. The rule is simple:
- A child node with a
slot="name"attribute goes to the slot with that name. Named slots are the subject of the next chapter. - Every other child node goes to the default slot. That means any node with no
slotattribute.
"Node" matters here. The default slot receives more than elements:
- Elements, such as
<p>or<c-other-component>. - Text, such as a bare
Hellobetween the tags. - Several siblings at once. All of them go into the same slot, in the order written.
This is valid, and both pieces are slotted:
<template>
<c-info-box>
Plain text works too.
<p>And so does an element.</p>
</c-info-box>
</template>Only direct children of the host are assigned to slots. If you write a <div> containing a <p>, the div is assigned to the slot and the p travels along inside it. The p is not assigned on its own.
Where the slot can go
The <slot> can sit at any depth in the child's template, wrapped in as many elements as you like. In the example above it sits inside div.body, inside section.box. The content appears wherever the slot is. The parent has no control over that position.
One default slot per template
Use at most one unnamed <slot> in a template. Each node can be assigned to only one slot, so a second unnamed slot would never receive anything. If you need content in more than one place, use named slots.
What happens when nothing is passed
If the parent writes <c-info-box></c-info-box> with no children, the slot is empty and renders nothing. The wrapper elements in the child's template still render, though.
In infoBox, that means an empty div.body with 1rem of padding inside a bordered box. The user sees an empty bordered rectangle. This is a common surprise. The next chapters cover how to show fallback content and how to hide wrappers when the slot is empty (using the slotchange event).
What happens when the child has no slot
If the parent passes children to a component whose template has no <slot> element, the content is not displayed. LWC does not raise an error. The parent's markup simply has nowhere to appear.
When slotted content is missing, check the child's template first. Look for a <slot> before debugging anything else.
Dynamic content in the default slot
Because the slotted markup is part of the parent's template, all the usual template features work in it:
<!-- orderPage/orderPage.html -->
<template>
<c-info-box>
<template lwc:if={hasItems}>
<ul>
<template for:each={items} for:item="item">
<li key={item.id}>{item.name}</li>
</template>
</ul>
</template>
<template lwc:else>
<p>Your cart is empty.</p>
</template>
</c-info-box>
</template>// orderPage/orderPage.js
import { LightningElement } from 'lwc';
export default class OrderPage extends LightningElement {
items = [
{ id: '1', name: 'Notebook' },
{ id: '2', name: 'Pen' }
];
get hasItems() {
return this.items.length > 0;
}
}When items changes, the parent re-renders and the content inside the slot updates. The child does not need to do anything. The infoBox is unaware of the list, the conditional, or the data.
Who owns the slotted content
Slotted content appears inside the child's layout, but it belongs to the parent. This ownership decides how three things behave. A later chapter treats it in detail. Here is what you need for the default slot.
| Aspect | Behavior for default-slot content |
|---|---|
| Data binding | Expressions such as `{count}` are evaluated against the parent's class. The child cannot see `count`. |
| Event handlers | `onclick={handleAdd}` calls the parent's `handleAdd`. The handler runs with the parent as `this`. |
| Styling | The parent's CSS file styles the slotted elements. The child's CSS does not reach them by ordinary selectors. |
In the first example, .note is defined in orderPage.css, and it works. If you moved .note into infoBox.css, the paragraph would stay unstyled. The child's stylesheet applies to elements in the child's own template, and the paragraph is not one of them.
Reaching slotted content from the child
Because the nodes belong to the parent, the child does not find them with this.template.querySelector(). That method searches the child's own template. To look at slotted elements, the child uses this.querySelector(), or it asks the <slot> element for its assigned nodes. Later chapters walk through both approaches. For now, remember that a failing this.template.querySelector('.note') inside infoBox is expected behavior.
Default slot or public property?
Use the default slot when:
- The parent should provide structure, not only a value (headings, buttons, lists, other components).
- The content may include its own handlers or bindings.
- The child is a layout shell: card, panel, modal body, tab content.
Use a public property when:
- The value is plain data, like a label, a number, or a record ID.
- The child needs to understand the value, for example to format it or validate it.
- The child must control how the value looks.
A rough test: if the child's JavaScript would need to read the value, a property is usually the better fit. If the child only needs to display whatever it is given, a slot is usually better.
Edge cases and gotchas
- Whitespace and text nodes. Spaces and line breaks between tags generally do not become visible content, but the browser can still count text nodes as assigned nodes. Do not assume that a non-empty list of assigned nodes means visible content. The
slotchangechapter returns to this. - Content with an unmatched
slotattribute. A node withslot="footer"does not go to the default slot. If the child has no slot namedfooter, that node is not displayed. It also does not fall back to the default slot. - Wrapper elements are always rendered. As shown earlier, an empty slot leaves the child's wrapper elements on the page.
- Putting the slot in the wrong template. The
<slot>must be in the template of the child that receives content. Placing it in the parent does nothing for the child. - Static
classon the slot element. The<slot>element is a placeholder. Do not put styling classes on it and expect the projected content to inherit them. Style a wrapper element around the slot instead, asdiv.bodydoes in the example. - Expecting the child to react to changes automatically. The parent's changes re-render the slotted nodes, but the child's JavaScript is not notified. If the child needs to know, it listens for
slotchange.
Quick checklist for using the default slot
- Add one
<slot></slot>to the child's template, inside the wrapper where the content should appear. - In the parent, write the content between the child's opening and closing tags, with no
slotattribute. - Put handlers and data in the parent's JavaScript, and style in the parent's CSS.
- Check how the child's wrapper looks when the slot is empty.
Exercise
A developer writes <c-info-box><p class="note">Hello</p></c-info-box> and defines .note { color: red; } in infoBox.css, the child's stylesheet. The text appears in the default color. Why, and where should the rule go?
Show answer
The <p> is written in the parent's template, so the parent owns it. The child's stylesheet applies to elements in the child's own template, not to slotted content. Move the .note rule into the parent component's CSS file, where it will apply.
Exercise
The child infoBox template contains <div class="body"><slot></slot></div>. The parent renders <c-info-box></c-info-box> with nothing inside. What does the user see, and why?
Show answer
The user sees the empty bordered box with its padding. The slot has no content, but the section and the div.body around it are part of the child's template, so they render anyway.
Exercise
A parent passes <p>Total: {total}</p> into a child's default slot. The child's JavaScript class has no total property. Does the expression work? Whose property does it read?
Show answer
It works. The expression is evaluated in the parent's context, so it reads the parent's total property. The child does not need to know about it.