MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Shadow DOM & Web Components Basics0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
6.1Shadow DOM & Web Components Basics6.2Custom Elements Lifecycle (connectedCallback)6.3Virtual DOM Concepts & Reconciliation6.4MutationObserver API6.5ResizeObserver & IntersectionObserver APIs
HomeTutorialsJavaScript for AdvancedShadow DOM & Web Components Basics
Custom Elements Lifecycle (connectedCallback)

Shadow DOM & Web Components Basics

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

A long-standing challenge in web development is CSS encapsulation: a global style rule like p { color: red } or button { padding: 10px } easily leaks into third-party widgets, breaking layouts. Web Components and the Shadow DOM solve this by providing native, browser-level component encapsulation for markup and styles.


1. The 3 Pillars of Web Components

  1. 1
    Custom Elements: APIs to define new HTML tags (e.g. <user-card>, <video-player>).
  2. 2
    Shadow DOM: An encapsulated DOM subtree isolated from the main document's styles and scripts.
  3. 3
    HTML Templates (<template> & <slot>): Reusable markup fragments rendered dynamically.

2. Light DOM vs. Shadow DOM

1 Light DOM (Regular Page DOM)
2 ├── <h1>Global Heading</h1> (Styled by global.css)
3 └── <user-badge> ───────────┐
4 ▼
5 Shadow Root (#shadow-root)
6 ├── <style>h1 { color: blue; }</style> <── SCOPED!
7 └── <h1>Component Heading</h1> <── Isolated!
  • Light DOM: The standard DOM tree. Global CSS and scripts apply freely.
  • Shadow Root: The boundary attached to an element. Styles inside cannot leak out, and global styles cannot penetrate in!

3. Creating a Shadow Root: attachShadow()

You attach a shadow root to any standard or custom element using attachShadow({ mode: 'open' | 'closed' }):

1<div id="host-element"></div>
2 
3<script>
4 const host = document.querySelector('#host-element');
5
6 // Attach open shadow root
7 const shadow = host.attachShadow({ mode: 'open' });
8 
9 // Add encapsulated styles and markup
10 shadow.innerHTML = `
11 <style>
12 /* These styles affect ONLY elements inside this Shadow DOM! */
13 h2 {
14 color: #4f46e5;
15 font-family: sans-serif;
16 border-bottom: 2px solid #4f46e5;
17 }
18 p { color: #64748b; }
19 </style>
20 <h2>Encapsulated Widget</h2>
21 <p>Global CSS styles cannot override this text!</p>
22 `;
23</script>

4. open vs. closed Mode

  • mode: 'open' (Recommended): Accessible from JavaScript via element.shadowRoot. Enables testing and developer inspection.
  • mode: 'closed': element.shadowRoot returns null. Rarely used because it offers no true security and hinders testing.

5. Composition with <slot>

The <slot> element acts as a placeholder for content projected from the parent Light DOM into the Shadow DOM:

1<fancy-card>
2 <!-- Light DOM Content -->
3 <span slot="title">Enterprise Security</span>
4 <p slot="body">Native isolation powered by W3C standards.</p>
5</fancy-card>

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What problem does the Shadow DOM primarily solve in web development?
Question 2
What method is used to create and attach a Shadow DOM to an element?
Question 3
What is the difference between mode: 'open' and mode: 'closed' when attaching a shadow root?
Question 4
What HTML element serves as an insertion point for projecting Light DOM content into a Shadow DOM component?
Question 5
Can global page styles like body { font-family: Arial; } penetrate and style elements inside an open Shadow DOM?
Next Lesson

Custom Elements Lifecycle (connectedCallback)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Defensive Programming & InvariantsCustom Elements Lifecycle (connectedCallback)

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext