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:
- 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. - Content: This part contains the content that is inserted where the
<slot>
elements are in the template. So in this case thespan
elements will end up where the<slot>
elements are. Eachspan
element refers to a specific<slot>
element via itsslot
attribute. For examplespan slot="term-1"
refers toslot name="term-1"
. - 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.
Attribute | Description |
---|---|
name | Name 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.
accesskey
autocapitalize
class
contenteditable
data-*
dir
draggable
hidden
id
inputmode
is
itemid
itemprop
itemref
itemscope
itemtype
lang
part
slot
spellcheck
style
tabindex
title
translate
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.
onabort
onauxclick
onblur
oncancel
oncanplay
oncanplaythrough
onchange
onclick
onclose
oncontextmenu
oncopy
oncuechange
oncut
ondblclick
ondrag
ondragend
ondragenter
ondragexit
ondragleave
ondragover
ondragstart
ondrop
ondurationchange
onemptied
onended
onerror
onfocus
onformdata
oninput
oninvalid
onkeydown
onkeypress
onkeyup
onlanguagechange
onload
onloadeddata
onloadedmetadata
onloadstart
onmousedown
onmouseenter
onmouseleave
onmousemove
onmouseout
onmouseover
onmouseup
onpaste
onpause
onplay
onplaying
onprogress
onratechange
onreset
onresize
onscroll
onsecuritypolicyviolation
onseeked
onseeking
onselect
onslotchange
onstalled
onsubmit
onsuspend
ontimeupdate
ontoggle
onvolumechange
onwaiting
onwheel
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.