![]() |
In the modern world of front-end development, usability, encapsulation, and maintainability are fundamental principles. Web Components perfectly meet these needs, offering a set of standard technologies for creating custom, reusable, and framework-independent elements.
Two key elements that complete this architecture are the HTML tags <template>
and <slot>
. In this article, we'll explore what they are, how they work, and how to use them together to build modular and scalable interfaces.
1. What is a Web Component?
Web Components are a set of standard web APIs that allow you to create custom HTML elements with encapsulated behavior and style.
A Web Component is composed of three main technologies:
- Custom Elements: API for defining new custom HTML tags.
- Shadow DOM: Isolated DOM that encapsulates structure and style.
- HTML Templates: Reusable, non-immediately rendered content used as the basis for components.
2. The <template> tag
The <template>
tag allows you to define HTML fragments that are not immediately rendered. Its content is processed only when explicitly inserted into the DOM via JavaScript.
Basic example:
<template id="card-template">
<style>
.card {
border: 1px solid #ccc;
padding: 1em;
border-radius: 5px;
}
</style>
<div class="card">
<slot name="title"></slot>
<slot name="content"></slot>
</div>
</template>
3. The <slot>
tagThe <slot>
tag represents an insertion point for the user-provided content of the component. It is used to create flexible and modular components.
Slot Types:
- Default slot: Unnamed, inserts all unassigned content.
- Named slot: With
name=""
, allows you to place specific content at specific points on the component.
4. Create a complete Web Component
Let's now see a complete example that combines all three concepts: Web Component, <template>
and <slot>
.
Step 1: Component definition
<template id="user-card">
<style>
.user-card {
border: 1px solid #ddd;
padding: 16px;
border-radius: 8px;
background-color: #f9f9f9;
}
.username {
font-weight: bold;
}
</style>
<div class="user-card">
<div class="username"><slot name="name">Anonymous</slot></div>
<div class="bio"><slot name="bio">No description available.</slot></div>
</div>
</template>
Step 2: Registering the Custom Element
<script>
class UserCard extends HTMLElement {
constructor() {
super();
const template = document.getElementById('user-card');
const shadow = this.attachShadow({ mode: 'open' });
shadow.appendChild(template.content.cloneNode(true));
}
}
customElements.define('user-card', UserCard);
</script>
Step 3: Using the component
<user-card>
<span slot="name">Joe Smith</span>
<p slot="bio">Front-end developer with a passion for design.</p>
</user-card>
Step 4: Component Output
![]() |
5. Advantages of the Web Components approach
- ✅ Reusability: self-contained components, ready to be reused in any project.
- ✅ Encapsulation: thanks to the Shadow DOM, styles and behaviors do not interfere with the rest of the page.
- ✅ Framework independence: Web Components are compatible with all modern libraries (React, Vue, Angular) or vanilla JS.
6. Final Thoughts
Web Components, <template>
, and <slot>
provide a solid foundation for building modular and scalable web interfaces. Although they are relatively new native technologies compared to popular frameworks, they are gaining increasing traction thanks to their versatility and native support in modern browsers.
Knowing how to use them allows you to build maintainable, clean, and future-proof applications.
Follow me #techelopment
Official site: www.techelopment.it
facebook: Techelopment
instagram: @techelopment
X: techelopment
Bluesky: @techelopment
telegram: @techelopment_channel
whatsapp: Techelopment
youtube: @techelopment