HTML <slot> Tag
<slot> element defines a slot, typically in a shadow tree.
<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.
<slot> element is written as
/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
Here's an example to demonstrate.
Don't get put off by the amount of code. We can chunk this into three parts:
- Template: The
templateelement 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.
- Content: This part contains the content that is inserted where the
<slot>elements are in the template. So in this case the
spanelements will end up where the
<slot>elements are. Each
spanelement refers to a specific
<slot>element via its
slotattribute. For example
span slot="term-1"refers to
- Script: This is the part that inserts the contents into the shadow DOM as defined by the
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:
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
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 can be added to an HTML element to provide more information about how the element should appear or behave.
<slot> element accepts the following attributes.
|Name of shadow tree slot.|
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 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.