Sub topic 5
Who owns slotted content: styling, data, and events
This chapter assumes you can build a basic Lightning web component (HTML template, JavaScript class, CSS file), use a child component in a parent template, and handle DOM events. It also assumes you have seen a default slot and named slots. Here you will learn which component is responsible for the content that fills a slot.
The rule: the parent owns it
Slotted content is written in the parent's template. The child only decides where it appears. Everything about that markup therefore belongs to the parent: its CSS scope, its data bindings, and its event handlers.
Here is a parent that fills the card component's slots:
<!-- parent.html -->
<template>
<c-card>
<span slot="header" class="title">Order summary</span>
<p class="count">{itemCount} items in your cart.</p>
<lightning-button slot="footer" label="Checkout" onclick={handleCheckout}></lightning-button>
</c-card>
</template>// parent.js
import { LightningElement } from 'lwc';
export default class Parent extends LightningElement {
itemCount = 2;
handleCheckout() {
this.itemCount = 0;
}
}/* parent.css */
.title {
font-weight: bold;
}
.count {
color: #444444;
}The next diagram shows where each piece lives.
Styling
The .title and .count rules above live in parent.css, so they apply to the slotted elements. A rule in the child's CSS file is scoped to the child's own template. This card.css rule will not color the paragraph from the parent:
/* card.css */
.body p {
color: red;
}The child's .body div is in the child's template, but the <p> was created by the parent. If the child needs to style the top-level elements that arrive in a slot, it can use the ::slotted() pseudo-element, for example ::slotted(p). This selector only matches elements assigned directly to a slot. It cannot reach anything nested inside them. Check how it behaves with your org's shadow DOM mode before depending on it.
The more reliable pattern is to let the parent style its own content. If the child wants to influence the look, it can expose styling hooks such as CSS custom properties, which pass through the shadow boundary.
Data
The expression {itemCount} is evaluated in the parent's context. The child never sees itemCount and cannot read the parent's properties. When itemCount changes, the parent's template re-renders and the slotted <p> updates in place. The child does nothing.
This also means a child cannot hand data back into slotted markup. If the footer content needs a value that only the child knows, a slot is the wrong tool. The next chapter compares slots with public properties.
Events
Handlers are attached in the parent's template, so they run with the parent as this. In the example, clicking Checkout calls Parent.handleCheckout(), which changes itemCount on the parent. The child's code is not involved.
A native event from slotted content still bubbles through the child's slot position. If the child needs to know that something was clicked, it can listen on an element that contains the slot:
<!-- card.html (body section) -->
<div class="body" onclick={handleBodyClick}>
<slot></slot>
</div>// card.js
import { LightningElement } from 'lwc';
export default class Card extends LightningElement {
handleBodyClick(event) {
console.log('Something inside the body slot was clicked', event.target.tagName);
}
}Both handlers can run for the same click. The parent's onclick on the slotted element runs first, and the child's handler on the wrapper runs as the event bubbles up. Keep this in mind when the child reacts to events it did not define.
Summary of ownership
| Concern | Owner | Consequence |
|---|---|---|
| Markup | Parent | The parent decides which elements exist and which slot each one uses. |
| CSS | Parent | Child CSS does not style slotted elements, apart from `::slotted()` on top-level ones. |
| Data bindings | Parent | `{expression}` resolves against the parent's properties. |
| Event handlers | Parent | Handlers run with the parent as `this`. |
| Position and fallback | Child | The child decides where slotted content appears and what shows when a slot is empty. |
Exercise
A parent template contains <c-card><p class="note">{message}</p></c-card>. The file card.css has p { color: red; }, and parent.css has .note { color: green; }. Which file's rule applies to the paragraph, and where does {message} get its value?
Show answer
Only parent.css applies, because the paragraph belongs to the parent's template and the child's CSS is scoped to the child's own elements. The p rule in card.css does not match it. {message} is read from the parent component's message property.
Exercise
A slotted lightning-button has onclick={handleCheckout} in the parent's template. Which class defines handleCheckout, and what does this refer to inside it?
Show answer
The parent's class defines it, and this is the parent component instance. The child component may also see the click bubble through its slot, but it does not run handleCheckout.