Web Components Community Group Logo Web Components Community Group at W3C About Articles Toggle darkmode

Specifications

Web components is a meta-specification made possible by four other specifications:

  • The Custom Elements specification
  • The shadow DOM specification
  • The HTML Template specification
  • The ES Module specification

These four specifications can be used on their own but combined allow developers to define their own tags (custom element), whose styles are encapsulated and isolated (shadow dom), that can be restamped many times (template), and have a consistent way of being integrated into applications (es module).

The Custom Elements specification

This section applies to the cross-browser version of the Custom Elements specification (v1). See Eric Bidelman’s articles on Custom Elements v1.

The Custom Elements specification lays the foundation for designing and using new types of DOM elements that are fully-featured and conforming. Following the Custom Elements spec, authors can define behaviors and styles for new HTML elements.

  • Autonomous custom elements are new HTML tags, defined entirely by the author. They have none of the semantics of existing HTML elements, so all behaviors need to be defined by the author.
  • Customized built-ins extend existing HTML elements with custom functionality. They inherit semantics from the elements they extend. The specification for customized built-ins is still a work in progress, and at present is only supported by Chrome.

Create a custom button as an autonomous custom element

To create an autonomous custom element, extend HTMLElement. The Custom Elements syntax is:

class AutonomousButton extends HTMLElement {
  ...
}
customElements.define("autonomous-button", AutonomousButton);

To use the element:

<autonomous-button>Click Me :)</autonomous-button>

When the browser sees the tag, it constructs and renders a new instance of the AutonomousButton class. However, this element will not behave like an HTML