Aryan PrajapatKnowledge Contributor
How do you create custom HTML element?
How do you create custom HTML element?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
The creation of custom HTML elements involves two main steps,
Define your custom HTML element: First you need to define some custom class by extending HTMLElement class. After that define your component properties (styles,text etc) using connectedCallback method. Note: The browser exposes a function called customElements.define inorder to reuse the element.
class CustomElement extends HTMLElement {
connectedCallback() {
this.innerHTML = “This is a custom element”;
}
}
customElements.define(“custom-element”, CustomElement);
Use custom element just like other HTML element: Declare your custom element as a HTML tag.