Custom Elements: Create Your Own HTML Tags

Think of Custom Elements as creating your own Lego bricks for the web, defining new HTML tags with custom logic. Use them to build reusable components like a <product-card>. Be wary of extending built-in elements, as Safari does not support this.
Why it exists
Standard HTML has a limited set of tags. For complex UIs, developers often end up with nested divs that lack semantic meaning, like .... Custom Elements were created to solve this, allowing developers to define new, meaningful, and self-contained HTML elements.
The mental model
Think of Custom Elements as creating your own vocabulary for HTML. Instead of assembling generic div and span blocks into a "card," you can define a <product-card> element. The browser then knows exactly what a <product-card> is, including its structure, styles, and interactive behavior, because you've provided the blueprint in JavaScript.
How it works
The process has two main steps. First, you define the element's behavior in a JavaScript class that extends HTMLElement. Inside this class, you can implement lifecycle callbacks—special methods the browser calls automatically when the element is added to the page, removed, or has an attribute change. Second, you register your new element with the browser by calling customElements.define('element-name', YourClass). The name must contain a hyphen to avoid conflicts with standard HTML tags. Once registered, you can use <element-name> directly in your HTML.
When to use it
Use Custom Elements when you need to create reusable UI components that are framework-agnostic. They are perfect for building design systems, widget libraries, or any feature that needs to be encapsulated and easily shared. Because they are a web standard, they work in any major browser and with any (or no) JavaScript framework.
When not to use it
There are two types: autonomous elements (like <my-element>) and customized built-in elements (like ). Customized built-ins, which extend existing HTML tags, are not supported in Safari. For this reason, you should avoid them if you need broad browser compatibility. Stick to autonomous elements for cross-browser projects.
One canonical example
A minimal autonomous element for a pop-up. First, define the class: class PopupInfo extends HTMLElement { constructor() { super(); } }. Then, register it with the browser: customElements.define('popup-info', PopupInfo);. Now, you can use it directly in your HTML: <popup-info></popup-info>. The browser sees this tag, finds its registered class, and creates an instance to manage its behavior.
Interview question
Which type of Custom Element should be avoided for broad browser compatibility?
- a.Customized built-in elementsCorrect
- b.Elements that extend a generic JavaScript class
- c.Elements whose names lack a hyphen
- d.Autonomous elements
Why? this is the answer
The card explicitly states that "Customized built-in elements... are not supported in Safari," making them unsuitable for broad compatibility. Autonomous elements, conversely, are recommended for cross-browser projects.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #web components
- #html
- #javascript
- #dom
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles