Skip to content

<slot> - Shadow DOM Slot Element

Web Components HTML5

The <slot> element is a placeholder inside a Web Component that users can fill with their own markup. It enables content projection in Shadow DOM, allowing component consumers to pass custom content into predefined areas of your component while maintaining encapsulation.

Result
<!-- Inside Shadow DOM -->
<slot></slot> <!-- Default slot -->
<slot name="header"></slot> <!-- Named slot -->
<slot>Fallback content</slot> <!-- Slot with fallback -->
<!-- Using slots (Light DOM) -->
<my-component>
<div slot="header">Goes to named slot</div>
<p>Goes to default slot</p>
</my-component>
AttributeValueDescription
nameStringIdentifies a named slot

The <slot> element supports all global attributes.

Understanding the difference between slot types:

Result

Slots can have default content that appears when no content is provided:

Result

Use the ::slotted() pseudo-element to style content from the light DOM:

Result

Monitor when slot content changes:

Result

Access assigned content programmatically:

Result
Result
Result

Excellent Modern Support

The <slot> element is supported in all modern browsers:

  • Chrome: 53+ (2016)
  • Firefox: 63+ (2018)
  • Safari: 10+ (2016)
  • Edge: 79+ (2020)
  • Opera: 40+ (2016)
  • Mobile: Widely supported

Note: Requires Shadow DOM support. Use polyfills for older browsers.

  • <template> - Content templates
  • Shadow DOM - Encapsulated DOM trees
  • Custom Elements - Define new HTML elements
  • Web Components - Reusable component standard
  1. Provide fallback content: Always include default content in slots
  2. Name slots clearly: Use descriptive names for named slots
  3. Keep slot structure simple: Don’t overcomplicate slot hierarchies
  4. Use ::slotted() carefully: Remember it only styles direct children
  5. Document slots: Clearly document which slots your component accepts
  6. Event handling: Be aware of event retargeting in Shadow DOM
  7. Accessibility: Ensure slotted content maintains accessibility
  8. Test without content: Verify fallback content works correctly
  9. Monitor changes: Use slotchange events when needed
  10. Style defensively: Remember light DOM styles can override

❌ Avoid

  • Relying on slot content being present
  • Complex nested slot structures
  • Overusing named slots
  • Forgetting ::slotted() limitations
  • Not testing with empty slots

✅ Do Instead

  • Provide meaningful fallback content
  • Keep slot structure flat and simple
  • Use default slot when possible
  • Test both with and without content
  • Document expected slot usage

Learn More: