completefrontend
Open lab My progress
THE WEB COMPONENTS FIELD GUIDE

Placing content with slots

Learn how web component slots work: the default slot, named slots with the slot attribute, fallback content, and which children can be slotted.

By the end, you can
  • 01
    Show the page's content in your componentPut a default <slot> and named slots in a shadow root, and fill them with slot attributes.
  • 02
    Give a slot fallback contentSay when the fallback shows, and why spaces between tags hide it in a default slot.
  • 03
    Know what can be slottedOnly the host's own element and text children go into slots. Spot the ones that never show.

What is a slot in web components?

Ravi's tea card at Leaf and Kettle has a shadow root now. Two attributes fill it: the tea's name and its price. Today Asha puts the Oolong on sale. It costs 4 instead of 5. She wants the old price crossed out on the card. She also wants a tasting note under it.

She tries tags inside the price attribute, and the card prints them as plain text. How can the page put its own markup inside the card, in the right place?

With a slot. A slot is a <slot> element inside a component's shadow root. It marks the place where the page's own content shows up: the text and elements written between the component's tags.

Here is what Asha tried first. The card comes from the tea shop kit, as lesson 7.3 left it:

Tags inside an attribute show as textPop out in the code editor (opens in a new tab)HTML
<tea-card name="Oolong" price="<s>5</s> 4"></tea-card> <script type="module">  import "./tea-kit/index.js";</script>
ResultLoading…
  • Line 1 puts <s>5</s> 4 in the price attribute. <s> is the HTML tag for crossed-out text.
  • Line 4 loads the kit, which defines tea-card.

The result shows Price: <s>5</s> 4, tags and all. The card writes each attribute with textContent, as in lesson 6.1. So an attribute only ever gives the card text. That keeps the card safe, as lesson 7.3 showed. But it also means the page can't pass markup.

A slot solves this. Here is the smallest one:

The page's text shows where the slot isPop out in the code editor (opens in a new tab)HTML
<tea-card>  Floral and sweet, with a honey finish.</tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <h3>Oolong</h3>        <p>Tasting notes: <slot></slot></p>`;    }  }  customElements.define("tea-card", TeaCard);</script>
ResultLoading…
  • Lines 1 to 3 write a card with text between its tags. That text is the card's child.
  • Line 9 gives the card a shadow root.
  • Lines 11 and 12 fill it: a heading, and a paragraph that ends with <slot></slot>.

The result shows Oolong, then Tasting notes: Floral and sweet, with a honey finish. The text from line 2 shows up where the slot is.

On the left, the page: a <tea-card> card with a "Floral and sweet…" card inside it, labelled stays in the page. On the right, a window called tea-card, the shadow tree, holds <h3> Oolong, <p> Tasting notes:, and an indented <slot>. An arrow labelled shown here goes from the text to the slot.the page<tea-card>"Floral and sweet…"stays in the pageshown heretea-card<h3> Oolong<p> Tasting notes:<slot>
Line 2's text stays inside the tag, in the page. The <slot> on line 12 shows it in the shadow tree, right after Tasting notes.

Remember lesson 6.1? Once a card has a shadow root, the browser draws the shadow tree instead of the card's children. Those children are the card's light DOM, as lesson 5.5 called them. Lesson 6.1 compared the shadow tree to a poster over a shop window, and a slot to a hole cut in the poster. Here is a second way to picture it:

Real-life analogyA slot is the pocket on a name badge

At a tea fair, every visitor gets the same badge holder, with the fair's logo printed on it. On the front is a clear pocket. You write your name on a card and slide it in. The holder decides where your name shows. The card is still yours: take it out, and the pocket is empty again.

In real life: The badge holder the fair printed
In JavaScript: The card's shadow tree, written by the component
In real life: The clear pocket on the front
In JavaScript: The <slot> element
In real life: The name card you write yourself
In JavaScript: The text and elements between the card's tags
In real life: Taking your card out again
In JavaScript: Removing the child: the slot has nothing to show
The main idea

A slot is a <slot> element in a shadow root. It shows the host's own children in that spot. Children with no slot attribute go to the default slot. A child with slot="price" goes to <slot name="price">. What's written inside a slot shows only when nothing goes there.

The host is the element that owns the shadow root, here the card. Every major browser has supported slots since January 2020. They're Baseline: widely available.

This lesson builds the card step by step: the default slot, named slots, fallback content, and what can go into a slot. At the end, the kit's own card gets slots. The Web Components lesson on the JavaScript path gives a shorter preview.

How does the default slot work?

A <slot> with no name is the default slot. Every child of the host that doesn't ask for a named slot goes there. The slot's place in the shadow tree decides where those children show.

Try it. The playground keeps the card from the overview, and moves its slot:

Playground: where does the slot go?
A card whose shadow tree puts its slot in different placesPop out in the code editor (opens in a new tab)HTML
<tea-card>  Floral and sweet, with a honey finish.</tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <h3>Oolong</h3>        <p>Tasting notes:</p>`;    }  }  customElements.define("tea-card", TeaCard);</script>
ResultLoading…
Try it yourself
The note isn't drawn.

The shadow tree has no <slot>, so nothing shows the card's own text. It's still inside the tag on line 2, as in lesson 6.1.

Only lines 11 and 12, the shadow tree's markup, change. Line 2's text is the same every time.
  • No slot shows only Oolong and Tasting notes. The note is still inside the tag, but nothing draws it.
  • In the paragraph shows the note right after Tasting notes, from line 12.
  • Before the heading shows the note at the top. Line 11 puts the slot before the <h3>.

The page wrote the same text each time. Only the shadow tree changed. The page decides what the content is, and the component decides where it goes.

Many children, one slot

A default slot takes every child with no slot attribute. That means text and elements, in the order the page wrote them.

Several children in one default slotPop out in the code editor (opens in a new tab)HTML
<tea-card>  <b>Floral</b> and sweet.  <i>Best without milk.</i></tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <h3>Oolong</h3>        <p><slot></slot></p>`;    }  }  customElements.define("tea-card", TeaCard);  console.log(document.querySelector("tea-card b").textContent);</script>
ResultLoading…
  • Line 2 is a bold word and some text. Line 3 is a sentence in italics.
  • Line 13 has the only slot, inside a paragraph.
  • Line 17 finds the bold word with document.querySelector(), and prints Floral.

The result shows Oolong, then Floral and sweet. Best without milk. on one line. The bold word, the text after it, and the italic sentence all went into the one slot, in page order.

Line 17 shows one more thing. The bold word is still in the page, where line 2 wrote it, so the page's own query finds it. The slot only shows it. The next lesson, Light DOM, shadow DOM, and the flat tree, looks at where slotted nodes really live.

Why it matters: the page writes plain HTML, and your component places it. A card, a dialog, or a tab panel can hold whatever content its users bring.

How do named slots work?

One slot isn't enough for Asha's card. The name belongs in the heading, and the price under it. So give each slot a name, and let each child say which one it wants. It takes two attributes:

  • In the shadow tree, <slot name="price"> makes a named slot called price.
  • In the page, slot="price" on a child asks for that slot.
A card with a name slot and a price slotPop out in the code editor (opens in a new tab)HTML
<tea-card>  <span slot="price">Price: 4</span>  <span slot="name">Green tea</span></tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <h3><slot name="name"></slot></h3>        <p><slot name="price"></slot></p>`;    }  }  customElements.define("tea-card", TeaCard);</script>
ResultLoading…
  • Line 2 is a span that asks for the price slot. Line 3 asks for the name slot.
  • Line 12 puts the name slot inside the heading.
  • Line 13 puts the price slot inside a paragraph.

The result shows Green tea as the heading, and Price: 4 under it. The page wrote the price first, but the card shows the name first. Each child goes to the slot with its name. The slots' order in the shadow tree decides what you see.

On the left, the page in its order: <span slot="price"> on top, then <span slot="name">. On the right, a window called tea-card holds <slot name="name"> on top and <slot name="price"> below. Two crossing arrows go from each span to the slot with its name. Under the window: the shadow tree's order wins.the page, in order<span slot="price"><span slot="name">tea-card<slot name="name"><slot name="price">the shadow tree's order wins
Line 2 writes the price first. Each span goes to the slot with its name, and the shadow tree has the name slot first. So Green tea shows on top.
Real-life analogyNamed slots are the spaces in a bento box

Leaf and Kettle sells lunch in bento boxes. Each box has spaces with labels: rice, pickles, and one big space with no label. The cook reads the label on each tub and puts the food in the space with the same label. Food with no label goes in the big space. A tub labelled soup stays on the counter, because no space says soup.

In real life: A space with a label
In JavaScript: A named slot, like <slot name="price">
In real life: The label on a tub
In JavaScript: The slot attribute on a child
In real life: The big space with no label
In JavaScript: The default slot
In real life: The soup tub left on the counter
In JavaScript: A child whose slot name matches no slot

The slot attribute is a global attribute. Every HTML element has it, like id and class. It arrived in the same browser versions as <slot>:

Now pick a slot for a paragraph. This card has three: a name slot, a price slot, and a default slot.

Playground: which slot does the paragraph go to?
A card with a name slot, a price slot, and a default slotPop out in the code editor (opens in a new tab)HTML
<tea-card>  <span slot="name">Green tea</span>  <span slot="price">Price: 4</span>  <p>Floral and sweet</p></tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <h3><slot name="name"></slot></h3>        <div><slot name="price"></slot></div>        <slot></slot>`;    }  }  customElements.define("tea-card", TeaCard);</script>
ResultLoading…
Try it yourself
It goes to the default slot.

The paragraph has no slot attribute, so it goes to the <slot> with no name, on line 15. That's under the price.

Only line 4 changes: the paragraph's slot attribute. The card has three slots, on lines 13 to 15.
  • No slot attribute puts the paragraph in the default slot, under the price.
  • slot="name" puts it in the heading, after Green tea. Two children share the name slot, in page order.
  • slot="price" puts it after the price. One slot can hold many children.
  • slot="Price" hides it. Names must match exactly, capital letters too.

Children with no matching slot

What happens to a child whose slot doesn't exist? It isn't deleted. It just has no place to show.

A child that asks for a slot nobody hasPop out in the code editor (opens in a new tab)HTML
<tea-card>  <span slot="name">Green tea</span>  <span slot="photo">A photo of the leaves</span></tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `<h3><slot name="name"></slot></h3>`;    }  }  customElements.define("tea-card", TeaCard);   const photo = document.querySelector('[slot="photo"]');  console.log(photo.isConnected);  console.log(photo.textContent);</script>
ResultLoading…
  • Line 3 asks for a photo slot. Line 11 has only a name slot.
  • Line 16 finds the photo span in the page.
  • Line 17 prints true. The span is still in the page, as isConnected tells you.
  • Line 18 prints A photo of the leaves. Its text is all there.

The result shows only Green tea. Why it matters: a typo in a slot name doesn't throw an error. The content just vanishes from the screen. When something doesn't show, compare the two names letter by letter.

What is fallback content in a slot?

A slot can hold content of its own, between <slot> and </slot>. That's its fallback content. It shows only when nothing from the page goes into the slot.

A node that goes into a slot is assigned to it, or slotted. The HTML standard puts the rule in one line: a slot shows its assigned nodes, if it has any, and its own contents if not. Ravi's menu has a new tea, Chai, with no price yet:

Two cards, one with no pricePop out in the code editor (opens in a new tab)HTML
<tea-card>  <span slot="name">Green tea</span>  <span slot="price">Price: 4</span></tea-card><tea-card>  <span slot="name">Chai</span></tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <h3><slot name="name">A new tea</slot></h3>        <p><slot name="price">Ask at the counter</slot></p>`;    }  }  customElements.define("tea-card", TeaCard);</script>
ResultLoading…
  • Lines 1 to 4 are a card with a name and a price.
  • Lines 5 to 7 are a card with only a name.
  • Line 15 gives the name slot the fallback A new tea. Line 16 gives the price slot Ask at the counter.

The first card shows Green tea and Price: 4. Its slots got children, so their fallback stays hidden. The second card shows Chai and Ask at the counter. Nothing asked for its price slot, so the slot shows its own text.

The rule keeps working as the page changes. Remove the price span from Green tea's card, and Ask at the counter shows at once. Add a price span to Chai's card, and its fallback hides. You never have to tell the slot.

Real-life analogyFallback content is the sample photo in a new frame

Asha buys a photo frame for the shop. It comes with a sample photo inside. The sample shows until she slides in her own photo of the tea garden. If she takes hers out, the sample shows again.

In real life: The frame
In JavaScript: The slot
In real life: The sample photo it came with
In JavaScript: The fallback content, written inside <slot>
In real life: Asha's own photo
In JavaScript: A child of the host that goes into the slot
In real life: Taking her photo out again
In JavaScript: Removing the child: the fallback shows again

Fallback content can be any markup, not just text. MDN's example puts a whole paragraph inside a slot as its fallback.

Why doesn't my default slot show its fallback?

This one surprises almost everyone. Give the default slot a fallback, and it often never shows, even when the card looks empty. Try it:

Playground: is the card empty?
A card whose default slot has fallback contentPop out in the code editor (opens in a new tab)HTML
<script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <h3>Oolong</h3>        <p><slot>No tasting notes yet</slot></p>`;    }  }  customElements.define("tea-card", TeaCard);</script> <tea-card></tea-card>
ResultLoading…
Try it yourself
The fallback shows.

Nothing at all sits between the tags. The slot gets nothing, so it shows its own content from line 8: No tasting notes yet.

Lines 1 to 13 are the same every time. Only the card, from line 14, changes: what sits between its tags.
  • Nothing shows the fallback, No tasting notes yet.
  • One space hides it.
  • A line break hides it too.
  • A comment shows the fallback again.

Spaces and line breaks between tags are text. The browser keeps them as text nodes, children of the card like any other. A text child goes to the default slot. So the slot isn't empty, and its fallback hides. The spaces draw nothing, so the card looks blank. MDN notes it too: even a single whitespace text node counts. A comment is a child as well, but comments are never slotted.

It's like slipping a clear sheet of plastic into the photo frame. You see nothing, but the sample photo is gone.

Two halves. Left, <tea-card></tea-card>: a <slot> card, the note nothing assigned, and a No tasting notes yet card, labelled fallback shows. Right, <tea-card> with a line break before </tea-card>: a <slot> card, a line break (text) card under it, and a crossed-out No tasting notes yet card, labelled fallback hidden.<tea-card></tea-card><slot>nothing assignedNo tasting notes yetfallback shows<tea-card>⏎</tea-card><slot>line break (text)No tasting notes yetfallback hidden
On the left, nothing sits between the tags, so the slot shows its fallback. On the right, the line break is a text node. It goes into the slot and hides the fallback, though it draws nothing.

Why it matters: most people write a card over several lines. So put fallback content on named slots. Spaces can't reach them, because a named slot only gets children that ask for it.

Two slots with the same name

Ravi wants the price twice: above the name and below it. So he writes two price slots:

Two slots with the same namePop out in the code editor (opens in a new tab)HTML
<tea-card>  <span slot="price">Price: 4</span></tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <p><slot name="price">Ask at the counter</slot></p>        <h3>Green tea</h3>        <p><slot name="price">Ask at the counter</slot></p>`;    }  }  customElements.define("tea-card", TeaCard);</script>
ResultLoading…
  • Line 2 is the price, and it asks for the price slot.
  • Lines 11 and 13 are two slots, both named price.

The result shows Price: 4, then Green tea, then Ask at the counter. The browser looks for the first slot in the shadow tree with the child's name, and only that one. The second slot gets nothing, so it shows its fallback. A node can show in one place only.

The same goes for two default slots: the first one gets everything. Keep each slot name unique in a shadow root, as MDN advises.

What can be slotted?

Not everything inside a card can go into a slot. The DOM standard calls the things that can slottables, and there are two kinds:

  • elements, like <span>, <img>, or another custom element;
  • text nodes, like the words and spaces between tags.

They must also be children of the host, one level down. When the browser fills slots, it only looks at the host's own children. Comments are never slotted, and neither is anything deeper down. Here a span sits one level deeper, inside a div:

A span inside a div asks for the price slotPop out in the code editor (opens in a new tab)HTML
<tea-card>  <div>    <span slot="price">Price: 4</span>  </div></tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <p><slot name="price">Ask at the counter</slot></p>        <slot></slot>`;    }  }  customElements.define("tea-card", TeaCard);</script>
ResultLoading…
  • Line 2 is a div, the card's only element child. Line 3, inside it, is a span that asks for the price slot.
  • Line 13 is the price slot, with a fallback. Line 14 is the default slot.

The result shows Ask at the counter, then Price: 4 under it. The span never reached the price slot. Only the div is the card's child. It has no slot attribute, so it went to the default slot, and the span went along inside it. The span's own slot attribute did nothing.

On the left, a tree: <tea-card>, its child <div>, and inside the div <span slot="price">, labelled its slot attribute does nothing. On the right, a window called tea-card holds <slot name="price"> with Ask at the counter under it, and a <slot> below. An arrow goes from the div to the <slot>.<tea-card><div><span slot="price">its slot attribute does nothingtea-card<slot name="price">Ask at the counter<slot>
Only the <div> is the card's child, so only the div is slotted, into the default slot. The span goes along inside it. The price slot gets nothing and shows Ask at the counter.
Real-life analogyOnly the parcels at the counter get sorted

At the post office, the clerk sorts the parcels you hand over the counter. She reads the label on each one and drops it in the right bag. A letter packed inside a parcel travels inside it. Its own address doesn't matter here, because nobody opens the parcel to read it.

In real life: The parcels you hand over
In JavaScript: The host's own children
In real life: The label on a parcel
In JavaScript: The slot attribute on a child
In real life: The mail bags
In JavaScript: The slots
In real life: A letter packed inside a parcel
In JavaScript: An element inside a child, which goes wherever its parent goes

Two things follow from the rule:

  • To put text in a named slot, wrap it in an element, like <span slot="name">. Bare text has no attributes, so it can only go to the default slot.
  • To fix a nested element, move the slot attribute up to the host's child: the div, here.

Any element can be slotted: an image, a button, a list, or another component. Even a <slot> can be slotted into another component's slot. Lesson 8.3, The slot API, covers that.

The browser's own elements are built the same way. Lesson 1.1 opened the hidden shadow root of <details>. It has two slots: one shows your <summary>, and the other shows the rest of what you wrote inside.

Read each row across: what the page puts inside a card with a price slot and a default slot, and where the card shows it.

Where each child of the card goes
What the page puts inside the cardWhere does it show?
Text, like Floral and sweetIn the default slot. Text can't have a slot attribute, so it can't pick a named slot.
An element with no slot attributeIn the default slot.
An element with slot="price"In the first <slot name="price">.
An element with slot=""In the default slot. An empty name is the same as no name.
An element with slot="photo", when no slot has that nameNowhere. It stays in the page, but it isn't drawn.
Spaces or a line break between two tagsIn the default slot, as a text node. You can't see it, but it hides that slot's fallback.
A commentNowhere. Comments are never slotted, so they don't hide a fallback.
An element inside another childInside its parent, wherever the parent goes. Its own slot attribute does nothing.
Which slot does it land in?
  • The words Floral and sweet, written straight inside the card
  • <em>Best hot</em>, with no slot attribute
  • The line break between two tags
  • <span slot="price">Price: 4</span>
  • A second <span slot="price">
  • <span slot="Price">, with a capital P
  • <img slot="photo">, and the card has no photo slot
  • <span slot="price"> inside a <div> child
Try it yourself
0 of 8 correct

The card has a price slot and a default slot. Sort each thing the page writes inside it.

Choose a category for every card. You can change an answer at any time; Reset clears them all.

Adding slots to the tea card

Now Asha's card. The kit's tea-card gets three slots. Its name and price attributes keep working, so a card with only attributes shows what it showed before. Here are the first 12 lines of the new tea-kit/tea-card.js:

The kit card's template, with three slots (tea-kit/tea-card.js, lines 1 to 12)JavaScript
const template = document.createElement("template");
template.innerHTML = `
  <style>
    .card { border: 2px solid seagreen; border-radius: 8px; padding: 8px 12px; }
    h3 { margin: 0; color: seagreen; }
    p { margin: 4px 0 0; }
  </style>
  <article class="card" aria-labelledby="name">
    <h3 id="name"><slot name="name"></slot></h3>
    <p id="price"><slot name="price"></slot></p>
    <slot></slot>
  </article>`;
  • Lines 1 to 8 are lesson 7.1's template, unchanged.
  • Line 9 puts a name slot inside the heading.
  • Line 10 puts a price slot inside the price line.
  • Line 11 adds a default slot under the price, for tasting notes or anything else.

Where do the attributes go now? Into the slots, as their fallback content:

The kit card's callback, which now fills the slots (tea-kit/tea-card.js, lines 33 to 38)JavaScript
  attributeChangedCallback(name, oldValue, newValue) {
    if (oldValue === newValue) return;
    const text = newValue ?? "";
    if (name === "name") this.shadowRoot.querySelector('slot[name="name"]').textContent = text;
    if (name === "price") this.shadowRoot.querySelector('slot[name="price"]').textContent = `Price: ${text}`;
  }
  • Line 2 is lesson 7.3's check: the same value means nothing to do.
  • Line 4 writes the name attribute's text inside the name slot.
  • Line 5 writes Price: and the price attribute inside the price slot.

Text inside a <slot> is its fallback. So a card with only attributes shows them, as before. A child with slot="price" replaces the whole price line with the page's own markup. Here is the menu, with three cards:

Three kit cards, with and without slotted contentPop out in the code editor (opens in a new tab)HTML
<tea-card name="Green tea" price="4"></tea-card><tea-card name="Oolong" price="5">  <span slot="price"><s>Price: 5</s> Price: 4 today</span>  Floral and sweet, with a honey finish.</tea-card><tea-card price="3">  <span slot="name">Black tea <small>(strong)</small></span></tea-card> <script type="module">  import "./tea-kit/index.js";</script>
ResultLoading…
  • Line 1 is a card with attributes only. It shows Green tea and Price: 4, both from the slots' fallback.
  • Line 3 slots a price line with the old price in <s>. The result shows Price: 5 crossed out, then Price: 4 today.
  • Line 4 is plain text, so it goes to the default slot. The tasting note shows under the price.
  • Line 7 slots a name with a small note in it: Black tea (strong). The price still comes from line 6's attribute.

Each card still gets its accessible name from its heading, as in lesson 6.3: Green tea, Oolong, and Black tea (strong). A screen reader hears what the slot shows.

Why does the default slot have no fallback? Because of the line breaks. Most cards are written over several lines, like the one on lines 2 to 5. A No tasting notes yet fallback would hide on every one of them, notes or not. The named slots don't have that problem.

Attribute or slot?

Real component libraries use the same pattern. Web Awesome's <wa-input> has a label attribute and a label slot, and its docs say to use either one. Its source writes the attribute's text inside the slot, as the fallback (line 413 of input.ts). You met Web Awesome in lesson 2.1.

One thing to know when a page uses both. Say the Oolong card from line 2 gets a new price attribute later. The callback writes the new text into the price slot, but only as fallback. The slotted span stays on screen, so the card still shows Price: 4 today. Change the span, not the attribute.

Read each row across: what the card should show, and how each way handles it.

The kit card's name and price, by attribute or by slot
What the card should showWith the name or price attributeWith a slot
Plain text, like Green teaWorks. The card writes it as text.Works. Wrap the text in an element with slot="name".
Markup, like an old price crossed outThe tags show as text, because the card uses textContent.Works. The page's own elements show in the card.
Both at onceThe attribute's text waits as the slot's fallback.The slotted element shows, not the attribute's text.

Common mix-ups

  • "Children with no matching slot are deleted." No. They stay in the page, and isConnected is still true. They just aren't drawn.
  • "Any element inside the host can pick a slot." No. Only the host's own children are slotted. A slot attribute deeper down does nothing.
  • "A slot moves the page's content into the shadow root." No. The content stays where the page wrote it. The slot only shows it.
  • "Two slots with the same name both show the content." No. The first one gets all of it. The second shows its fallback.
  • "An empty-looking card shows its default slot's fallback." No. Spaces and line breaks are text nodes, and they go to the default slot.
  • "Slot names ignore capital letters." No. slot="Price" doesn't match name="price".
  • "You need a template to use slots." No. A <slot> works in any shadow root. Most examples here fill theirs with innerHTML.
  • "Old tutorials' <content select> still works." No. That was shadow DOM v0, which Chrome removed in Chrome 80. Use <slot>.

Practice exercises

Answer first, then use the hints. Each result runs live, so you can check yourself.

Exercise 1 · Warm-upWhich heading shows?

This card has no name child. What does the heading say?

<tea-card>  <span slot="price">Price: 5</span></tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <h3><slot name="name">A new tea</slot></h3>        <p><slot name="price">Ask at the counter</slot></p>`;    }  }  customElements.define("tea-card", TeaCard);</script>
ResultLoading…

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · Warm-upA capital letter

    What does the price line show?

    <tea-card>  <span slot="name">Oolong</span>  <span slot="Price">Price: 5</span></tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <h3><slot name="name">A new tea</slot></h3>        <p><slot name="price">Ask at the counter</slot></p>`;    }  }  customElements.define("tea-card", TeaCard);</script>
    ResultLoading…

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeAdd a price slot

      Line 13 is a comment where the price slot should go. Add the slot. After your change, what shows under the name?

      <tea-card>  <span slot="name">Green tea</span>  <span slot="price">Price: 4</span></tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <h3><slot name="name"></slot></h3>        <!-- Add a slot for the price here. -->`;    }  }  customElements.define("tea-card", TeaCard);</script>
      ResultLoading…

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · PracticeFind the bug in the sale box

        Before you fix it, what does the price line show? Then make the price show, and keep the div.

        <tea-card>  <span slot="name">Oolong</span>  <div class="sale">    <span slot="price">Price: 4</span>  </div></tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <h3><slot name="name"></slot></h3>        <p><slot name="price">Ask at the counter</slot></p>`;    }  }  customElements.define("tea-card", TeaCard);</script>
        ResultLoading…

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · ChallengeMake the notes fallback show

          No tasting notes yet never shows. Change only the shadow tree, not the page, so it does. After your fix, what shows under the name?

          <tea-card>  <span slot="name">Green tea</span></tea-card> <script>  class TeaCard extends HTMLElement {    constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `        <h3><slot name="name"></slot></h3>        <p><slot>No tasting notes yet</slot></p>`;    }  }  customElements.define("tea-card", TeaCard);</script>
          ResultLoading…

          Answer, then press Check. Spacing and letter case don’t matter.

            Exercise 6 · ChallengeAn attribute as the fallback

            Make the label attribute the slot's fallback content, as the kit's card does. After your change, what does the first badge show?

            <tea-badge label="New"></tea-badge><tea-badge label="New"><b slot="label">Back in stock</b></tea-badge> <script>  class TeaBadge extends HTMLElement {    static observedAttributes = ["label"];     constructor() {      super();      this.attachShadow({ mode: "open" });      this.shadowRoot.innerHTML = `<p><slot name="label"></slot></p>`;    }     attributeChangedCallback(name, oldValue, newValue) {      // Make the label attribute the slot's fallback content.    }  }  customElements.define("tea-badge", TeaBadge);</script>
            ResultLoading…

            Answer, then press Check. Spacing and letter case don’t matter.

              Check your understanding

              For each question, ask two things: is it a child of the host, and which slot has its name?

              The slots quiz · 7 questionsScore: first tries count
              1. Question 1 of 7A card has a price slot and a default slot. Where does <em>Best hot</em> go, written inside the card with no slot attribute?

                Choose an answer to see the explanation.

              2. Question 2 of 7This card has two price slots. What does its bottom line show?

                Read the code, then predictPop out in the code editor (opens in a new tab)HTML
                <tea-card>
                  <span slot="price">Price: 4</span>
                </tea-card>
                
                <script>
                  class TeaCard extends HTMLElement {
                    constructor() {
                      super();
                      this.attachShadow({ mode: "open" });
                      this.shadowRoot.innerHTML = `
                        <p><slot name="price">Ask at the counter</slot></p>
                        <h3>Green tea</h3>
                        <p><slot name="price">Ask at the counter</slot></p>`;
                    }
                  }
                  customElements.define("tea-card", TeaCard);
                </script>

                Choose an answer to see the explanation.

              3. Question 3 of 7A child has slot="photo", and the card has no slot with that name. What happens to the child?

                Choose an answer to see the explanation.

              4. Question 4 of 7A card's shadow root has <slot>No tasting notes yet</slot>. The page writes <tea-card>, a line break, and </tea-card>. Why is there no fallback?

                Choose an answer to see the explanation.

              5. Question 5 of 7In this card, where does the span with Price: 4 show?

                Read the code, then predictPop out in the code editor (opens in a new tab)HTML
                <tea-card>
                  <div>
                    <span slot="price">Price: 4</span>
                  </div>
                </tea-card>
                
                <script>
                  class TeaCard extends HTMLElement {
                    constructor() {
                      super();
                      this.attachShadow({ mode: "open" });
                      this.shadowRoot.innerHTML = `
                        <p><slot name="price">Ask at the counter</slot></p>
                        <slot></slot>`;
                    }
                  }
                  customElements.define("tea-card", TeaCard);
                </script>

                Choose an answer to see the explanation.

              6. Question 6 of 7Which of these can be slotted?

                Choose an answer to see the explanation.

              7. Question 7 of 7With the kit's new card, what is the heading of <tea-card name="Oolong"><span slot="name">Oolong <small>(new)</small></span></tea-card>?

                Choose an answer to see the explanation.

              Key takeaways

              • A <slot> in a shadow root shows the host's own children in that spot. The children stay in the page.
              • The default slot, a <slot> with no name, takes every child with no slot attribute, text included, in page order.
              • <slot name="price"> takes the children with slot="price". Names match exactly, and only the first slot with a name gets content.
              • A child whose slot name matches no slot stays in the page, but isn't drawn.
              • Content inside a <slot> is its fallback. It shows only when nothing is assigned. Spaces and line breaks count, so put fallback on named slots.
              • Only the host's element and text children are slotted. Comments and deeper elements are not.

              Remember the one-liner.
              The page brings the content, the slots decide where it shows, and the fallback fills any slot the page leaves empty.

              Sources

              The facts on this page come from these standards, docs, and posts:

              Coming next: Light DOM, shadow DOM, and the flat tree. Slotted content stays in the page but shows in the shadow tree. That lesson shows all three trees.

              CompleteFrontend Clear concepts. Working examples.