<slot>...</slot>

HTML4 HTML5

The <slot> tag is a special element in Web Components technology (Shadow DOM)[cite: 3]. It acts as a placeholder inside a component template where custom external HTML content can be injected[cite: 3].

This allows creating flexible, reusable components with fixed internal structures whose specific sections (e.g., header or footer) can be modified externally[cite: 3].

Both opening and closing tags are mandatory[cite: 3].

Attributes:

Example 1

In the example below, a card component template defines a <slot> with attribute name="card-text" for custom text[cite: 3]. If no content is provided, default fallback text inside the slot is shown[cite: 3]:

Code:

<template id="custom-card-template"> <div style="border: 1px solid #999; padding: 10px; border-radius: 4px;"> <h3 style="margin-top: 0;">Component Card</h3> <p> <slot name="card-text">This is default card text (shown when custom content is missing).</slot> </p> </div> </template>

The browser will interpret this template and its slots (when rendered in a component) as follows[cite: 3]:

Component Card

This is default card text (shown when custom content is missing).

Example 2: Two slots (one with default content, one injected)

In this example, the component features two slots: title and footer[cite: 3]. We inject a custom title into the first slot, while the second remains empty, rendering its default content[cite: 3]:

Code:

<!-- Component template with two slots --> <template id="user-card-template"> <div style="border: 1px solid #666; padding: 12px;"> <header> <slot name="title">Default header title</slot> </header> <footer style="margin-top: 10px; font-size: 0.9em; color: #555;"> <slot name="footer">Default component footer text</slot> </footer> </div> </template> <!-- Component usage: custom title injected, footer left default --> <user-card> <h4 slot="title" style="margin: 0;">Injected Title: User Profile</h4> </user-card>

The browser will interpret this structure as follows (header replaced by custom text, footer keeps default text)[cite: 3]:

Injected Title: User Profile