Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

slot — a content placeholder in Shadow DOM

The slot element marks a place in a Shadow DOM tree for assigned nodes. When none are assigned, it represents its own fallback content.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<slot name="title">Fallback title</slot>
<slot>Fallback content</slot>

slot typically appears in a Shadow DOM tree. It represents assigned nodes or, when none are assigned, its own fallback content. Both opening and closing tags are required.

With name-based assignment, name="title" on slot matches slot="title" on an element that is a child of the host. These are different attributes: name names the placeholder, whereas slot selects its destination. An unnamed slot is the default destination for content with an empty slot name, including text, rather than content assigned to a named slot.

assignedElements() returns assigned elements, while assignedNodes() also includes text nodes. Without the flatten option, these methods do not return fallback content as assigned nodes.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Slot example</title>
<div id="host"><span slot="title">Hello</span></div>
<script>
  const host = document.getElementById('host');
  const root = host.attachShadow({ mode: 'open' });
  root.innerHTML = '<p><slot name="title">Fallback title</slot></p><p><slot>Fallback content</slot></p>';
  const titleSlot = root.querySelector('slot[name="title"]');
  console.log(titleSlot.assignedElements()[0].textContent);
</script>
</html>

Result

Hello
Fallback content

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources