Skip to content
Tutorials Center
My learning

Topics

Web Components Tutorial

Learn Web Components step by step: 39 lessons in 9 chapters with examples, quizzes, diagrams and projects.

Start learning →

of lessons completed

Getting Started

  1. 1 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.
  2. 2 Your First Web Component Build your first complete Web Component: a counter button with a custom tag, a class, a shadow root, a template and event handling, explained line by line.
  3. 3 How Custom Elements, Shadow DOM and Templates Fit Together See how Custom Elements, Shadow DOM and HTML templates cooperate inside one component, and try each technology on its own to understand what it contributes.
  4. 4 Web Components and Frameworks Compare Web Components with React, Vue and Angular. Learn what each does best, how they work together, and when a native component is the better choice.
  5. 5 Setting Up and Debugging Web Components Set up a Web Components project with nothing but a text editor, check browser support, organize files and debug custom elements and shadow roots in DevTools.

Custom Elements

  1. 6 Autonomous Custom Elements Learn how to define autonomous custom elements: valid names, the HTMLElement class, constructor rules, display behavior, the registry API and common pitfalls.
  2. 7 Custom Element Lifecycle Callbacks Learn every custom element lifecycle callback: constructor, connectedCallback, disconnectedCallback, attributeChangedCallback, adoptedCallback, and how to clean up.
  3. 8 Attributes and observedAttributes Make custom elements configurable with HTML attributes. Learn observedAttributes, attributeChangedCallback, parsing strings into types and boolean attributes.
  4. 9 Properties and Attribute Reflection Give custom elements a JavaScript API with properties, getters and setters, and learn when and how to reflect properties to attributes without causing loops.
  5. 10 Customized Built-in Elements Extend native elements such as button, a and input with the is attribute. See how customized built-ins work, what they cannot do, and the Safari support gap.
  6. 11 Upgrading Elements and whenDefined Understand how undefined custom elements are upgraded, how to wait with customElements.whenDefined, how to prevent flashes of unstyled content, and lazy-load components.

Shadow DOM

  1. 12 Shadow DOM Basics Learn what Shadow DOM is, how attachShadow creates a shadow root, which elements can host one, and how the shadow tree differs from the light DOM and the page.
  2. 13 Open and Closed Shadow Roots Compare open and closed shadow roots, learn what mode controls, how to keep a private reference, and use the delegatesFocus, clonable and serializable options.
  3. 14 Style Encapsulation in Shadow DOM Understand how CSS is scoped in Shadow DOM, which styles still pass through, and how to style the host with :host, :host() and :host-context() and share stylesheets.
  4. 15 Events and the Shadow Boundary Learn how events cross the shadow boundary: event retargeting, the composed and bubbles flags, composedPath(), and how to dispatch custom events from a component.
  5. 16 Declarative Shadow DOM Create shadow roots straight from HTML with template shadowrootmode, render components on the server, hydrate them, and use getHTML and setHTMLUnsafe from script.

Templates and Slots

  1. 17 The template Element Master the HTML template element: why its content is inert, how to clone and fill it, differences between cloneNode and importNode, and how to use it in components.
  2. 18 Slots: Passing Content into Components Use the slot element to let page authors pass content into a shadow tree. Learn the default slot, fallback content, how slotting works, and what stays in the light DOM.
  3. 19 Named Slots Build components with several content areas using named slots. Learn the name and slot attributes, how unmatched content is handled, and build a reusable card.
  4. 20 slotchange, assignedNodes and Manual Slot Assignment React to slotted content with the slotchange event, read slot.assignedNodes and assignedElements, handle late-arriving children, and use manual slot assignment.
  5. 21 Styling Slotted Content Style slotted elements from three places: the page, the component with ::slotted(), and with inheritance. Learn ::slotted limits, specificity, and the slot pseudo-class.

Events and Properties

  1. 22 Attributes vs Properties in Custom Elements Understand the difference between HTML attributes and JavaScript properties on custom elements, when to use each, and how to design a clean public API.
  2. 23 Reflecting Attributes and Observed Changes Keep attributes and properties in sync with observedAttributes, attributeChangedCallback and reflection, and avoid infinite loops and needless re-renders.
  3. 24 Custom Events and Component Communication Let components talk to the page with CustomEvent. Learn naming, detail payloads, bubbles and composed, cancelable events, and the properties-down, events-up pattern.
  4. 25 Event Retargeting and Composed Events Learn how events behave at shadow DOM boundaries: retargeting, composed vs non-composed events, composedPath(), and how to design events that cross cleanly.

Forms and Accessibility

  1. 26 Form-Associated Custom Elements Build custom form controls that submit values, reset, and join a form like native inputs, using formAssociated, ElementInternals and setFormValue.
  2. 27 Validation with ElementInternals Add native-style constraint validation to custom form controls with setValidity, ValidityState flags, validation messages and :invalid or :user-invalid styling.
  3. 28 ARIA and ElementInternals for Accessible Components Give custom elements roles, names and states for assistive technology using ElementInternals ARIA properties, default semantics and custom :state() pseudo-classes.
  4. 29 Focus and Keyboard Support in Web Components Make custom elements keyboard friendly with tabindex, delegatesFocus, focus-visible styling and the roving tabindex pattern for toolbars, tabs and menus.

Styling and Theming

  1. 30 CSS Custom Properties for Theming Components Use CSS custom properties as the main theming API for shadow DOM components, with defaults, fallbacks, design tokens, dark mode and a documented list of variables.
  2. 31 Shadow Parts and exportparts Expose chosen internal elements of a shadow tree for outside styling with the part attribute, the ::part() pseudo-element, exportparts and the theme attribute pattern.
  3. 32 Host Selectors and Adopted Stylesheets Style the host and slotted content from inside a shadow root, then share CSS between many instances efficiently with constructable stylesheets and adoptedStyleSheets.

Tooling and Patterns

  1. 33 Lit and Web Component Libraries See what libraries such as Lit add on top of raw custom elements: reactive properties, templates and batched updates. Includes a runnable mini-base-class stand-in.
  2. 34 Testing Web Components Learn what to test in a custom element, how to render it in a fixture, wait for updates, simulate events and test shadow DOM, with a tiny runnable test harness.
  3. 35 Lazy Loading and Distributing Components Load custom elements on demand with customElements.whenDefined, :defined, upgrades and dynamic import, avoid layout shift, and package components for npm and CDNs.
  4. 36 Declarative Shadow DOM and Server Rendering Render shadow DOM straight from HTML with the shadowrootmode attribute so components appear without JavaScript, then hydrate them. Includes server-side output patterns.

Projects and Practice

  1. 37 Project: Toast Notifications Build an accessible toast notification system as a web component with a live region, auto-dismiss, pause on hover, events, themeable parts and a static show() API.
  2. 38 Project: Accessible Tabs Build a tabs component with slots, the tab, tablist and tabpanel ARIA roles, roving tabindex, arrow-key navigation, a change event and a selected property.
  3. 39 Practice Exercises and Challenges Practice custom elements, shadow DOM, slots, events, forms, accessibility and theming with graded exercises, runnable starters, hints and a final capstone challenge.