Home / Learn HTML / HTML Web Components and Template

HTML Web Components and Template

Create your own HTML tags, like <my-card>, and reuse them anywhere.

In shortA custom element name needs a hyphen. Shadow DOM keeps its style private.

Definition

Web Components are your own reusable HTML tags. They rest on three parts: custom elements (a new tag name with a dash, like my-card), shadow DOM (private markup and style) and the template and slot elements.

Theory in plain words

A custom elementstyleslotshadow rootmy-card
A custom element

What it is

Web Components are your own reusable HTML tags built with custom elements, shadow DOM and templates.

Why we use it

They let you reuse widgets across projects and keep styles inside.

How it works

You define a class and register it with customElements.define. The shadow DOM hides its inside from the rest of the page.

Where it is used

  • Design systems
  • Reusable widgets
  • Embeddable components
Template and cloneTry it Yourself
Your first custom elementTry it Yourself
Custom element with an attributeTry it Yourself
Shadow DOM (private styles)Try it Yourself
SlotsTry it Yourself
Button with stateTry it Yourself

Quick quiz

1. A custom element name must contain

2. Shadow DOM keeps

3. <template> content is

My notes
Ask a question about this lesson