HTML Web Components
What are Web Components?
Web Components allows you to create your own reusable, custom elements.
You write the element once, and then use it as often as you want.
Web Components consists of three main technologies:
- HTML templates
- Custom elements
- Shadow DOM
Web components need a little JavaScript, but the examples on this page are short.
HTML Templates
The HTML <template> element is used as a container to hold some HTML content that will be hidden from the user when the page loads.
The content inside <template> can be rendered later with a JavaScript.
You can copy the content with JavaScript and add the copy to the page:
Example
<template id="fruitTemplate">
<li class="fruit">New fruit</li>
</template>
<ul id="fruits">
<li>Apple</li>
</ul>
<button onclick="addFruit()">Add a fruit</button>
<script>
function addFruit() {
const template = document.getElementById("fruitTemplate");
const copy = template.content.cloneNode(true);
document.getElementById("fruits").appendChild(copy);
}
</script>
Try it Yourself »
When the page loads, the list shows only Apple.
After two clicks, the list shows:
Apple
New fruit
New fruit
template.content holds the HTML inside the template.
cloneNode(true) makes a copy of it, so you can use the template again and again.
Then you add the copy to the page with the appendChild() function.
Create Custom Elements
A custom element is an element that you create yourself.
Note: The name of a custom element must start with a lowercase letter, and must contain a hyphen (to not conflict with future HTML elements).
This example creates a <hello-box> element that says hello to a name:
Example
<hello-box name="Maria"></hello-box>
<hello-box name="Ali"></hello-box>
<script>
class HelloBox extends HTMLElement {
connectedCallback() {
const name = this.getAttribute("name");
this.innerHTML = "<p>Hello, " + name + "!</p>";
}
}
customElements.define("hello-box", HelloBox);
</script>
Try it Yourself »
The page shows:
Hello, Maria!
Hello, Ali!
This is what each part does:
class HelloBox extends HTMLElementcreates a new kind of elementconnectedCallback()called each time the element is added to the pagethisis the element itselfcustomElements.define()makes the element available in the pagedefine()holds the name of the element, and its constructor function
Shadow DOM
Normally, the CSS on a page works on all elements on that page.
A shadow DOM is a hidden part inside your custom element, with its own CSS.
The CSS inside the shadow DOM only works inside it.
You create a shadow DOM with attachShadow() function:
Example
<p>This paragraph is outside the shadow DOM.</p>
<red-box></red-box>
<script>
class RedBox extends HTMLElement {
connectedCallback() {
const shadow = this.attachShadow({mode: "open"});
shadow.innerHTML =
"<style>p { color: red; }</style>" +
"<p>This paragraph is inside the shadow DOM.</p>";
}
}
customElements.define("red-box", RedBox);
</script>
Try it Yourself »
Only the paragraph inside the shadow DOM will be red.
The other paragraphs on the page stay black.
Summary
- Web components let you create your own elements
- The
<template>element holds HTML that is not shown until you copy it - A custom element needs a class constructor and
customElements.define() - A shadow DOM keeps the CSS of a custom element inside the element
HTML Template Tags
| Tag | Description |
|---|---|
| <template> | Defines a container for content that is hidden when the page loads |