The two elements that start it off
Conference talks on web components tend to frame them as a lot of JavaScript wrapped around a trivial gain, and this author counted themselves among the skeptics. Building panels and extra material for a teaching project aimed at covering every HTML element in the spec meant a first real encounter with <slot> and <template>, and the conclusion from working through them properly is straightforward: web components are a lot easier than they seem.
A <template> — as the name suggests — holds template HTML structure for the component. Both it and <slot> are ordinary HTML elements, and neither implies anything over ±1000 lines of JS.
<template>
<p>The Zombies are coming!</p>
</template>
Adding the slot
<slot> customizes what the <template> renders on the page.
<template>
<p>The <slot>Zombies</slot> are coming!</p>
</template>
<template>
<p>The <slot name="whats-coming">Zombies</slot> are coming!</p>
</template>
The name attribute identifies which content goes where in the template: what has been done here is that a slot named whats-coming means that zombie is coming first, but the component gives some flexibility to slot something else in.
Using the component
<apocalyptic-warning>
<span slot="whats-coming">Halitosis Laden Undead Minions</span>
</apocalyptic-warning>
<template>
<p>The <slot name="whats-coming">Zombies</slot> are coming!</p>
</template>
Registering with JavaScript
JavaScript is required for the component to swap a slot, but only a constructor function is needed to register the custom element.
// Defines the custom element with our appropriate name, <apocalyptic-warning>
customElements.define("apocalyptic-warning",
// Ensures that we have all the default properties and methods of a built in HTML element
class extends HTMLElement {
// Called anytime a new custom element is created
constructor() {
// Calls the parent constructor, i.e. the constructor for `HTMLElement`, so that everything is set up exactly as we would for creating a built in HTML element
super();
// Grabs the <template> and stores it in `warning`
let warning = document.getElementById("warningtemplate");
// Stores the contents of the template in `mywarning`
let mywarning = warning.content;
const shadowRoot = this.attachShadow({mode: "open"}).appendChild(mywarning.cloneNode(true));
}
});
const shadowRoot = this.attachShadow({mode: "open"}).appendChild(mywarning.cloneNode(true));
The constructor does several things: it takes the custom element (this) and creates a shadow DOM, and then the shadow DOM appends a deep copy of the template as a node. A shadow root mode of open simply means JS from outside can access and manipulate elements inside the shadow DOM. With the template attached, the <slot> and slot attribute take over to match up content with where it should go. Two instances of the same component can then render different content by changing one element.
A gotcha and the naming rule
Custom element names must include a hyphen. That's a spec rule, and it keeps a new web component from colliding with a same-named element if HTML releases one.
Single encapsulated CSS
A <style> element can also be included right in the <template>.
<template id="warningtemplate">
<style>
p {
background-color: pink;
padding: 0.5em;
border: 1px solid red;
}
</style>
<p>The <slot name="whats-coming">Zombies</slot> are coming!</p>
</template>
The styles are scoped directly to the component and, thanks to the shadow DOM, nothing leaks out to other elements on the page. But the mental model of copy-insert-inject is wrong. In the DOM, the content in a custom element stays where it is and the shadow DOM is laid over it.

So any descendant selectors or classes in the template's <style> element will have no affect on the slotted content. However the custom element can be used as an element selector in any CSS file—including a main stylesheet—and descendant selectors from though CSS will work.
apocalyptic-warning span {
color: blue;
}
The reverse caveat stands: styles from the main CSS file cannot reach elements in the <template> or shadow DOM.
Wrapping it up
© 2026 Tech Report.



