Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

shadowrootslotassignment — assigning nodes to slots

The shadowrootslotassignment attribute on template selects how nodes are assigned to slots in a declarative shadow root: automatic named assignment or explicit manual assignment.

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

Syntax

<template shadowrootmode="open" shadowrootslotassignment="named|manual">…</template>

shadowrootslotassignment is an enumerated attribute on template that configures slot assignment when a declarative shadow root is created. Use it with a valid shadowrootmode; it does not create a root by itself. In the syntax shown, named|manual means choose one value, not that literal text.

The value named enables automatic matching between a host child's slot attribute and a shadow-tree slot element's name attribute. An omitted attribute, an empty value, and an invalid value all select the named state. This is not a boolean attribute: its presence alone does not enable manual mode.

The value manual disables automatic name-based assignment. A script assigns nodes through HTMLSlotElement.assign(). Until a slot has assigned nodes, it represents its own fallback content. In the example, assign(item) assigns an element that is a direct child of the host.

The setting is read when the parser creates the root; changing the attribute later does not reconfigure an existing root. The example requires support for this attribute, not merely declarative Shadow DOM.

Examples

<!doctype html>
<meta charset="utf-8">
<title>Manual slot assignment</title>
<div id="host">
  <template shadowrootmode="open" shadowrootslotassignment="manual">
    <slot>Fallback</slot>
  </template>
  <span id="item">Hello</span>
</div>
<pre id="result"></pre>
<script>
  const host = document.getElementById('host');
  const item = document.getElementById('item');
  const slot = host.shadowRoot.querySelector('slot');
  const before = slot.assignedNodes().length;
  slot.assign(item);
  const after = slot.assignedNodes().length;
  document.getElementById('result').textContent =
    `${before} -> ${after}: ${slot.assignedNodes()[0].textContent}`;
</script>

Result

0 -> 1: Hello

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