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
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Web Components – Custom Elements, | JavaScript Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. JavaScript Course

Lesson 33 of 55

·javascript

Web Components – Custom Elements, Shadow DOM & Templates

13 mins

Course progress60%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Shadow DOM creates an isolated style scope – external CSS cannot bleed in.
  • Use **`observedAttributes`** and `attributeChangedCallback` to react to attribute changes.
  • Web components are **framework‑agnostic** and can be used in React, Vue, etc.

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>
Make it stickPractice what you've learned1 quiz · 7 practical tests · Optional activities from JavaScript CourseBrowse activities
Previous lessonProgressive Enhancement & Graceful DegradationContinue to lesson 34Transpiling with Babel – ES6 → ES5Back to JavaScript Course curriculum