Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App
Next Article:
What is shadow root and how to use it ?
Next article icon

What is shadow root and how to use it ?

Last Updated : 09 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A shadow root is the root node of a shadow DOM, which is a hidden DOM subtree attached to an element in the main DOM. This allows for the encapsulation of HTML, CSS, and JavaScript, enabling the creation of self-contained components.

  • Encapsulation: Isolates component styles and behavior from the global document, preventing unintended interactions.
  • Reusability: Facilitates the development of modular components that can be reused across different parts of an application.
  • Maintainability: Simplifies code management by keeping component-specific code separate from the main document.

Prerequisites for Understanding Shadow DOM

Before diving into Shadow DOM, it’s essential to have a solid understanding of the basics of the DOM, DOM tree, and Web Components. Check out these helpful resources:

  • What is DOM? - Learn about the structure and API for interacting with web documents.
  • Understanding DOM Tree - Explore how browsers create a tree-like structure for HTML documents.
  • Introduction to Web Components - Discover how to build reusable and encapsulated custom elements.

Shadow DOM in Web Components

The Shadow DOM is a key feature of Web Components that enables developers to encapsulate a component's internal structure and styling. This ensures that styles and scripts do not leak out or get affected by the rest of the page.

Key Concepts of Shadow DOM

  • Shadow Host: The element to which the shadow DOM is attached.
  • Shadow Tree: The DOM tree inside the shadow DOM.
  • Shadow Boundary: The boundary between the main DOM and shadow DOM.
  • Shadow Root: The root node of the shadow tree.
HTML DOM

Creating a Shadow DOM:

To create a shadow DOM, you can use the attachShadow() method on a DOM element. This method takes an object with a single property, mode, which can be set to either 'open' or 'closed'. An open Shadow Root can be accessed using JavaScript, while a closed one cannot.

HTML
<!--Driver Code Starts--> <div id="shadow-host"></div>  <!--Driver Code Ends-->  <script>   const shadowHost = document.getElementById('shadow-host');   const shadowRoot = shadowHost.attachShadow({ mode: 'open' });    const paragraph = document.createElement('p');   paragraph.textContent = 'This is inside the shadow DOM';   shadowRoot.appendChild(paragraph); </script>  


In this example:

  • A shadow root is attached to the <div> with the ID shadow-host.
  • A <p> element is created and its text content is set.
  • The paragraph is appended to the shadow root, encapsulating it within the shadow DOM.

Best Practices for Using Shadow DOM

  • Encapsulate Styles Within the Shadow DOM: Always define component-specific styles within the shadow root to avoid relying on global styles.
  • Use the mode Property Wisely: When attaching a shadow root, choose between 'open' and 'closed' modes based on your need for external access. An open shadow root can be accessed via JavaScript, while a closed one cannot.
  • Leverage Slots for Content Distribution: Utilize <slot> elements to allow users to insert content into your component, enhancing its flexibility and reusability.

Next Article
What is shadow root and how to use it ?

N

NehaMali
Improve
Article Tags :
  • Web Technologies
  • HTML
  • HTML-DOM
  • HTML-Misc

Similar Reads

    How to Add Shadow to Image in CSS ?
    Adding shadow to an image can enhance its visual appeal and make it stand out on your webpage. In this article, we will explore various approaches to add shadow to an image using CSS. Table of Content Add Shadow on Image using box-shadow PropertyAdd Shadow on Image on HoverAdd Shadow on Image using
    2 min read
    How to Set the Inset Shadow Using CSS ?
    In CSS, setting an inset shadow involves creating an inner shadow effect within an element's boundaries. Unlike traditional shadows that appear outside the element, an inset shadow gives a recessed or sunken look, enhancing the element's visual depth and appearance.Note: By default, the shadow gener
    2 min read
    How to Create Card with Box Shadow in React Native ?
    Creating Card with box shadow in react native makes it stand out within the interface and display information in an appealing way. It will be done in React Native by using the expo cli. Expo simplifies cross-platform app development by providing a unified codebase for iOS, Android, and the web. With
    4 min read
    How to Add Shadow in CSS?
    Shadows in CSS can be used to create depth and visual interest by adding the effect that simulates the shadow of an element. It can be applied to both the text and block elements to enhance the design of the web pages. CSS can provide several properties to achieve different shadow effects, including
    4 min read
    How to set inner text shadow with CSS ?
    CSS is a style sheet language that describes the document's presentation written with HTML or similar markup languages. In this tutorial, we are going to learn how to set inner text shadows with CSS. Approach: Text shadow is used to design the text elements such as paragraphs, headings, etc. We firs
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences