HTML <slot> Tag

The HTML <slot> element defines a slot, typically in a shadow tree.

The <slot> element is a placeholder inside a web component created using the shadow DOM specification, that you can insert your own markup into. This allows you to create and combine separate DOM trees.

Syntax

The <slot> element is written as slot name=""/slot (end tag is required), where name is the name of the slot.

Other elements can refer to this <slot> element's name with the slot attribute.

Like this:

Example

Here's an example to demonstrate.

Don't get put off by the amount of code. We can chunk this into three parts:

  1. Template: The template element is used to declare fragments of HTML that can be cloned and inserted in the document by script. Its contents are not rendered until they is added to the document using a script. This is the part that contains the <slot> elements. They act as placeholders for the content to be inserted.
  2. Content: This part contains the content that is inserted where the <slot> elements are in the template. So in this case the span elements will end up where the <slot> elements are. Each span element refers to a specific <slot> element via its slot attribute. For example span slot="term-1" refers to slot name="term-1".
  3. Script: This is the part that inserts the contents into the shadow DOM as defined by the template element.

So in this case, it's taking the content from the span elements, and applying it to two definition lists and their respective headings.

Here it is with a preview:

Styles

One of the interesting things about this approach, is that any CSS that you include in the template element is only applied to the shadow DOM tree. It won't affect the rest of the page.

Let's tweak the previous example to demonstrate what I mean:

In this example, the first list is inserted with the <slot> element but the second list is not.

In this example, I explicitly put the medical terms into their own definition list. I also moved the heading to its own h1 element.

This results in the second list being unstyled. This is because the only styles I'm declaring are inside the template element, which means that they're only applied to HTML elements that are within that shadow DOM tree.

Conversely, if I move the styles outside of the template element, those styles are only applied to the second list, and the first list goes unstyled:

Attributes

Attributes can be added to an HTML element to provide more information about how the element should appear or behave.

The <slot> element accepts the following attributes.

AttributeDescription
nameName of shadow tree slot.

Global Attributes

The following attributes are standard across all HTML elements. Therefore, you can use these attributes with the <slot> tag , as well as with all other HTML tags.

For a full explanation of these attributes, see HTML 5 global attributes.

Event Handlers

Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event.

Most event handler content attributes can be used on all HTML elements, but some event handlers have specific rules around when they can be used and which elements they are applicable to.

For more detail, see HTML event handler content attributes.