Custom Elements
In one line
Custom Elements is a browser API for registering new HTML element types with custom behavior via a JavaScript class.
In simple words
Custom Elements is the API that lets you define brand-new HTML tags. You write a class extending HTMLElement (or a specific built-in like HTMLButtonElement), register it with customElements.define('my-tag', MyClass), and then use <my-tag> anywhere in your HTML — it gets instantiated and your class's lifecycle methods run.
The lifecycle callbacks are connectedCallback (added to DOM), disconnectedCallback (removed), attributeChangedCallback (an observed attribute changed), and adoptedCallback (moved to a new document). These mirror React's mount/unmount/update lifecycle but at the platform level.
Custom Elements can be autonomous (a brand new tag) or customized built-in (extending an existing tag like <button is="my-button">). Combined with Shadow DOM and templates, they form the Web Components standard for framework-free reusable UI.
Example
class CountdownTimer extends HTMLElement {
static observedAttributes = ["seconds"];
connectedCallback() {
let n = Number(this.getAttribute("seconds"));
this.render = () => { this.textContent = `${n}s`; };
this.render();
this.timer = setInterval(() => { if (--n <= 0) clearInterval(this.timer); this.render(); }, 1000);
}
disconnectedCallback() { clearInterval(this.timer); }
}
customElements.define("countdown-timer", CountdownTimer);
// <countdown-timer seconds="10"></countdown-timer>The class defines lifecycle callbacks. connectedCallback starts the interval; disconnectedCallback cleans it up. The tag behaves like a built-in element.
Related terms
Related Resources
Related Lessons
- Web Components
Custom Elements define new tags.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.