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.
- 01Show the page's content in your componentPut a default
<slot>and named slots in a shadow root, and fill them withslotattributes. - 02Give a slot fallback contentSay when the fallback shows, and why spaces between tags hide it in a default slot.
- 03Know 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:
<tea-card name="Oolong" price="<s>5</s> 4"></tea-card> <script type="module"> import "./tea-kit/index.js";</script>- Line 1 puts
<s>5</s> 4in thepriceattribute.<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:
<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>- 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.
<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:
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
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:
<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>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.
- 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.
<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>- 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 printsFloral.
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 calledprice. - In the page,
slot="price"on a child asks for that slot.
<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>- Line 2 is a span that asks for the
priceslot. Line 3 asks for thenameslot. - Line 12 puts the
nameslot inside the heading. - Line 13 puts the
priceslot 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.
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
slotattribute 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.
<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>The paragraph has no slot attribute, so it goes to the <slot> with no name, on line 15. That's under the price.
- 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.
<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>- Line 3 asks for a
photoslot. Line 11 has only anameslot. - 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:
<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>- 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 slotAsk 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.
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:
<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>Nothing at all sits between the tags. The slot gets nothing, so it shows its own content from line 8: No tasting notes yet.
- 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.
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:
<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>- Line 2 is the price, and it asks for the
priceslot. - 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:
<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>- 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.
<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.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
slotattribute 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
slotattribute 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.
| What the page puts inside the card | Where does it show? |
|---|---|
Text, like Floral and sweet | In the default slot. Text can't have a slot attribute, so it can't pick a named slot. |
An element with no slot attribute | In 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 name | Nowhere. It stays in the page, but it isn't drawn. |
| Spaces or a line break between two tags | In the default slot, as a text node. You can't see it, but it hides that slot's fallback. |
| A comment | Nowhere. Comments are never slotted, so they don't hide a fallback. |
| An element inside another child | Inside its parent, wherever the parent goes. Its own slot attribute does nothing. |
- The words
Floral and sweet, written straight inside the card <em>Best hot</em>, with noslotattribute- 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
The card has a price slot and a default slot. Sort each thing the page writes inside it.
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:
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
nameslot inside the heading. - Line 10 puts a
priceslot 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:
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
nameattribute's text inside the name slot. - Line 5 writes
Price:and thepriceattribute 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:
<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>- 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, thenPrice: 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.
| What the card should show | With the name or price attribute | With a slot |
|---|---|---|
| Plain text, like Green tea | Works. The card writes it as text. | Works. Wrap the text in an element with slot="name". |
| Markup, like an old price crossed out | The tags show as text, because the card uses textContent. | Works. The page's own elements show in the card. |
| Both at once | The 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
isConnectedis stilltrue. They just aren't drawn. - "Any element inside the host can pick a slot." No. Only the host's own children are slotted. A
slotattribute 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 matchname="price". - "You need a template to use slots." No. A
<slot>works in any shadow root. Most examples here fill theirs withinnerHTML. - "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.
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>A new tea. Only the price span went into a slot. The name slot got nothing, so it shows its fallback from line 11.
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>Ask at the counter. Names must match exactly. No slot is called Price, so the price slot gets nothing and shows its fallback. Change line 3 to slot="price" to fix it.
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><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> <p><slot name="price"></slot></p>`; } } customElements.define("tea-card", TeaCard);</script>Price: 4. The new slot's name matches the span's slot attribute, so the span shows there, under the heading.
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><tea-card> <span slot="name">Oolong</span> <div class="sale" slot="price"> <span>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>Ask at the counter. The span is inside the div, so its slot attribute does nothing. This card has no default slot, so the div isn't drawn at all. With slot="price" on the div, the div and its span show in the price line.
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><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 name="notes">No tasting notes yet</slot></p>`; } } customElements.define("tea-card", TeaCard);</script>No tasting notes yet. The line breaks have no default slot to go to now. Nothing asks for the notes slot, so its fallback shows. A page adds notes with slot="notes".
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><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) { this.shadowRoot.querySelector("slot").textContent = newValue ?? ""; } } customElements.define("tea-badge", TeaBadge);</script>New. The first badge has no label child, so its slot shows the fallback: the attribute's text. The second badge has a <b slot="label">, so it shows Back in stock instead.
Check your understanding
For each question, ask two things: is it a child of the host, and which slot has its name?
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 noslotattribute?Choose an answer to see the explanation.
Question 2 of 7This card has two price slots. What does its bottom line show?
Read the code, then predict<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.
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.
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.
Question 5 of 7In this card, where does the span with Price: 4 show?
Read the code, then predict<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.
Question 6 of 7Which of these can be slotted?
Choose an answer to see the explanation.
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 noslotattribute, text included, in page order. <slot name="price">takes the children withslot="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:
- Using templates and slots: Adding flexibility with slots, MDN.
- <slot>: The Web Component Slot element, MDN.
- slot (HTML global attribute), MDN.
- HTML Standard: The slot element, WHATWG.
- DOM Standard: Slottables, WHATWG.
- DOM Standard: Find a slot, WHATWG.
- DOM Standard: Find slottables, WHATWG.
- Shadow DOM v1: Composition and slots, Eric Bidelman, web.dev.
- :has-slotted, MDN.
- Input: Slots, Web Awesome.
- input.ts, at Web Awesome 3.15.0, Web Awesome.
- A history of the HTML slot element, Jan Miksovsky, Component Kitchen.
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.