Custom Elements
ELI5 — The Vibe Check
Custom Elements let you define your own HTML tags that the browser recognizes as first-class citizens. Want a
Real Talk
Custom Elements is a Web Components API for defining new HTML elements. Using customElements.define(), you register a JavaScript class that extends HTMLElement, with lifecycle callbacks for connection, disconnection, and attribute changes. Names must contain a hyphen to avoid conflicts with native elements.
Show Me The Code
class FancyButton extends HTMLElement {
connectedCallback() {
this.innerHTML = `<button class="fancy">${this.getAttribute('label')}</button>`;
}
static get observedAttributes() { return ['label']; }
}
customElements.define('fancy-button', FancyButton);
When You'll Hear This
"Custom Elements give us framework-agnostic components that work in any HTML page" / "The hyphen requirement in custom element names is annoying but prevents clashes"
Related Terms
HTML (HyperText Markup Language)
HTML is like the skeleton of a webpage. You write tags like <h1> and <p> and the browser builds the bones of your site from them.
Lit
Lit is Google's way of saying 'Web Components don't have to be painful.
Shadow DOM
Shadow DOM is like a force field around your component's internals.
Web Components
Web Components are the browser's native component system. Custom elements, shadow DOM, templates - no framework needed.