Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 39 ยท Getting Started

What Are Web Components?

Meet Web Components, the browser's built-in way to create reusable HTML elements. Learn the three core technologies, what they solve and what runs on this site.

Every web page you have ever seen is built from elements: <button>, <input>, <video>, <details>. Each one bundles markup, styling and behavior into a single tag you can drop anywhere. Web Components let you create your own elements of that kind, using nothing but the browser. You write a class, give it a tag name such as <star-rating>, and from then on any page can use it like a native element.

The big idea is reuse without a framework. A component written once works in plain HTML, in a content management system, inside React or Vue, or in a page that was written ten years ago. It needs no build step, no virtual DOM and no library, because the platform itself understands it.

The problem Web Components solve

Imagine you built a nice "alert box" with HTML, CSS and a little JavaScript. To reuse it you copy a block of markup, a block of CSS and a script. Soon three problems appear:

  • Copy and paste. Every page repeats the same markup. Fix a bug and you must fix it everywhere.
  • Style collisions. Your .title class clashes with the .title class of the page that hosts the alert.
  • Leaky internals. A script on the page can reach into your alert and break it with a stray querySelector.

Web Components fix each problem with a dedicated feature. One tag replaces the copied markup, a style boundary keeps CSS from leaking in or out, and a protected subtree hides the internals.

The three core technologies

"Web Components" is an umbrella name for a small group of standards that work together.

Three building blocks

Custom Elements Your own tag with its own class
Shadow DOM A private tree with scoped CSS
Templates Inert markup you can clone

Each one is useful alone. Together they make a component.

1 / 4

Custom Elements is the API for defining a new tag and attaching a JavaScript class to it. Shadow DOM gives an element a private internal tree whose styles and structure are isolated from the page. HTML templates (<template> and <slot>) provide reusable chunks of markup and a way for the user of a component to pass content into it.

You can use them separately. A custom element without Shadow DOM is perfectly valid, and Shadow DOM can be attached to ordinary built-in elements such as a <div>. They shine when combined.

A tiny taste

Here is the smallest useful custom element. It defines a <hello-badge> tag that shows a greeting. You will study every line in the next lessons, so just read it for the shape.

Your first custom tag

The two <hello-badge> tags were written in plain HTML. When customElements.define ran, the browser found them, created instances of your class and called connectedCallback. No framework touched the page.

Where Web Components are used today

You already use them even if you never wrote one. Large products ship design systems as Web Components so that every team, whatever framework it prefers, gets identical buttons and dialogs. Embedded widgets such as video players, maps, chat bubbles and payment forms often hide behind a custom tag so that their CSS cannot break the host page. Content sites use them for small interactive islands inside mostly static HTML.

They are also standard, which matters for longevity. Frameworks come and go, but a component written against the DOM API works as long as browsers do.

What Web Components are not

Be realistic about the limits:

  • They are low level. The platform gives you tags, encapsulation and lifecycle callbacks, but no template syntax with data binding and no state management. You write rendering code yourself, or use a tiny helper library (covered in the tooling chapter).
  • They need JavaScript to define the element. The tag is plain HTML, but its behavior comes from a script.
  • They are not a replacement for every framework feature, such as routing or server rendering. Declarative Shadow DOM, which you will meet later, narrows the server rendering gap.

All examples in this track run in your browser inside a sandboxed preview. Web Components are part of every modern browser, so no library is loaded. Examples that would need a build tool or a server are shown read-only and clearly labelled.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which three technologies make up Web Components?

    Custom Elements define new tags, Shadow DOM isolates their internals, and templates and slots provide reusable markup.
  2. 2. Why must a custom element name contain a dash?

    The HTML standard promises never to create built-in elements with a dash, so names like my-card are safe.
  3. 3. Do Web Components require a framework such as React?

    They are web standards implemented by the browser. Frameworks can use them but are not needed.
  4. 4. What does Shadow DOM mainly provide?

    Shadow DOM isolates an element's internal structure and CSS from the rest of the page.

Summary

  • Web Components let you create reusable, framework-free HTML elements.
  • The three technologies are Custom Elements, Shadow DOM and templates with slots.
  • They solve copy-paste markup, style collisions and leaky internals.
  • They are low level: you write the rendering logic yourself or add a small helper.
  • A custom tag in plain HTML is upgraded when customElements.define runs.

Next: Your First Web Component