Skip to content

Web Components and Custom Elements

Web Components let you create reusable, encapsulated custom HTML elements. This tutorial covers Custom Elements, Shadow DOM, and templates—the foundation of modern component-based web development.

What you’ll learn:

  • What Web Components are and why they matter
  • Defining custom elements with CustomElementRegistry
  • Lifecycle callbacks (connectedCallback, disconnectedCallback, etc.)
  • Attributes versus properties
  • Shadow DOM for encapsulation
  • HTML templates and slots
  • Practical custom element examples
  • When to use custom elements

Prerequisites:

  • Comfortable with HTML, CSS, and JavaScript
  • Understanding of ES6 classes and modules

Web Components are a set of web platform APIs that let you create new HTML elements with encapsulation and reusability. They consist of:

  1. Custom Elements — Define new HTML tags
  2. Shadow DOM — Encapsulate styles and markup
  3. HTML Templates — Reusable markup blocks
  4. ES Modules — Organize and share components

Benefits:

  • Framework-agnostic — Work with any framework or vanilla JS
  • Reusable — Define once, use anywhere
  • Encapsulated — Styles and markup isolated from the rest of the page
  • Native — Part of the web platform standard
  • Performant — No framework overhead

Web Components work in all modern browsers (Chrome 67+, Firefox 63+, Safari 10.1+, Edge 79+). Polyfills exist for older browsers.


A custom element is a JavaScript class that extends HTMLElement.

Result
Result
Result

Custom elements have hooks that fire at specific lifecycle points.

Fires when the element is inserted into the DOM.

Result

Fires when the element is removed from the DOM.

Result

Fires when observed attributes change.

Result

Fires when the element is adopted into a new document (rarely used).

Result

Custom elements can have both HTML attributes and JavaScript properties. Handle both properly.

Result
Result

Shadow DOM encapsulates styles and markup, preventing conflicts with the rest of the page.

Result
Result

Slots let users pass content into your component.

Result

HTML templates provide reusable markup that doesn’t render until cloned.

Result
Result

Result
Result
Result

  • Reusable UI components — Buttons, cards, modals with consistent styling
  • Brand consistency — Enforce company design system across projects
  • Complex interactions — Encapsulate behavior and styling
  • Web Components libraries — Create shareable component libraries
  • Framework integration — Use web components with any framework
  • Simple styling — Use CSS classes instead
  • One-off components — If used only once, custom elements add overhead
  • Heavy logic — Consider lightweight libraries if component is very complex
  • SEO-critical content — Search engines crawl shadow DOM, but encapsulation may complicate indexing

Web components work well with:

  • Vue.js — Excellent support
  • React — Good support, but event handling nuances exist
  • Angular — Built-in support
  • Svelte — Excellent support
  • Vanilla JS — Native support

Result

  • Custom Elements extend HTMLElement to create new HTML tags
  • Lifecycle callbacks (connectedCallback, etc.) manage component behavior
  • Attributes pass configuration via HTML; properties access via JavaScript
  • Shadow DOM encapsulates styles and markup, preventing conflicts
  • Templates and slots provide flexible, reusable content structures
  • Custom elements are framework-agnostic and work everywhere
  • Use custom elements for reusable UI components and design systems
  • Always clean up in disconnectedCallback to prevent memory leaks
  • Prefer open shadow DOM for debugging; use closed only when necessary

Progressive Enhancement

Build resilient websites that work without JavaScript. Continue →

Accessibility

Make sure your custom elements are accessible to everyone. Continue →