Shadow DOM in HTML5

When developing UI using HTML and JavaScript, as it happens when using jQuery libraries (and similar), the internal implementation of the widgets we draw on screen is not encapsulated from the rest of the page. This lack of encapsulation means that whenever the page CSS or the page JavaScript change it might influence our UI. The lack of encapsulation risks future upgrades for our library that include changes in the internal details of the widgets.

The Shadow DOM specification addresses this encapsulation problem. When using the Shadow DOM, elements can get a new kind of node associated with them. This new kind of node is called a shadow root. An element that has a shadow root associated with is called a shadow host. The content of a shadow host isn’t rendered. The content of the shadow root is rendered instead.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Shadow DOM Sample</title>
</head>
<body>
<bt>Hello, world!</bt>
<script>
    var host = document.querySelector('bt');
    var root = host.createShadowRoot();
    root.textContent = "שלום";
</script>
</body>
</html>

The following video clip shows the execution of this code sample and explains it. The use of Shadow DOM is popular in frameworks such as AngularJS.

If you try to write code in JavaScript that gets the content of the element it won’t be the text in hebrew. It will be the text written in between the elements in the HTML file.

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences