Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. JavaScript Course

Lesson 33 of 55 · javascript

Web Components – Custom Elements, Shadow DOM & Templates

Duration: 13 mins

Custom elements let you define new HTML tags.

class MyButton extends HTMLElement {
  constructor(){
    super();
    const tmpl = document.getElementById('tpl').content;
    this.attachShadow({mode:'open'}).appendChild(tmpl.cloneNode(true));
  }
}
customElements.define('my-button', MyButton);

Shadow DOM encapsulates markup and styles.

<template id="tpl">
  <style>button{background:#0066cc;color:#fff;padding:0.5rem;}</style>
  <button><slot></slot></button>
</template>

Use <my-button> like any native element.

Example usage

<my-button>Click me</my-button>
Previous: Progressive Enhancement & Graceful DegradationNext: Transpiling with Babel – ES6 → ES5