HTML Reference ยท Web Components
The <slot> element
Reference for the HTML <slot> element: named and default slots in web components, fallback content, slotchange, ::slotted() styling and technical details.
The <slot> element is a placeholder inside a shadow tree. When a web component has a shadow root, the element's ordinary children (its light DOM) are not rendered directly. Instead, each child is shown at the position of a matching <slot>: a child with slot="title" goes into <slot name="title">, and everything without a slot attribute goes into the unnamed default slot. Whatever you write between <slot> and </slot> is fallback content, shown only when nothing is assigned.
<!-- inside the component's shadow root --> <h2><slot name="title">Untitled</slot></h2> <slot></slot>
Try it
The second <info-note> has no children, so both of its slots show their fallback content. The <em> is a direct child of the host, so the component's ::slotted(em) rule colors it.
Attributes
| Attribute | Values | Description |
|---|---|---|
name |
Any string | The slot's name. Light DOM children whose slot attribute has the same value are shown here. A slot without name (or with an empty one) is the default slot. |
It also accepts the global attributes. Don't confuse the name attribute on <slot> with the global slot attribute, which goes on the children being passed in.
Usage notes
- Slotted nodes stay in the light DOM. They are only displayed in the slot. The page can still query and style them normally, and they keep their place in the page's DOM tree.
- Only direct children are assigned. A
slotattribute on a grandchild of the host does nothing; the element carryingslotmust be a direct child. - One slot per name. If two slots share a name, the first one in tree order receives the content. Text nodes and elements without a
slotattribute all go to the first default slot. - Styling. Inside the shadow tree,
::slotted(selector)styles assigned elements, but only those top-level elements, not their descendants. Page styles also apply to slotted content and normally win over the component's::slotted()rules. - Reacting to changes. The
slotchangeevent fires on a slot when its assigned nodes change.slot.assignedNodes()andslot.assignedElements()return what is currently assigned; pass{ flatten: true }to resolve nested slots and include fallback content. - Layout. A slot has
display: contents, so it generates no box of its own. Style a wrapper element if you need one. - Manual assignment. A shadow root created with
slotAssignment: "manual"ignoresslotattributes; your code assigns nodes withslot.assign(...).
Technical summary
| Content categories | Flow content, phrasing content |
| Permitted content | Transparent |
| Tag omission | None; both the start and end tags are required |
| Permitted parents | Any element that accepts phrasing content |
| Implicit ARIA role | No corresponding role |
| DOM interface | HTMLSlotElement |
| Default display | contents |
Browser support
Supported in every current browser, along with ::slotted() and the slotchange event. Manual slot assignment arrived later but is now available in all major browsers.
Related
<template>โ inert markup, often used to build a shadow tree- Global attributes โ including the
slotattribute - Lesson: Templates and Web Components