liveBook · Manning

Slot element

As explained in Vanilla Web

The HTML <slot> element defines an insertion point for content projected into a web component's shadow tree.

A web component can keep its structure in shadow DOM while letting consumers supply selected content. A <slot> element marks an insertion point in that structure. Children supplied inside the custom element are projected there; the slot itself does not add visible content. section 8.4

Choose the insertion points the component needs:

  • Use <slot></slot> for children without a slot attribute. If there is more than one unnamed slot, only the first receives projected content. section 8.4
  • Use <slot name="title"></slot> for a named insertion point. A consumer supplies it with a child such as <span slot="title">Camera</span>; matching children are projected in their original order. section 8.4.2 section 8.4.4
  • Put fallback markup between a slot’s tags, as in <slot>No message provided.</slot>. It appears only when that slot receives no matching content. section 8.4.1

A product-details contract

A product-details component can place named title and action slots around an unnamed description slot. Its consumer supplies <span slot="title">USB-C Camera</span>, description text, and <button slot="action">Add to Cart</button>. If no action is supplied, a disabled “Out of stock” button in the slot serves as fallback. The consumer’s child order need not match the template’s layout. section 8.4.2

Projection does not move children into the shadow DOM. The browser renders a flattened tree combining shadow structure with assigned light-DOM content, while the DOM trees remain separate. Slotted content is primarily styled by the document that defines it. For predictable styling, style a slot’s container, expose CSS custom properties, or document styling expectations for consumers. section 8.4.3 section 8.5 section 8.5.2

To react to assignments, listen for slotchange on the slot and inspect assignedNodes() or assignedElements(). The event tracks changes to directly assigned nodes, not edits inside them; assignedElements() excludes text and comments. Tests can check assignment through these methods instead of inferring it from outer HTML. section 8.6.1 section 8.6.2 section 9.7.2

Treat slot names as a public API: name them semantically, document expected content, and provide useful fallbacks. Prefer attributes or properties for content required for the component to function; reserve slots for customization. Keep nested slot chains shallow. section 8.8 section 8.7

How Vanilla Web covers it

How deep it goes
Works through default and named assignment, fallback, flattened rendering, styling, and assignment APIs; also identifies limits of slotchange.
Examples
Uses a decorated highlight card and a product-details component with title, description, action, and fallback content.
What it assumes
Assumes familiarity with custom elements and their shadow DOM, while defining slots as placeholders for light-DOM children.
What you can do after
Build it from scratch with named and default slots, fallbacks, assignment inspection, and a documented consumer contract.
How it connects
Connects shadow DOM and light DOM through content projection, with links to templates, CSS custom properties, and component testing.

Also explained in

Compare how 2 books explain slot element →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.