<template> holds markup that isn't part of the page. The parser puts its children into template.content, a DocumentFragment owned by a separate inert document with no browsing context: nothing renders, <script>s don't run, images don't load, custom elements aren't upgraded, and document.querySelector() doesn't see it. You stamp copies with template.content.cloneNode(true) or document.importNode(template.content, true); each copy becomes live when inserted, so its scripts and images run per copy. Parsing once and cloning many times is the point.
<slot> lives in a shadow tree and marks where the host's own children (its light DOM) are rendered:
- A child with
slot="title"goes to the first<slot name="title">. - Every other child, including text and whitespace text nodes, goes to the default slot, the first
<slot>without a name. - Only direct children of the host are slotted; a
slotattribute deeper down is ignored. - A child that matches no slot isn't rendered, though it stays in the DOM.
- A slot's own children are fallback content, rendered only when nothing is assigned to it.
Slotted nodes aren't moved: they stay in the light DOM, so page styles and queries still apply to them. Inside, ::slotted(<compound>) styles them, but only top-level slotted elements, and outer page rules win over it. Events from slotted nodes travel through the slot, then up the shadow tree to the host.
From script, slot.assignedNodes() (all nodes) and slot.assignedElements() (elements only) list the assignment, { flatten: true } resolves nested slots and fallback, and node.assignedSlot goes the other way. The slotchange event fires on a slot when its assigned nodes change, in a microtask; it bubbles but isn't composed. With attachShadow({ slotAssignment: 'manual' }), the component chooses instead: slot.assign(node1, node2).