<slot>

A placeholder in a web component where the content written between its tags ends up.

Display
contents
Closing tag
Required
Permitted content
Transparent content, displayed as the default content when nothing is assigned to the slot
On this page

Example

#
HTML
<template id="course-card-template">
  <style>
    :host { display: block; margin-bottom: 12px; }
    .card { max-width: 280px; padding: 12px 16px; border: 1px solid #8886; border-radius: 12px; }
    h3, ::slotted(h3) { margin: 0 0 6px; }
  </style>
  <article class="card">
    <slot name="title"><h3>Untitled course</h3></slot>
    <slot>A description is coming soon.</slot>
  </article>
</template>

<course-card>
  <h3 slot="title">HTML from scratch</h3>
  Build your first page in a week.
</course-card>
<course-card></course-card>

<script>
  customElements.define('course-card', class extends HTMLElement {
    constructor() {
      super()
      const template = document.getElementById('course-card-template')
      this.attachShadow({ mode: 'open' }).append(template.content.cloneNode(true))
    }
  })
</script>
Preview

The second card has no content, so the slots show their default content.

Definition and usage

#

The <slot> element works inside the Shadow DOM, the internal, isolated structure of a web component. It marks the place where content written on the page between the component’s tags should appear. This lets a single component, such as a course card, display different titles and descriptions.

A named slot (<slot name="title">) accepts elements with the slot="title" attribute, while an unnamed slot takes all the remaining content. What you write inside the <slot> itself is the default content, visible when nothing is assigned to the slot.

The inserted elements still belong to the page’s regular DOM, so you style them with normal CSS. From inside the component you can additionally style them with the ::slotted() selector, and react to content changes in the slotchange event.

Syntax

#
Syntax
<slot name="name">Default content</slot>

Attributes

#
  • name

    text

    The slot’s name. Elements of the component with a slot attribute of the same value end up in it. An unnamed slot takes all the remaining content.

The element also supports all global attributes, such as class, id, style and hidden.

More examples

#
Without JavaScript: declarative Shadow DOM
HTML
<user-badge>
  <template shadowrootmode="open">
    <style>
      .badge { display: inline-flex; gap: 8px; padding: 4px 12px; border-radius: 999px; background: #8b5cf633; }
    </style>
    <span class="badge">
      <slot name="level">Level 1</slot>
      <slot>Anonymous</slot>
    </span>
  </template>
  <b slot="level">Level 7</b>
  Anna
</user-badge>
Preview

Default CSS

#

Most browsers display this element with these default styles:

CSS
slot {
  display: contents;
}

Browser support

#

Widely available · since 2020

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Don’t confuse the <slot> element with the slot attribute. The element lives in the component’s Shadow DOM, and the attribute goes on the content you direct into it.
  • ::slotted() lets you style only the directly inserted elements, not their descendants. Set deeper styles in the page’s regular CSS.
  • <slot> only makes sense inside a Shadow DOM. Placed in the page’s regular HTML, it does nothing special.

Related entries

#

Spotted a mistake or missing an example? Write to us.