A custom element is a developer-defined HTML element registered with the browser. It gives a reusable UI component its own tag, so a page can use concise, meaningful markup instead of repeating the same HTML, CSS, and JavaScript wherever that UI appears. Custom elements work in applications with or without a web framework. section 1.3 section 1.2
A radio group in one element
`<radio-group name="color" labels="Red,Green,Blue" values="red,green,blue" value="red"></radio-group>` can represent three labeled radio buttons. Without the component, each use needs separate radio inputs, labels, and surrounding markup. The attributes supply the choices and initial selection; the element handles rendering them. section 1.1
To define a vanilla custom element, create a JavaScript class extending `HTMLElement`, then register its tag with `customElements.define("hello-world", HelloWorld)`. The browser uses that association when it encounters `<hello-world></hello-world>`. A custom element name must be lowercase and contain a hyphen, distinguishing it from built-in tags. Even an empty custom element needs a closing tag; it cannot be self-closing. section 1.3 section 3.1
Listing 1.4 hello-world.js
class HelloWorld extends HTMLElement { #1
constructor() { #2
super();
this.attachShadow({ mode: "open" });
}
connectedCallback() { #3
const name = this.getAttribute("name") ?? "World";
this.shadowRoot.innerHTML = `
<style>
button {
color: blue;
}
</style>
<button type="button">Hello, ${name}!</button>
`;
this.addEventListener("click", () => {
const button = this.shadowRoot.querySelector("button");
const { style } = button;
style.color = style.color === "red" ? "blue" : "red";
});
}
}
customElements.define("hello-world", HelloWorld); #4
The complete `hello-world` example shows registration, a `name` attribute with a default, a lifecycle callback, shadow DOM markup and styling, and a click handler that changes button color. section 1.3
Custom elements can bundle their rendering, styling, and behavior, while shadow DOM can help protect internal structure and styles from unintended outside changes. The browser APIs need no helper library, though libraries can reduce boilerplate and ease rendering and state management. For reliable integration, design elements to respond correctly to attribute and property changes and to dispatch events appropriately. section 1.8 section 3.0 section 4.0
Framework use may need a little configuration. In Vue, custom elements can be imported and used in templates, with refs for property updates and event bindings for custom events. Configure Vue to recognize their tags; otherwise it may warn that it cannot resolve them as components. section 9.7



