Light DOM, shadow DOM, and the flat tree
Learn the shadow DOM flat tree: where slotted content really lives, what the browser draws and inherits, and how Chrome DevTools links the trees.
- 01Name the three treesTell the light tree, the shadow tree, and the flat tree apart, and say which one the browser draws.
- 02Find where slotted content livesRead slotted nodes from the host, not the shadow root, and say why a slot's own children are only its fallback.
- 03Predict what the browser drawsSay what a slotted node inherits, where its
langcomes from, and follow slots with DevTools' badges.
What is the flat tree in shadow DOM?
Asha's Oolong card is on Leaf and Kettle's menu. Since lesson 8.1, it shows the old price crossed out, then the new one. Ravi wants the card to notice a sale by itself. So in the card's code, he looks for the crossed-out price inside the card's shadow root.
He gets null. Nothing. Yet the crossed-out price is right there on the screen, inside the card's border. Asha opens DevTools. The price span sits outside the card's #shadow-root, with the card's other children. So where does the span live: in the page, or in the card?
Slotted content lives in the light DOM. It stays a child of the host, where the page wrote it. The browser draws it inside the shadow tree, at its slot. The tree the browser draws is the flat tree: the shadow tree, with each slot filled by the nodes assigned to it.
Here is Ravi's problem, on a page of its own:
<tea-card> <span slot="price"><s>Price: 5</s> Price: 4</span></tea-card> <script> class TeaCard extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); this.shadowRoot.innerHTML = ` <h3>Oolong</h3> <p><slot name="price"></slot></p>`; } } customElements.define("tea-card", TeaCard); const card = document.querySelector("tea-card"); console.log(card.shadowRoot.querySelector("s")); console.log(card.querySelector("s").textContent);</script>- Line 2 is the card's one child element: a span that asks for the
priceslot. Inside it,<s>crosses out the old price. - Lines 10 to 12 fill the shadow root: a heading, and a paragraph with the price slot.
- Line 18 looks for the
<s>in the shadow root. It printsnull. - Line 19 looks for it in the card itself, with
card.querySelector(). It printsPrice: 5.
The result shows Oolong, then Price: 5 crossed out and Price: 4. So the <s> is drawn inside the card. The shadow root can't find it, but the card can. Both answers are right. They look in different trees.
Every card with slots has three trees:
- The light tree is the card and the children the page wrote between its tags. Here, that's the span and its
<s>. - The shadow tree is the shadow root and what the card's code put in it. Here, that's the heading, the paragraph, and the slot.
- The flat tree is what the browser builds from the other two, and draws. Here, it's the shadow tree, with the span inside the slot.
<span slot="price"> and its <s>. The card wrote the heading and the slot. The flat tree puts the page's span inside the slot, and that's what you see.At a tea tasting, Leaf and Kettle gives each guest a tray. The shop printed labels on it: one spot for the pot, one for the cup. Asha brought her own cup from home, and she sets it on the cup spot. It's still her cup. Then Ravi takes a photo of the set tray. The photo shows her cup on the shop's tray, as one picture.
- In real life: The tray, with a labelled spot for each thing
- In JavaScript: The card's shadow tree, with its slots
- In real life: The cup Asha brought from home
- In JavaScript: A child the page wrote: the light tree
- In real life: Setting the cup on the spot marked cup
- In JavaScript: A child with
slot="price"showing in<slot name="price"> - In real life: The photo of the set tray
- In JavaScript: The flat tree, which the browser draws
A slotted node lives in the light tree, as a child of its host. The browser draws the flat tree, where that node sits inside its slot. parentNode and the page's searches follow the light tree. Inherited styles and what you see follow the flat tree.
A few words on the names. The DOM standard calls the host's own tree its light tree, and lesson 6.1 called the host's children its light DOM. Both mean the same thing here. The CSS Shadow spec names the flat tree. It also calls it the flattened element tree, so many articles say flattened tree. You met it once already, in lesson 6.3, when a color crossed into a shadow root.
What are the light tree, the shadow tree, and the flat tree?
Each tree answers a different question. The light tree says what the page wrote. The shadow tree says what the component wrote. The flat tree says what you see. Two of them you can print:
<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"></slot></p>`; } } customElements.define("tea-card", TeaCard); const card = document.querySelector("tea-card"); console.log(card.innerHTML.trim()); console.log(card.textContent.trim()); console.log(card.shadowRoot.innerHTML);</script>- Line 2 is the card's light DOM: one span.
- Line 10 fills the shadow root with a paragraph and a price slot.
- Line 16 prints the card's
innerHTML:<span slot="price">Price: 4</span>.trim()cuts off the spaces and line breaks around it. - Line 17 prints the card's
textContent:Price: 4. - Line 18 prints the shadow root's
innerHTML:<p><slot name="price"></slot></p>.
The result shows Price: 4. But no line prints the span inside the slot, the way the result draws it. On the card, innerHTML and textContent read only the light tree, as Node properties and contents describes them. The shadow root's innerHTML reads only the shadow tree. The flat tree is a third thing, and the browser builds it.
How? The CSS Shadow spec gives the steps. The browser starts at the top of the page and walks down. For each node, it picks the children like this:
- A host, like the card, gets its shadow root's children instead of its own. Its own children only come in through slots.
- A slot gets the nodes assigned to it.
- A slot with nothing assigned keeps its own children: its fallback.
- Any other element keeps its own children.
So a slotted span shows up in the flat tree once, inside its slot. A child that no slot takes isn't in the flat tree at all. And a slot's fallback drops out as soon as something is assigned to it.
Try it. Pick what the page puts inside the card, and watch all three trees:
<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> <tea-card> <span slot="name">Oolong</span> <span slot="price">Price: 5</span></tea-card><tea-card>
<span slot="name">
"Oolong"
<span slot="price">
"Price: 5"#shadow-root
<h3>
<slot name="name">
"A new tea"
<p>
<slot name="price">
"Ask at the counter"<tea-card>
<h3>
<slot name="name">
<span slot="name">
"Oolong"
<p>
<slot name="price">
<span slot="price">
"Price: 5"The light tree has the two spans the page wrote. The flat tree is the shadow tree, with each span inside the slot that has its name. The slots' own text is left out.
- Name and price puts each span inside its slot in the flat tree.
- Name only leaves the price slot with nothing assigned. So the flat tree keeps its fallback, Ask at the counter.
- A photo slot puts the photo span in the light tree, but not in the flat tree.
- Nothing inside makes the flat tree the shadow tree, with both fallbacks.
The shadow tree never changes. The light tree changes, and the flat tree follows it. Why it matters: when something doesn't show, ask which tree it's missing from. If it's missing from the light tree, the page never wrote it. If it's only missing from the flat tree, no slot took it.
Is slotted content in the light DOM or the shadow DOM?
Back to Ravi's null. Where does a slotted node live? Ask the node itself:
<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>`; } } customElements.define("tea-card", TeaCard); const card = document.querySelector("tea-card"); const price = document.querySelector("span"); console.log(price.parentNode === card); console.log(card.children.length); console.log(card.shadowRoot.querySelector("span"));</script>- Line 15 finds the card. Line 16 finds the span with
document.querySelector(), the page's own search. - Line 17 asks whether the span's
parentNodeis the card. It printstrue. - Line 18 counts the card's element children with
children.length. It prints1. - Line 19 looks for a span in the shadow root. It prints
null.
The span's parent is the card, just as line 2 wrote it. The page's search finds it, and the shadow root's search doesn't. Slotting changed what you see. It didn't change where the span lives. MDN puts it in one line: assigning an element to a slot doesn't change its parent node. Walking and searching the DOM covers parentNode and children.
Asha's grandmother lends a painting to the town museum. The museum hangs it on a wall, and visitors see it there. But the painting is still on the family's list. It isn't in the museum's list of its own art. Ask the museum what it owns, and the painting isn't on the answer.
- In real life: The painting
- In JavaScript: The slotted span
- In real life: The family's list of its paintings
- In JavaScript: The card's children:
card.childrenandcard.querySelector() - In real life: The museum's list of its own art
- In JavaScript: The shadow tree:
card.shadowRoot.querySelector() - In real life: The spot on the museum's wall
- In JavaScript: The
<slot>, which shows the span but doesn't own it
Why are a slot's children empty?
Now the other side. A slot shows the span. So is the span the slot's child? Ask the slot:
<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>`; } } customElements.define("tea-card", TeaCard); const card = document.querySelector("tea-card"); const slot = card.shadowRoot.querySelector("slot"); console.log(slot.children.length); console.log(slot.childNodes.length); console.log(slot.textContent);</script>- Line 16 finds the slot in the shadow root.
- Line 17 prints
slot.children.length:0. The slot has no element children. - Line 18 prints
slot.childNodes.length:1. That one node is its fallback text. - Line 19 prints
slot.textContent:Ask at the counter. It's the fallback, even though the result doesn't show it.
A slot's children are only what the component wrote inside it. The nodes it shows aren't among them. This surprises many people. Code that reads slot.children to find slotted content finds nothing. The slot does know what is assigned to it. You ask it with the slot API, the topic of the next lesson.
Why it matters: read slotted content from the host. Inside a component, the host is this. So that's this.querySelector() or this.children, as in lesson 5.5. It isn't this.shadowRoot.querySelector().
A slot shows its own host's children
The span belongs to the light tree. So if you move it in the light tree, it moves on the screen. Click the button in the result:
<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><button>Move the price to Chai</button> <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); const [green, chai] = document.querySelectorAll("tea-card"); const price = green.querySelector('[slot="price"]'); document.querySelector("button").addEventListener("click", () => { chai.append(price); });</script>- Lines 1 to 7 are two cards. Only Green tea's card has a price.
- Line 22 gives the two cards names. Line 23 finds the price span inside Green tea's card.
- Line 25 runs when you click.
append()moves the span to the end of Chai's card.
Before the click, the result shows Green tea with Price: 4, and Chai with Ask at the counter. After it, they swap. Green tea shows Ask at the counter, and Chai shows Price: 4. No code touched a slot. The span got a new parent, and Chai's price slot took it. A slot shows whatever its own host's children are.
What does the browser render?
The browser draws from the flat tree. The CSS Shadow spec explains why. Selectors match the DOM trees, each one on its own. That's why the page's rules stop at a shadow root, as lesson 6.3 showed. But the rest of CSS needs one tree for the whole page. The flat tree is that tree. Boxes, layout, and inherited styles all come from it.
Slots draw no box of their own
The slot is in the flat tree too. But the spec says a slot acts as if it had display: contents. That means it makes no box of its own. The nodes inside it are laid out as if they sat straight in the slot's parent. In the overview's card, the crossed-out price is drawn inside the paragraph's box. A slot is like a chalk mark on the floor where a table will go: it marks the place, but takes up no room itself.
Do slotted elements inherit styles from the slot?
Yes. In the flat tree, a slotted node sits inside its slot. So it inherits from the slot, and the slot inherits from its own parent in the shadow tree. Here the page and the card both set a color:
<style> tea-card { color: crimson; }</style> <tea-card> <span slot="name">Green tea</span></tea-card> <script> class TeaCard extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); this.shadowRoot.innerHTML = ` <style>h3 { color: seagreen; }</style> <h3><slot name="name"></slot></h3>`; } } customElements.define("tea-card", TeaCard); const tea = document.querySelector("span"); console.log(getComputedStyle(tea).color);</script>- Line 2 makes the card crimson. That rule is in the page.
- Line 15 makes the card's heading seagreen. That rule is in the shadow root.
- Line 16 puts the name slot inside the heading.
- Line 22 prints the span's color with
getComputedStyle(), which gives a style's final value. It printsrgb(46, 139, 87): seagreen.
In the light tree, the span's parent is the crimson card. Yet Green tea is seagreen. Inheritance followed the flat tree: from the heading, through the slot, to the span. The spec says it directly: slotted elements inherit from their slot, not from their light tree parent. Styles and classes covers getComputedStyle().
Now set the color yourself. Put it on the card, on the slot, on both, or nowhere:
<style> /* no color on the card */</style> <tea-card> <span slot="name">Green tea</span></tea-card> <script> class TeaCard extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); this.shadowRoot.innerHTML = ` <style>/* no color on the slot */</style> <h3><slot name="name"></slot></h3>`; } } customElements.define("tea-card", TeaCard); const tea = document.querySelector("span"); console.log(getComputedStyle(tea).color);</script>Nobody sets a color, so the span gets the browser's normal text color. The console prints it as an rgb() value.
- Nowhere leaves the browser's normal text color.
- On the card makes Green tea crimson. The card's color flows down the flat tree, through the slot.
- On the slot makes it seagreen. A slot draws no box, but it still passes its styles down.
- On both keeps it seagreen. The slot is closer to the span in the flat tree.
Why it matters: content the page slots into your heading takes your heading's color and font, with no work from the page. Only inherited properties pass down this way. Styling slotted content on purpose belongs to lesson 11.3.
Where do lang and dir come from?
Language and text direction follow a different rule. The lang attribute says which language text is in. The dir attribute says which way it runs: left to right, or right to left. MDN explains that a slotted element takes both from its parent node. Slotting doesn't change its parent. So they come from the light tree, not from the slot:
<div lang="fr"> <tea-card> <p>Thé vert</p> </tea-card></div> <script> class TeaCard extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); this.shadowRoot.innerHTML = `<div lang="en"><slot></slot></div>`; } } customElements.define("tea-card", TeaCard); const tea = document.querySelector("p"); console.log(tea.matches(":lang(fr)")); console.log(tea.matches(":lang(en)"));</script>- Line 1 wraps the card in a French
<div lang="fr">. - Line 3 is the card's child: a paragraph, Thé vert, which is French for green tea.
- Line 12 puts the slot inside an English
<div lang="en">in the shadow tree. - Line 18 asks whether the paragraph matches
:lang(fr), a selector for French content.matches()answerstrueorfalse. It printstrue. - Line 19 asks about English. It prints
false.
The paragraph is drawn inside the English div, but it still counts as French. It took its language from its parent in the light tree, the card, and the card took it from the French div. The dir attribute and the :dir() selector work the same way. One detail, which MDN notes too: the direction the text is drawn in is the CSS direction property. That one is inherited, so it follows the flat tree, like color.
Ravi's cousin visits from Paris and sits at Leaf and Kettle's window table. The afternoon sun falls on that table, so it falls on him too. But he still speaks French, the language of his family. Where he sits decides his light. His family decides his language.
- In real life: The window table he sits at
- In JavaScript: The slot's place in the shadow tree
- In real life: The sunlight on that table
- In JavaScript: Inherited CSS, like
color, which follows the flat tree - In real life: His family, back home
- In JavaScript: The node's parent in the light tree
- In real life: The language he speaks
- In JavaScript:
langand:dir(), which follow the light tree
Why it matters: the page keeps control of its content's language. To mark slotted content as French, set lang in the page, around the content. A lang inside the component won't reach it.
Is there an API that returns the flat tree?
No. No single call gives it to you, and Chrome doesn't keep one either. Blink's own notes say it never stores a flat tree. Blink is Chrome's rendering engine. It walks the flat tree each time it needs it, from the light tree, the shadow trees, and the slots. That's like a map app that works out your route each time you ask, instead of a printed map. Blink's TraverseChild() follows the same steps as the list above.
You can walk it the same way. Use shadowRoot for a host and childNodes for other elements. For a slot, use the slot API, which the next lesson covers. That lesson also shows slots passed on into a deeper component.
How do you see slots in Chrome DevTools?
DevTools doesn't show the flat tree either. Its Elements panel shows the light tree and the shadow tree, each in its own place. Badges link them. A badge is a small label after an element in the Elements panel, and two badges are for slots. Open this card in DevTools and follow them:
<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"></slot></h3> <p><slot name="price">Ask at the counter</slot></p>`; } } customElements.define("tea-card", TeaCard);</script>Lines 2 and 3 are the card's light DOM: a name and a price. Lines 12 and 13 put a name slot and a price slot in the shadow root. Now open DevTools in Chrome:
- Right-click the result and choose Inspect.
- In the Elements panel, find
<tea-card>and open it with its small arrow. The first line under it is#shadow-root (open). After the shadow root come the two spans: the card's own children. - Next to
<span slot="name">, find theslotbadge, and click it. DevTools jumps into the shadow root and selects<slot name="name">. - Under that slot, find the short
↪ <span>line with arevealbadge. Clickrevealto jump back to the span.
If a badge is missing, right-click any element in the Elements panel. Open Badge settings, and check slot.
slot badge that jumps to its slot. Under the slot, a reveal badge jumps back.At a concert, you leave your coat at the cloakroom. The attendant tears a ticket in two. One half hangs on the hook with your coat, and you keep the other. Both halves have the same number, so either one leads to the other.
- In real life: Your coat, on its hook
- In JavaScript: The slotted span, shown in its slot
- In real life: The half of the ticket you keep
- In JavaScript: The
slotbadge, next to the span in the light tree - In real life: The half on the hook
- In JavaScript: The
revealbadge, under the<slot>in the shadow tree - In real life: The same number on both halves
- In JavaScript: Each badge takes you to the other
Notice where each span is listed: under the card, after the shadow root, because that's where it lives. The ↪ <span> line under the slot is only a link back to it. Why it matters: when content is missing from a card, the slot badge tells you at once. A span with no badge went to no slot. Lesson 10.3, Debugging web components, covers the rest of the Elements panel for components.
Reading the kit card's three trees
Now Ravi's sale check, on the real tea shop kit. The kit's card from lesson 8.1 has three slots: name, price, and a default slot. This lesson doesn't change the kit. Ravi's script runs on the menu page, and reads each card from the right tree:
<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></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"; for (const card of document.querySelectorAll("tea-card")) { const old = card.querySelector('[slot="price"] s'); console.log(old ? `On sale, was ${old.textContent}` : "Full price"); } const strong = document.querySelector("small"); console.log(getComputedStyle(strong).color);</script>- Lines 1 to 7 are three kit cards. Oolong's card slots a price line with the old price in
<s>. Black tea's card slots a name with a<small>note. - Line 10 loads the kit.
- Lines 12 to 15 go through the cards. Line 13 looks in each card's light tree for an
<s>inside the price span. - Line 14 prints one line per card:
Full price,On sale, was Price: 5, andFull price. - Lines 17 and 18 print the color of the
<small>note:rgb(46, 139, 87), seagreen.
The search on line 13 works because the price span lives in the light tree, as the card's child. The same search in card.shadowRoot would find nothing, for every card. And the note is seagreen for a flat tree reason. The kit's card makes its heading seagreen, and its name slot sits in that heading. The page never colored the note.
Read each row across: what you use, which tree it reads, and what it gives for Oolong's card.
| What you use | Which tree does it read? | What it gives for Oolong's card |
|---|---|---|
card.innerHTML and card.children | The light tree. | The price span the page wrote, and nothing from the shadow root. |
card.querySelector() | The light tree. | It finds the <s> inside the price span. |
card.shadowRoot.querySelector() | The shadow tree. | It finds the heading and the slots, but never the <s>. |
slot.childNodes | The shadow tree. | Only the price slot's own fallback text, which isn't drawn. |
getComputedStyle() for color | The flat tree. | A slotted name takes the heading's seagreen. |
:lang() and :dir() | The light tree. | The language and direction of the page around the card. |
| DevTools' Elements panel | The light tree and the shadow tree. | Both trees, linked by slot and reveal badges. |
Now sort what's drawn from what isn't, for a card with a name slot and a price slot:
<span slot="name">, a child of the card<span slot="photo">, a child of the card- The
<h3>in the card's shadow root - The
<slot name="name">element itself - The price slot's fallback, when nothing asks for the price slot
- The name slot's fallback, when a name span is slotted
- A comment written between the card's tags
The card's shadow tree has a heading with a name slot and a paragraph with a price slot. Each slot has fallback text. Sort each node.
Common mix-ups
- "Slotting moves the element into the shadow root." No. It stays a child of the host, where the page wrote it. The slot only shows it.
- "shadowRoot.querySelector() can find slotted content." No. Slotted nodes aren't in the shadow tree. Search the host instead.
- "A slot's children are the nodes it shows." No.
slot.childNodesholds only its fallback. The slot API, in the next lesson, lists what's assigned. - "Slotted content inherits styles from its light DOM parent." No. Inherited properties like
colorfollow the flat tree, through the slot. - "Slotted nodes take lang and dir from the slot." No. They take them from their parent in the light tree. Only the drawn
directionfollows the flat tree. - "There is an API that returns the flat tree." No. You build it from
shadowRoot,childNodes, and the slot API. - "DevTools shows the flat tree." No. It shows the light tree and the shadow tree, and links them with
slotandrevealbadges.
Practice exercises
Answer first, then use the hints. Each result runs live, so you can check yourself.
What does line 16 print?
<tea-card> <b slot="name">Mint tea</b></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 card = document.querySelector("tea-card"); console.log(card.shadowRoot.querySelector("b"));</script>null. The <b> is slotted, but it stays the card's child in the light tree. The shadow root's search can't find it.
The result shows only two lines. What does line 19 print?
<tea-card> <span slot="name">Mint tea</span> <span slot="price">Price: 3</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> <p><slot name="price"></slot></p>`; } } customElements.define("tea-card", TeaCard); console.log(document.querySelector("tea-card").children.length);</script>3. All three spans are the card's children. The photo span isn't drawn, because no slot takes it, but it's still in the light tree.
Predict first: what color is Fresh and grassy? Then check the result.
<style> tea-card { color: crimson; }</style> <tea-card> <em>Fresh and grassy</em></tea-card> <script> class TeaCard extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); this.shadowRoot.innerHTML = ` <style>.notes { color: seagreen; }</style> <p>Mint tea</p> <div class="notes"><slot></slot></div>`; } } customElements.define("tea-card", TeaCard);</script>Seagreen. In the flat tree, the <em> sits in the slot inside the notes div, and inherits that div's seagreen. Mint tea is crimson: its paragraph has no color, so it inherits the card's.
The card should say On sale under the price, but the line stays empty. Fix line 16. What does the card show under the price now?
<tea-card> <span slot="price"><s>Price: 5</s> Price: 4</span></tea-card> <script> class TeaCard extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); this.shadowRoot.innerHTML = ` <p><slot name="price"></slot></p> <p id="badge"></p>`; } connectedCallback() { const old = this.shadowRoot.querySelector("s"); this.shadowRoot.getElementById("badge").textContent = old ? "On sale" : ""; } } customElements.define("tea-card", TeaCard);</script><tea-card> <span slot="price"><s>Price: 5</s> Price: 4</span></tea-card> <script> class TeaCard extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); this.shadowRoot.innerHTML = ` <p><slot name="price"></slot></p> <p id="badge"></p>`; } connectedCallback() { const old = this.querySelector("s"); this.shadowRoot.getElementById("badge").textContent = old ? "On sale" : ""; } } customElements.define("tea-card", TeaCard);</script>On sale. The <s> lives in the card's light tree, so this.shadowRoot.querySelector() never finds it. this.querySelector() searches the card's own children and finds it.
Predict first: what does line 16 print? Then check the console.
<tea-card lang="fr"> <p>Menthe</p></tea-card> <script> class TeaCard extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); this.shadowRoot.innerHTML = `<div lang="en"><slot></slot></div>`; } } customElements.define("tea-card", TeaCard); const tea = document.querySelector("p"); console.log(tea.matches(":lang(en)"));</script>false. The paragraph is drawn inside the English div, but its language comes from its light tree parent, the French card. So it matches :lang(fr), not :lang(en).
Using only DevTools' slot badge, find the slot the price landed in. Which element holds that slot?
<tea-card> <span slot="price">Price: 4</span></tea-card> <script> class TeaCard extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); this.shadowRoot.innerHTML = ` <header><slot name="price">Ask at the counter</slot></header> <h3>Green tea</h3> <footer><slot name="price">Ask at the counter</slot></footer>`; } } customElements.define("tea-card", TeaCard);</script>header. The slot badge jumps to the first <slot name="price">, inside <header>. The second slot, in the footer, gets nothing and shows its fallback.
Check your understanding
For each question, ask which tree it's about: the page's, the component's, or the one the browser draws.
Question 1 of 7A page writes
<span slot="price">inside a card, and the card's price slot shows it. Where does the span live?Choose an answer to see the explanation.
Question 2 of 7What does line 18 of this page print?
Read the code, then predict<tea-card> <span slot="price"><s>Price: 5</s> Price: 4</span> </tea-card> <script> class TeaCard extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); this.shadowRoot.innerHTML = ` <h3>Oolong</h3> <p><slot name="price"></slot></p>`; } } customElements.define("tea-card", TeaCard); const card = document.querySelector("tea-card"); console.log(card.shadowRoot.querySelector("s")); console.log(card.querySelector("s").textContent); </script>Choose an answer to see the explanation.
Question 3 of 7A slot has the fallback text Ask at the counter, and a span is assigned to it. What does
slot.childNodeshold?Choose an answer to see the explanation.
Question 4 of 7In this page, what color is Green tea?
Read the code, then predict<style> tea-card { color: crimson; } </style> <tea-card> <span slot="name">Green tea</span> </tea-card> <script> class TeaCard extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); this.shadowRoot.innerHTML = ` <style>h3 { color: seagreen; }</style> <h3><slot name="name"></slot></h3>`; } } customElements.define("tea-card", TeaCard); const tea = document.querySelector("span"); console.log(getComputedStyle(tea).color); </script>Choose an answer to see the explanation.
Question 5 of 7A
<p>is slotted into a card inside<div lang="fr">. Its slot sits inside<div lang="en">in the shadow tree. Which language does:lang()say it's in?Choose an answer to see the explanation.
Question 6 of 7In Chrome DevTools, what does the
slotbadge next to a light DOM child do?Choose an answer to see the explanation.
Question 7 of 7Which one call returns a card's whole flat tree?
Choose an answer to see the explanation.
Key takeaways
- A host with slots has three trees: the light tree the page wrote, the shadow tree the component wrote, and the flat tree the browser draws.
- In the flat tree, a host's children are its shadow root's children. Each slot holds its assigned nodes, or its fallback when nothing is assigned.
- A slotted node stays the host's child. Find it from the host, not from the shadow root.
- A slot's own children are only its fallback content.
- Inherited CSS, like
color, follows the flat tree.langand:dir()follow the light tree. - No API returns the flat tree. DevTools links the trees with
slotandrevealbadges.
Remember the one-liner.
The page owns the slotted nodes, the component owns the slots, and the browser draws the flat tree that joins them.
Sources
The facts on this page come from these standards, docs, and source files:
- CSS Shadow Module Level 1: Flattening the DOM into an element tree, CSS Working Group.
- CSS Shadow Module Level 1: Slots and slotted elements in a shadow tree, CSS Working Group.
- DOM Standard: Shadow tree and the light tree, WHATWG.
- DOM Standard: Find flattened slottables, WHATWG.
- Using templates and slots: Adding flexibility with slots, MDN.
- Using shadow DOM: Attribute inheritance, MDN.
- Shadow DOM v1: Terminology, light DOM vs. shadow DOM, Eric Bidelman, web.dev.
- Badges reference: Slot, Chrome for Developers.
- Blink DOM README: FlatTreeTraversal, at Chromium 158.0.8127.18, Chromium.
- flat_tree_traversal.cc, at Chromium 158.0.8127.18, Chromium.
Coming next: The slot API. Find out what landed in each slot, and which slot a node landed in.