--- url: "https://web.dev/articles/customelements" titulek: "Working with Custom Elements | Articles | web.dev" h1: "Working with Custom Elements Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/customelements" drobecky: ["Articles", "Working with Custom Elements"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:04:19" znaku: 18382 slov: 2633 kodovani: "iso-8859-2" sha256: "86b1b9391e5d6274ff810424c000f91975151ffd0e172e0841faf09df14462a4" --- Working with Custom Elements Stay organized with collections Save and categorize content based on your preferences. Boris Smus Introduction The web severely lacks expression. To see what I mean, take a peek at a "modern" web app like GMail: There's nothing modern about `
` soup. And yet, this is how we build web apps. It's sad. Shouldn't we demand more from our platform? Sexy markup. Let's make it a thing HTML gives us an excellent tool for structuring a document but its vocabulary is limited to elements the [HTML standard](http://www.whatwg.org/specs/web-apps/current-work/multipage/) defines. What if the markup for GMail wasn't atrocious? What if it was beautiful: `

Feelin' this Web Components thing.

Heard of it? ... ` How refreshing! This app totally makes sense too. It's **meaningful**, **easy to understand**, and best of all, it's **maintainable**. Future me/you will know exactly what it does just by examining its declarative backbone. Getting started [Custom Elements](https://html.spec.whatwg.org/multipage/custom-elements.html) **allow web developers to define new types of HTML elements**. The spec is one of several new API primitives landing under the [Web Components](https://www.w3.org/TR/2012/WD-components-intro-20120522/) umbrella, but it's quite possibly the most important. Web Components don't exist without the features unlocked by custom elements: Define new HTML/DOM elements Create elements that extend from other elements Logically bundle together custom functionality into a single tag Extend the API of existing DOM elements Registering new elements Custom elements are created using `document.registerElement()`: `var XFoo = document.registerElement('x-foo'); document.body.appendChild(new XFoo());` The first argument to `document.registerElement()` is the element's tag name. The name **must contain a dash (-)**. So for example, ``, ``, and `` are all valid names, while `` and `` are not. This restriction allows the parser to distinguish custom elements from regular elements but also ensures forward compatibility when new tags are added to HTML. The second argument is an (optional) object describing the element's `prototype`. This is the place to add custom functionality (e.g. public properties and methods) to your elements. More on that later. By default, custom elements inherit from `HTMLElement`. Thus, the previous example is equivalent to: `var XFoo = document.registerElement('x-foo', { prototype: Object.create(HTMLElement.prototype) });` A call to `document.registerElement('x-foo')` teaches the browser about the new element, and returns a constructor that you can use to create instances of ``. Alternatively, you can use the other techniques of instantiating elements if you don't want to use the constructor. Extending elements Custom elements allows you to extend existing (native) HTML elements as well as other custom elements. To extend an element, you need to pass `registerElement()` the name and `prototype` of the element to inherit from. Extending native elements Say you aren't happy with Regular Joe `