Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

HTML Tutorial

HTML HOME HTML Introduction HTML Editors HTML Basic HTML Elements HTML Attributes HTML Headings HTML Paragraphs HTML Styles HTML Project I HTML Formatting HTML Quotations HTML Comments HTML Colors HTML CSS HTML Project II HTML Links HTML Images HTML Favicon HTML Page Title HTML Tables HTML Lists HTML Project III HTML Block & Inline HTML Div HTML Classes HTML Id HTML Project IV HTML Global Attributes HTML Buttons HTML Iframes HTML JavaScript HTML File Paths HTML Head HTML Layout HTML Responsive HTML Project V HTML Computercode HTML Semantics HTML Project VI HTML Dialog HTML Details HTML Style Guide HTML Entities HTML Symbols HTML Emojis HTML Character Sets HTML Languages HTML URL Encoding HTML Performance HTML SEO

HTML Forms

HTML Forms HTML Form Attributes HTML Form Elements HTML Input Types HTML Input Attributes Input Form Attributes HTML Form Validation HTML Progress and Meter HTML Project VII

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Video HTML Audio HTML YouTube HTML Project VIII

Web Accessibility

Web Accessibility ARIA Attributes Keyboard Focus and tabindex Landmarks Skip Links Color Contrast

Web APIs

Web APIs Geolocation API Drag and Drop API Popover API Web Storage API Web Workers API Server-Sent Events API

HTML Advanced

HTML and the DOM Web Components HTML Security Advanced Forms

HTML Cert

HTML Certificate

HTML Examples

HTML Examples HTML Videos HTML Editor HTML Quiz HTML Exercises HTML Challenges HTML Projects HTML Website HTML Syllabus HTML Study Plan HTML Interview Prep HTML Summary

HTML References

HTML Tag List HTML Attributes HTML Global Attributes HTML Browser Support HTML Event Attributes HTML Colors HTML Canvas HTML Audio/Video HTML Doctypes HTML Character Sets HTML URL Encode HTML Language Codes HTML Country Codes HTTP Status Codes HTTP Request Methods PX to EM Converter Keyboard Shortcuts

HTML Web Components


Share

What are Web Components?

Web Components allows you to create your own reusable, custom elements.

You write the element once, and then use it as often as you want.

Web Components consists of three main technologies:

  • HTML templates
  • Custom elements
  • Shadow DOM

Web components need a little JavaScript, but the examples on this page are short.


HTML Templates

The HTML <template> element is used as a container to hold some HTML content that will be hidden from the user when the page loads.

The content inside <template> can be rendered later with a JavaScript.

You can copy the content with JavaScript and add the copy to the page:

Example

<template id="fruitTemplate">
  <li class="fruit">New fruit</li>
</template>

<ul id="fruits">
  <li>Apple</li>
</ul>

<button onclick="addFruit()">Add a fruit</button>

<script>
function addFruit() {
  const template = document.getElementById("fruitTemplate");
  const copy = template.content.cloneNode(true);
  document.getElementById("fruits").appendChild(copy);
}
</script>
Try it Yourself »

When the page loads, the list shows only Apple.

After two clicks, the list shows:

Apple
New fruit
New fruit

template.content holds the HTML inside the template.

cloneNode(true) makes a copy of it, so you can use the template again and again.

Then you add the copy to the page with the appendChild() function.



Create Custom Elements

A custom element is an element that you create yourself.

Note: The name of a custom element must start with a lowercase letter, and must contain a hyphen (to not conflict with future HTML elements).

This example creates a <hello-box> element that says hello to a name:

Example

<hello-box name="Maria"></hello-box>
<hello-box name="Ali"></hello-box>

<script>
class HelloBox extends HTMLElement {
  connectedCallback() {
    const name = this.getAttribute("name");
    this.innerHTML = "<p>Hello, " + name + "!</p>";
  }
}

customElements.define("hello-box", HelloBox);
</script>
Try it Yourself »

The page shows:

Hello, Maria!
Hello, Ali!

This is what each part does:

  • class HelloBox extends HTMLElement creates a new kind of element
  • connectedCallback() called each time the element is added to the page
  • this is the element itself
  • customElements.define() makes the element available in the page
  • define() holds the name of the element, and its constructor function

Shadow DOM

Normally, the CSS on a page works on all elements on that page.

A shadow DOM is a hidden part inside your custom element, with its own CSS.

The CSS inside the shadow DOM only works inside it.

You create a shadow DOM with attachShadow() function:

Example

<p>This paragraph is outside the shadow DOM.</p>

<red-box></red-box>

<script>
class RedBox extends HTMLElement {
  connectedCallback() {
    const shadow = this.attachShadow({mode: "open"});
    shadow.innerHTML =
      "<style>p { color: red; }</style>" +
      "<p>This paragraph is inside the shadow DOM.</p>";
  }
}

customElements.define("red-box", RedBox);
</script>
Try it Yourself »

Only the paragraph inside the shadow DOM will be red.

The other paragraphs on the page stay black.


Summary

  • Web components let you create your own elements
  • The <template> element holds HTML that is not shown until you copy it
  • A custom element needs a class constructor and customElements.define()
  • A shadow DOM keeps the CSS of a custom element inside the element

HTML Template Tags

Tag Description
<template> Defines a container for content that is hidden when the page loads

Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.