← Back to HTML & CSS, page 6

html-css-0115 · Medium mid

How do named slots and fallback content work in a shadow DOM component?

A slot shows light DOM content inside a shadow tree. The default slot takes children with no slot attribute, named slots take children with a matching slot name, and content inside the slot tag is shown only as fallback. Slotted content stays in the page DOM.

Full answer

A slot is a hole in a component's shadow tree where the page's own content (the light DOM) is shown. The content stays in the page, so the page keeps control of it, search engines and scripts can see it, and page styles still apply to it. The shadow tree only decides where it is drawn.

Rules:

  • An unnamed slot takes all children that have no slot attribute. This is called the default slot.
  • A named slot, <slot name="title">, takes children with slot="title". Several children can fill one slot.
  • Content inside the slot tag is fallback content. It is shown only when nothing is assigned to the slot.
  • Children that point to a slot name that does not exist are not shown.
  • Slotted nodes are not moved in the DOM. slot.assignedNodes() and assignedElements() list them, and the slotchange event fires when the assignment changes.

This is a good way to build components such as cards and dialogs that accept their own content, without strings of HTML in attributes.

Order in the light DOM does not set the visual order. The shadow tree decides it. As with order in Flexbox, this can make the reading order differ from the visual order, so keep the structure simple.

The first box shows "Fees" in the header and the text in the body. The second box has no title, so the header shows the fallback "Untitled".

The check reads the slot assignments, which is how a script can tell what is shown.

html
<info-box id="a"><span slot="title">Fees</span>Pay monthly.</info-box>
<info-box id="b">Only body text</info-box>
<script>
  class InfoBox extends HTMLElement {
    constructor() {
      super();
      this.attachShadow({ mode: "open" }).innerHTML =
        '<header><slot name="title">Untitled</slot></header><section><slot></slot></section>';
    }
  }
  customElements.define("info-box", InfoBox);
</script>

Checked in Chrome 156 only, by reading the slot assignments. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Default slot takes children without a slot attribute.
  • Named slots match the slot attribute.
  • Content inside the slot element is fallback.
  • Slotted nodes stay in the light DOM.
  • slotchange and assignedElements help scripts.

Common mistakes

  • Expecting slotted content to be moved into the shadow tree. It stays in the page.
  • Adding two default slots, where only the first one gets content.
  • Using slots to reorder content in a way that breaks reading order.

Follow-ups

  • How do you detect when slotted content changes?
  • What happens to a child with a slot name that does not exist?

Tips

  • Explain that the content stays in the light DOM, and the slot only places it.
  • Name the fallback content as a feature.