🐧 Linux TippsDebian 11 Long Term Support reaches end-of-life(31.08.2026 um 02:00 Uhr)
🐧 Linux TippsUpdated Debian 13: 13.7 released(12.09.2026 um 02:00 Uhr)
🕵️ SicherheitslückenUSN-8741-1: Flatpak vulnerabilities(10.09.2026 um 10:44 Uhr)
🕵️ SicherheitslückenUSN-8742-1: Netty vulnerability(10.09.2026 um 11:01 Uhr)
🕵️ SicherheitslückenUSN-8737-2: GNU C Library vulnerabilities(10.09.2026 um 13:25 Uhr)
🕵️ SicherheitslückenUSN-8743-1: PHP vulnerabilities(10.09.2026 um 13:48 Uhr)
🕵️ SicherheitslückenUSN-8744-1: Python vulnerabilities(10.09.2026 um 15:53 Uhr)
🐧 Linux TippsUSN-8748-1: Linux kernel (NVIDIA) vulnerabilities(10.09.2026 um 17:32 Uhr)
🕵️ SicherheitslückenUSN-8745-1: KissFFT vulnerabilities(10.09.2026 um 17:36 Uhr)
🕵️ SicherheitslückenUSN-8746-1: libEBML vulnerability(10.09.2026 um 17:48 Uhr)
🐧 Linux TippsDebian 11 Long Term Support reaches end-of-life(31.08.2026 um 02:00 Uhr)
🐧 Linux TippsUpdated Debian 13: 13.7 released(12.09.2026 um 02:00 Uhr)
🕵️ SicherheitslückenUSN-8741-1: Flatpak vulnerabilities(10.09.2026 um 10:44 Uhr)
🕵️ SicherheitslückenUSN-8742-1: Netty vulnerability(10.09.2026 um 11:01 Uhr)
🕵️ SicherheitslückenUSN-8737-2: GNU C Library vulnerabilities(10.09.2026 um 13:25 Uhr)
🕵️ SicherheitslückenUSN-8743-1: PHP vulnerabilities(10.09.2026 um 13:48 Uhr)
🕵️ SicherheitslückenUSN-8744-1: Python vulnerabilities(10.09.2026 um 15:53 Uhr)
🐧 Linux TippsUSN-8748-1: Linux kernel (NVIDIA) vulnerabilities(10.09.2026 um 17:32 Uhr)
🕵️ SicherheitslückenUSN-8745-1: KissFFT vulnerabilities(10.09.2026 um 17:36 Uhr)
🕵️ SicherheitslückenUSN-8746-1: libEBML vulnerability(10.09.2026 um 17:48 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 5 Min Lesezeit
0

Understanding Shadow DOM: The Secret of Web Component Encapsulation 💯

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Encapsulation plays a key role in building modern web applications, preventing conflicts between different parts.



Imagine you're working on a large-scale project with multiple teams and components, and you don’t want your styles or scripts to interfere with each other.



This is where the Shadow DOM comes into play.






The Shadow DOM is a powerful feature of the Web Components specification, which allows you to encapsulate a part of your HTML and CSS.



This helps you to isolate a section of your page from the rest of the document, creating a more modular and maintainable approach to building web applications.



In this article, we’ll break down the concept of Shadow DOM, explore its benefits, and show you some simple examples.









📌 What is Shadow DOM?



The Shadow DOM creates a "shadow" DOM tree that is separate from the main DOM.



This shadow tree is attached to a host element and can have styles, scripts, and structures that don't interfere with the global page.



Essentially, it allows you to create self-contained components, which can be reused across your application without worrying about external styles or behavior.









📌 How Does It Work?



When you create a shadow tree using JavaScript, the elements inside it are isolated.



They cannot be affected by the styles or scripts outside the shadow tree, and vice versa.



This means you can safely add styles to your shadow DOM without worrying about them leaking out and affecting other parts of the page.



The Shadow DOM has three main modes:




  • Open: The shadow tree is accessible from JavaScript outside the component. You can access it through the shadowRoot property.


  • Closed: The shadow tree is completely encapsulated and cannot be accessed from outside the component. This is a more private and secure option.










📌 Benefits of Using Shadow DOM




  • Encapsulation: Styles and scripts inside the shadow tree won’t affect the rest of the page, and styles from the outside won't break your component.


  • Reusability: You can create reusable components that work consistently across different parts of your application.


  • Cleaner Code: By isolating functionality and styles within a shadow DOM, you can avoid the messiness of global CSS and JavaScript.










📌 Simple Example of Shadow DOM



Let's dive into a simple example to understand how Shadow DOM works.



1. Basic HTML Structure



We will start by creating a simple custom button component using the Shadow DOM.




CODE
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shadow DOM Example</title>
</head>
<body>
<my-button></my-button>

<script src="app.js"></script>
</body>
</html>









2. JavaScript to Create a Shadow DOM



In the app.js file, we will define a custom element <my-button> and create a shadow root for it. We will also apply styles and content inside the shadow tree.




CODE
class MyButton extends HTMLElement {
constructor() {
super(); // Always call super() first
// Attach a shadow root to the element
const shadow = this.attachShadow({mode: 'open'}); // Open mode

// Create a button element
const button = document.createElement('button');
button.textContent = 'Click Me';

// Apply some styles inside the shadow DOM
const style = document.createElement('style');
style.textContent = `
button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
`
;

// Attach the style and button to the shadow DOM
shadow.appendChild(style);
shadow.appendChild(button);
}
}

// Define the custom element
customElements.define('my-button', MyButton);












📍 How This Works




  • We create a custom HTML element called <my-button>.


  • In the JavaScript, we use this.attachShadow({mode: 'open'}) to create the shadow root.


  • We create a <button> element and add some styles inside the shadow DOM.


  • The styles are scoped within the shadow tree, meaning they won’t affect other elements outside of this component.




Now, when you open this HTML file in a browser, you'll see a styled button.



The important part is that the styles applied to this button won’t affect any other buttons or elements on the page.









📌 Shadow DOM with Closed Mode



In case you want the shadow tree to be fully encapsulated and not accessible from the outside, you can use the closed mode:




CODE
const shadow = this.attachShadow({mode: 'closed'}); // Closed mode






In the closed mode, the shadow tree cannot be accessed through JavaScript, making it more private.



This is useful when you want to keep the internal implementation details of the component hidden.












Conclusion ✅



The Shadow DOM revolutionizes the creation of modular and reusable web components.



It prevents conflicts in styles and scripts, enhancing code maintainability and scalability.



With minimal JavaScript, you can build powerful components that remain consistent and isolated from the rest of your application.



As you build more advanced applications and components, you'll find that using the Shadow DOM can significantly improve the structure and organization of your code.



Happy coding!

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Debian 11 Long Term Support reaches end-of-life
1 Quelle
Updated Debian 13: 13.7 released
1 Quelle
USN-8741-1: Flatpak vulnerabilities
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Understanding Shadow DOM: The Secret of Web Component Encapsulation 💯

Thematisch verwandte Begriffe: Understanding, Shadow, Secret, Component · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...