Skip to content

Web Components

In one line

Web Components are a set of browser standards for creating reusable, encapsulated custom HTML elements with their own behavior.

In simple words

Web Components are a suite of browser-native technologies that let you define your own HTML elements — like <my-button> or <user-card> — with custom behavior and styling that does not leak in or out. They are the platform's built-in answer to component frameworks like React, but with no library required.

The three pillars are Custom Elements (register a new tag name with a class), Shadow DOM (an isolated DOM subtree so styles and markup do not collide), and HTML Templates (<template> tags for reusable markup). Together they let you build self-contained widgets that work in any framework or none.

Web Components shine for design systems shared across teams using different frameworks, for embedding widgets in third-party sites, and for progressive enhancement. They interoperate with React/Vue/Astro but have a lower-level ergonomics, so many teams use a helper library like Lit.

Example

javascript
class MyGreeting extends HTMLElement {
  connectedCallback() {
    this.innerHTML = `<p>Hello, ${this.getAttribute("name")}!</p>`;
  }
}
customElements.define("my-greeting", MyGreeting);

// Usage in HTML: <my-greeting name="Ada"></my-greeting>
// Renders: <p>Hello, Ada!</p>

customElements.define registers a new tag. connectedCallback runs when the element is added to the DOM. The tag works like any built-in HTML element.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary