Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 38 ยท Getting Started

What Are Web Components?

Learn what web components are, the four browser features behind them, why they work in every framework, and build a tiny working component with no libraries.

A web component is a reusable HTML element that you define yourself. Instead of copying the same block of markup, styles and script into every page, you give it a name such as <user-card>, write it once, and then use it like any built-in tag. The browser itself runs it. No framework is required, and the element works in plain HTML, in a content management system, or inside React, Vue, Angular and Svelte pages.

This track teaches web components in two layers. First you learn the platform features that make them possible. Then you learn Lit, a very small library that removes the boilerplate and adds reactive rendering. Understanding the layer underneath makes Lit feel simple instead of magical.

The four building blocks

"Web components" is not one API. It is a name for a set of browser standards that work together.

Four browser features

Custom Elements Your own tag names with your own behavior
Shadow DOM Private markup and CSS inside an element
HTML Templates Inert markup you can stamp out many times
ES Modules Share components as plain JavaScript files

Together they make a component

1 / 2
  • Custom Elements let you register a JavaScript class for a tag name. Whenever the browser finds that tag, it creates an instance of your class.
  • Shadow DOM gives an element a private subtree. CSS inside it does not affect the page, and page CSS does not break it. You will study it in Shadow DOM Basics.
  • HTML templates (<template> and <slot>) hold markup that the browser parses but does not render until you clone it. See The Template Element.
  • ES modules are how you ship a component: one .js file that you load with <script type="module">.

A custom element in ten lines

The smallest possible web component is a class that extends HTMLElement and a call to customElements.define. Run this and then edit the text.

Your first custom element

Notice what happened. You wrote <hello-badge> in the HTML before the class existed. The browser treated it as an unknown element first, then upgraded it the moment define ran. This "upgrade" behavior is why component scripts can load late without breaking the page. Custom Elements: The Basics explores it fully.

Why not just copy and paste?

Consider a card that shows a user. Without components you repeat markup and CSS in each place, and a design change means editing every copy. With a component you change one file. The element also carries its behavior: a "like" button that toggles, a menu that opens, a date picker that validates.

Frameworks solve the same problem, but each one solves it in its own way. A React component cannot be dropped into an Angular app without a wrapper. A web component can, because it is just an HTML element and every framework already knows how to render HTML elements. This makes web components popular for design systems, widgets embedded in third-party pages, and long-lived products that outlive any one framework.

Encapsulation in action

Page styles normally apply everywhere. The next example shows a web component whose styles are private. The page CSS tries to color every p red, but the paragraph inside the component's shadow root ignores it.

Shadow DOM keeps styles private

The first paragraph is red and the second is green, even though both selectors say p. Each tree has its own style scope.

Where Lit fits

Writing attachShadow, innerHTML and attribute handling by hand gets repetitive, and re-rendering by replacing innerHTML throws away state. Lit adds three things on top of the standard APIs:

  1. Declarative templates written as tagged template literals (html and css).
  2. Reactive properties that re-render the component when they change.
  3. Efficient updates that touch only the parts of the DOM that changed.

Lit is about 5 KB minified and gzipped, has no build step requirement, and produces ordinary custom elements. A Lit component is still a web component, so everything in this chapter still applies.

flowchart LR
  A["Browser standards<br/>custom elements, shadow DOM,<br/>templates, modules"] --> B["Lit<br/>html, css, reactive properties"]
  B --> C["Your components<br/>user-card, app-menu, data-table"]
  C --> D["Any page or framework"]

Lit sits on top of the standards. Your components remain standard elements.

Browser support and limits

All four features work in current Chrome, Edge, Firefox and Safari. Two limits are worth knowing now:

  • Customized built-in elements (<button is="my-button">) are not supported in Safari. This track focuses on autonomous elements with a dash in their name, which work everywhere.
  • Server-side rendering of shadow DOM needs extra tooling (Declarative Shadow DOM and Lit's SSR packages). Components still render correctly on the client without it.

Every web component tag name must contain a hyphen, such as user-card. Names without one are reserved for built-in HTML elements.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which statement about web components is correct?

    Custom elements, shadow DOM and templates are built into the browser, so no library is required.
  2. 2. Which tag name is valid for a custom element?

    Custom element names must be lowercase and contain a hyphen so they never clash with built-in tags.
  3. 3. What does Shadow DOM mainly provide?

    A shadow root is a private subtree: its CSS and DOM are separated from the surrounding page.
  4. 4. What does Lit add on top of the platform?

    Lit components are still standard custom elements; Lit only makes rendering and reactivity easier.

Summary

  • A web component is a custom HTML element defined with a class and customElements.define.
  • Four standards make it possible: custom elements, shadow DOM, HTML templates and ES modules.
  • Because they are plain elements, web components work in any framework or none.
  • Shadow DOM keeps component styles and markup private.
  • Lit is a tiny library that adds templates, reactivity and fast updates on top of the standards.

Next: What Is Lit?