HTML <slot> Tag

The <slot> element is a placeholder inside a Web Component that you fill with your own markup when the component is used. It lets you keep a private shadow tree while composing light-DOM content into it.

Use the name attribute for named slots. Host children with a matching slot attribute are projected there. An unnamed <slot> is the default slot. Markup inside the slot is fallback content.

Baseline · Widely available.

Stable across browsers since about January 2020. Pair with attachShadow() or declarative shadow DOM via <template>.

Shadow trees only.

Slots project content when they live in a shadow root. Outside Shadow DOM they behave like ordinary elements.

HTML Syntax

syntax.html
<slot name="title">fallback</slot>

Both start and end tags are required. Fallback content sits between the tags.

Attributes

Besides global attributes, <slot> defines:

AttributeDescriptionTry it
nameNamed slot identifier. Host children with slot="…" match this name. Omit for the default slotTry it
(fallback content)Children of the slot shown when nothing is assignedTry it
class / idHooks for styling and scripting the placeholderTry it

Global Attributes

The <slot> tag also supports the Global Attributes in HTML.

Live Preview

Attach a shadow root with a default or named slot and project host children.

Uses attachShadow + <slot>. Pick a pattern, then Apply.

Preview

Default slot · projects unmarked children

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Default Slot

Unnamed children of the host fill the default <slot>.

HTML
<my-card id="card">
  Hello from light DOM
</my-card>

<script>
  const host = document.getElementById("card");
  const root = host.attachShadow({ mode: "open" });
  root.innerHTML = "<strong><slot></slot></strong>";
</script>
Try It Yourself

How It Works

  1. Attach a shadow root on the host element.
  2. Place an unnamed <slot> in the shadow tree.
  3. Leave host children without slot so they fill the default slot.
  4. Style around the slot in the shadow tree (here, a <strong> wrapper).

Example 2 — Named Slots

Match host slot="…" attributes to <slot name="…">.

HTML
<my-card id="named-card">
  <span slot="title">Sale</span>
  Save 20% this week
</my-card>

<script>
  const host = document.getElementById("named-card");
  const root = host.attachShadow({ mode: "open" });
  root.innerHTML =
    "<strong><slot name=\"title\"></slot></strong> " +
    "<slot></slot>";
</script>
Try It Yourself

How It Works

  1. Declare <slot name="title"> in the shadow tree.
  2. Mark light-DOM children with slot="title".
  3. Keep a default <slot> for unmarked children.
  4. Keep names unique within one shadow root.

Example 3 — Fallback Content

Content inside the slot shows until something is assigned.

HTML
<my-card id="fallback-card"></my-card>

<script>
  const host = document.getElementById("fallback-card");
  const root = host.attachShadow({ mode: "open" });
  root.innerHTML =
    '<slot name="title">NEED TITLE</slot>';
</script>
Try It Yourself

How It Works

  1. Put default text inside <slot>.
  2. Leave the host empty so nothing is assigned.
  3. Browser shows the fallback until a matching child appears.
  4. Assign content later to replace the fallback automatically.

Edge Cases & Pitfalls

shadow

Slots outside Shadow DOM

Without a shadow root, <slot> does not project light-DOM children.

names

Duplicate slot names

Keep names unique per shadow root. Duplicates send nodes to the first matching slot.

attr

Confusing name and slot

name goes on <slot>. The host child uses the slot attribute to choose a destination.

depth

Only top-level host children

With named assignment, only direct children of the host are distributed (not nested descendants).

legacy

Obsolete <shadow>

Do not use the old <shadow> tag. Use attachShadow + <slot>.

events

Listening for updates

Use the slotchange event when assigned nodes change and your UI must react.

Browser Support

The <slot> element is core HTML for Shadow DOM composition. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<slot>

Widely available since about January 2020. Chrome, Firefox, Safari, Edge, and Opera support Web Component slots.

95% Global support (approx.)
Google Chrome 53+
Full support
Mozilla Firefox 63+
Full support
Apple Safari 10+
Full support
Microsoft Edge 79+
Full support
Opera 40+
Full support
Samsung Internet 6+
Full support
Internet Explorer —
Not supported
<slot> Widely supported

Bottom line: Safe for modern Web Components. Always place slots inside a shadow root.

Key Takeaways

  • Role: placeholders that project light-DOM into a shadow tree.
  • name: named slots; omit for the default slot.
  • Fallback: children of the slot show when unassigned.
  • Practice: pair with attachShadow or <template>.

One line: put <slot> in the shadow tree, and match host children with slot="…".

Default CSS Settings

Slots are typically transparent layout-wise; approximate idea:

default.css
slot {
  display: contents; /* common author pattern; UA may vary */
}

Style wrappers around the slot in the shadow tree rather than relying on the slot box itself.

Frequently Asked Questions

A slot is a placeholder inside a Web Component’s shadow tree. Light-DOM children of the host are projected into matching slots so authors can customize the component’s content.
name identifies a named slot. Host children with a matching slot attribute are rendered there. An unnamed slot is the default slot for children without a slot attribute.
Markup placed between the slot’s start and end tags is shown when nothing is assigned to that slot. Once content is assigned, the fallback is not displayed.
Slots only act as insertion points when they live in a shadow root (for example from attachShadow or a template with shadowrootmode). Outside shadow trees they are ordinary elements.
Slot names should be unique per shadow root. If two slots share a name, assigned nodes go to the first matching slot.
slotchange fires on a slot when the nodes assigned to it change. Use it to react when projected content is added or removed.

Next: small

Mark side comments and fine print with the HTML small element.

small tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful