completefrontend
Open lab My progress
THE WEB COMPONENTS FIELD GUIDE

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.

By the end, you can
  • 01
    Name the three treesTell the light tree, the shadow tree, and the flat tree apart, and say which one the browser draws.
  • 02
    Find 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.
  • 03
    Predict what the browser drawsSay what a slotted node inherits, where its lang comes 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:

The card's shadow root can't find the slotted pricePop out in the code editor (opens in a new tab)HTML
<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>
ResultLoading…
  • Line 2 is the card's one child element: a span that asks for the price slot. 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 prints null.
  • Line 19 looks for it in the card itself, with card.querySelector(). It prints Price: 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.
Three windows side by side. The light window shows what the page wrote: <tea-card>, a <span> under it, and an <s> under the span. The shadow window shows what the card wrote: #shadow-root, then <h3> and <p>, and a <slot> under the p. The flat window shows what the browser draws. It holds <tea-card>, then <h3> and <p>. Under the p is the <slot>, with the <span> inside it and the <s> inside the span.what the page wrotelight<tea-card><span><s>what the card wroteshadow#shadow-root<h3><p><slot>what the browser drawsflat<tea-card><h3><p><slot><span><s>
The page wrote the <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.
Real-life analogyThe flat tree is a photo of the set tray

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
The main idea

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:

The card's own HTML, and its shadow root's HTMLPop 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"></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>
ResultLoading…
  • 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:

  1. A host, like the card, gets its shadow root's children instead of its own. Its own children only come in through slots.
  2. A slot gets the nodes assigned to it.
  3. A slot with nothing assigned keeps its own children: its fallback.
  4. 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:

Playground: see the three trees
A card with a name slot and a price slotPop 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><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>
ResultLoading…
What the page wrote (the light tree)Text
<tea-card>
  <span slot="name">
    "Oolong"
  <span slot="price">
    "Price: 5"
What the card wrote (the shadow tree)Text
#shadow-root
  <h3>
    <slot name="name">
      "A new tea"
  <p>
    <slot name="price">
      "Ask at the counter"
What the browser draws (the flat tree)Text
<tea-card>
  <h3>
    <slot name="name">
      <span slot="name">
        "Oolong"
  <p>
    <slot name="price">
      <span slot="price">
        "Price: 5"
Try it yourself
Both spans are in the flat tree, each inside its slot.

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.

Lines 1 to 12 are the same every time. Only the card, from line 14, changes. The trees under the result leave out the spaces and line breaks between tags.
  • 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:

The slotted span's parent is the cardPop 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>`;    }  }  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>
ResultLoading…
  • 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 parentNode is the card. It prints true.
  • Line 18 counts the card's element children with children.length. It prints 1.
  • 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.

Real-life analogyA slotted node is a painting on loan

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.children and card.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:

A slot's own children are only its fallbackPop 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>`;    }  }  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>
ResultLoading…
  • 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.

On the left, the light tree: <tea-card>, and under it <span slot="price">. An arrow labelled parentNode goes up from the span to the card. On the right, a window called shadow holds <p>, <slot name="price"> under it, and the text "Ask at the counter" inside the slot, labelled its only child. An arrow labelled shown here goes from the span to the slot.the light tree<tea-card><span slot="price">parentNodeshown hereshadow<p><slot name="price">"Ask at the counter"its only child
The span's parent is the card, in the light tree. The slot's only child is its fallback text. The slot shows the span, but it doesn't hold it.

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:

Move the price to the other cardPop 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><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>
ResultLoading…
  • 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:

A slotted name inherits the heading's colorPop out in the code editor (opens in a new tab)HTML
<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>
ResultLoading…
  • 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 prints rgb(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().

Two halves. Left, the light tree: <tea-card>, labelled crimson, with <span> under it. Right, the flat tree: <tea-card>, labelled crimson, then <h3>, labelled seagreen, then <slot>, then <span>, labelled seagreen.the light tree<tea-card>crimson<span>its parent is the cardthe flat tree<tea-card>crimson<h3>seagreen<slot><span>seagreen
In the light tree, the span's parent is the crimson card. In the flat tree, it sits in the slot inside the seagreen heading. Inherited color follows the flat tree, so Green tea is seagreen.

Now set the color yourself. Put it on the card, on the slot, on both, or nowhere:

Playground: which color does the span take?
A slotted span, with a color on the card and on the slotPop out in the code editor (opens in a new tab)HTML
<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>
ResultLoading…
Try it yourself
Green tea has the page's normal text color.

Nobody sets a color, so the span gets the browser's normal text color. The console prints it as an rgb() value.

Only line 2, the page's rule for the card, and line 15, the card's rule for its slot, change. Line 22 prints the span's color.
  • 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:

A slotted paragraph keeps the page's languagePop out in the code editor (opens in a new tab)HTML
<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>
ResultLoading…
  • 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() answers true or false. It prints true.
  • 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.

Real-life analogyA guest at the window table

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: lang and :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:

A card to inspect, with a name slot and a price slotPop out in the code editor (opens in a new tab)HTML
<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>
ResultLoading…

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:

  1. Right-click the result and choose Inspect.
  2. 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.
  3. Next to <span slot="name">, find the slot badge, and click it. DevTools jumps into the shadow root and selects <slot name="name">.
  4. Under that slot, find the short ↪ <span> line with a reveal badge. Click reveal to jump back to the span.

If a badge is missing, right-click any element in the Elements panel. Open Badge settings, and check slot.

A window called Elements, like Chrome's Elements panel. It lists <tea-card>, #shadow-root (open), <h3>, <slot name="name">, and under the slot an entry ↪ <span> with a reveal badge. Then <p>…</p>. After the shadow root come the card's own children: <span slot="name">Oolong</span> and <span slot="price">Price: 5</span>, each with a slot badge, and </tea-card>. An arrow goes from the name span's slot badge up to <slot name="name">, and a short arrow goes from the reveal badge down to the name span.Elements<tea-card>#shadow-root (open)<h3><slot name="name">↪ <span>reveal<p>…</p><span slot="name">Oolong</span>slot<span slot="price">Price: 5</span>slot</tea-card>
The spans are listed where they live: after the shadow root, as the card's children. Each has a slot badge that jumps to its slot. Under the slot, a reveal badge jumps back.
Real-life analogyThe slot badges are a cloakroom ticket

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 slot badge, next to the span in the light tree
In real life: The half on the hook
In JavaScript: The reveal badge, 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:

Find the cards on sale by reading the light treePop 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></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>
ResultLoading…
  • 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, and Full 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.

Which tree does each one read?
What you useWhich tree does it read?What it gives for Oolong's card
card.innerHTML and card.childrenThe 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.childNodesThe shadow tree.Only the price slot's own fallback text, which isn't drawn.
getComputedStyle() for colorThe 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 panelThe 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:

Is it in the flat tree?
  • <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
Try it yourself
0 of 7 correct

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.

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

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.childNodes holds 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 color follow 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 direction follows 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 slot and reveal badges.

Practice exercises

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

Exercise 1 · Warm-upSearch the shadow root

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>
ResultLoading…

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

    Exercise 2 · Warm-upCount the children

    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>
    ResultLoading…

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

      Exercise 3 · PracticeWhich color are the notes?

      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>
      ResultLoading…

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

        Exercise 4 · PracticeFind the bug in the sale badge

        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>
        ResultLoading…

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

          Exercise 5 · ChallengeIs the paragraph English?

          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>
          ResultLoading…

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

            Exercise 6 · ChallengeFollow the slot badge

            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>
            ResultLoading…

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

              Check your understanding

              For each question, ask which tree it's about: the page's, the component's, or the one the browser draws.

              The flat tree quiz · 7 questionsScore: first tries count
              1. 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.

              2. Question 2 of 7What does line 18 of this page print?

                Read the code, then predictPop out in the code editor (opens in a new tab)HTML
                <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.

              3. Question 3 of 7A slot has the fallback text Ask at the counter, and a span is assigned to it. What does slot.childNodes hold?

                Choose an answer to see the explanation.

              4. Question 4 of 7In this page, what color is Green tea?

                Read the code, then predictPop out in the code editor (opens in a new tab)HTML
                <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.

              5. 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.

              6. Question 6 of 7In Chrome DevTools, what does the slot badge next to a light DOM child do?

                Choose an answer to see the explanation.

              7. 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. lang and :dir() follow the light tree.
              • No API returns the flat tree. DevTools links the trees with slot and reveal badges.

              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:

              Coming next: The slot API. Find out what landed in each slot, and which slot a node landed in.

              CompleteFrontend Clear concepts. Working examples.