Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 39 ยท Templates and Slots

Named Slots

Build components with several content areas using named slots. Learn the name and slot attributes, how unmatched content is handled, and build a reusable card.

One slot is enough for a frame or a quote. Most real components have several regions: a card has a header, a body and a footer; a dialog has a title, content and actions. Named slots let the author direct each child to the right region.

Name and slot

Two attributes cooperate:

  • On the component side, <slot name="header"></slot> declares a region called header.
  • On the author side, any child with slot="header" is placed there.

Children without a slot attribute go to the default slot (the one with no name), if the component has one.

A card with three regions

The order the author writes the children in does not matter: the <a slot="footer"> is last in the source but renders in the footer region, and the header text renders first. The component controls layout; the author controls content.

Fallback content works per slot

Every slot, named or not, can contain fallback content. In the example, the second card has no body, so the default slot is empty; the first slot used its own text. If you want to show a message for missing parts, put it inside the slot element.

Assignment rules

The browser follows these rules for each child of the host:

  1. If the child is an element with a slot attribute, it is assigned to the first slot in the shadow tree (in tree order) whose name equals that value.
  2. If no slot matches, the child is not rendered at all. It stays in the DOM but is not displayed.
  3. Elements without a slot attribute, and all text nodes, go to the default slot (a <slot> with no name or an empty name).
  4. If there is no default slot, those nodes are not rendered.
flowchart LR
  A["Child element"] --> B{"Has slot attribute?"}
  B -->|"No"| C{"Default slot exists?"}
  C -->|"Yes"| D["Rendered in default slot"]
  C -->|"No"| E["Not rendered"]
  B -->|"Yes"| F{"Slot with that name?"}
  F -->|"Yes"| G["Rendered in that slot"]
  F -->|"No"| E

Every child ends in exactly one place, or nowhere.

A consequence: only direct children of the host can be slotted. A slot attribute on a grandchild is ignored.

Unmatched content is not rendered

The misspelled typo span stays in the DOM (the query finds it) but is not rendered, and assignedSlot is null. If a piece of content mysteriously never appears, check the slot names for typos.

Slot ordering and duplicates

Several children can share one slot name. They are all assigned to that slot, in document order. If two <slot> elements in the shadow tree have the same name, only the first receives nodes (a later slot stays empty and shows its fallback).

HTML
<info-card>
  <a slot="footer" href="#a">One</a>
  <a slot="footer" href="#b">Two</a>
</info-card>

Both links render in the footer, one after the other.

Designing a good slot API

  • Document slot names like attributes: they are part of your public API.
  • Prefer few, meaningful names: header, footer, actions, icon, title.
  • Keep a default slot for the main content so simple uses need no slot attribute.
  • Provide fallback where a region is optional but should look right when empty.
  • Hide empty regions. An empty <footer> still takes padding. Listen for slotchange (next lesson) and set a has-footer attribute on the host, then hide the region with :host(:not([has-footer])) footer { display: none }.

Passing slots through nested components

A component can forward its slot into another component's slot by putting its own <slot> as the child of the inner component:

JavaScript
// Outer's shadow tree:
// <inner-box><slot name="title" slot="header"></slot></inner-box>

The <slot> is itself a slottable, so it can carry a slot attribute. The flattened result chains content from the page author through both components.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does a page author send an element to the slot named footer?

    The slot attribute on the child matches the name attribute of the slot.
  2. 2. What happens to a child whose slot attribute matches no slot name?

    Unmatched children are unassigned: still present in the light DOM, but not displayed.
  3. 3. Which nodes can be assigned to a slot?

    Slotting applies to direct children (elements and text) of the shadow host.
  4. 4. Does the order of children in the source decide the order of regions?

    Named slots separate content order from layout, so the component controls the arrangement.

Summary

  • Declare regions with <slot name="x"> and fill them with slot="x" on direct children.
  • Children without a slot attribute (and text) go to the unnamed default slot.
  • Unmatched or unslotted children stay in the DOM but are not rendered.
  • Slots can have fallback content; duplicate slot names leave later slots empty.
  • Treat slot names as public API and keep the set small.

Next: slotchange and assignedNodes