Understanding the Shadow DOM

Understanding the Shadow DOM

In this article we will understand what is a Shadow DOM and how, when and why to use a Shadow DOM.

This is a basic introduction to what is a Shadow DOM and how ,when and why to use a Shadow DOM.

What is Shadow DOM?

In simple words, it’s a DOM inside a DOM.

Using Shadow Dom, you can attach a separate DOM tree which is hidden and encapsulated to any element. This is very helpful to achieve encapsulation in web components.

Understanding the Shadow DOM

How to use Shadow DOM

Shadow root: the root node of the shadow tree that is attached to the DOM element.

Create a shadow root:

Attaching shadow to a custom component in its constructor — most common use case

A custom component will work best when its encapsulated and abstracted. Shadow DOM helps to achieve that. We will look into it in a while.

var shadow = this.attachShadow({mode: 'open'});

Attaching shadow dom to an element in the DOM tree

In this case directly access the element of a DOM tree and attach the shadow DOM

var shadow = element.attachShadow({mode: 'open'});

The mode property of Shadow dom can be either ‘open’ or ‘closed’.

It specifies whether or not the shadow root’s internal features are accessible from JavaScript.

So if mode=closed, element.shadowRoot will return null.

Create the shadow dom structure:

var parent = document.createElement('div');
var img = document.createElement('img');
icon.appendChild(img);

Attach the shadow dom to the shadow root:

shadow.appendChild(parent);

Why a Shadow DOM?

As stated earlier it helps in abstracting the shadow tree from the actual tree.

Let’s see an example:

We have an HTML file with a div #divInDom and a ‘p’ tag

Understanding the Shadow DOM

Now we attach a shadow room to the div #div

Understanding the Shadow DOM

We have added a p tag to the shadow DOM which now looks like:

Understanding the Shadow DOM

The output:

Understanding the Shadow DOM

Now let’s try see the Shadow DOM in action ✨

Understanding the Shadow DOM

We add a style for ‘p’ tag, if you remember from above we have 2 ‘p’ tags now — one as part of the DOM and one as part of the Shadow DOM.

Let’s see what happens in the output:

Understanding the Shadow DOM

As you saw the styling did not affect Shadow DOM element.

The reverse is also true, the styles defined within a shadow DOM do not affect the root DOM.

Shadow DOM is nothing recent— browsers have used it for a long time to encapsulate the inner structure of an element. video tag is good example of this.

All you see in the DOM is the video element, but it contains a series of buttons and other controls inside its shadow DOM. The shadow DOM spec has made it so that you are allowed to actually manipulate the shadow DOM of your own custom elements.

web-development reactjs

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Web Developer

Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

ReactJS Web App Development Services

We provide top-notch ReactJS development services to global clients. Hire expert ReactJS developers from top React JS development company, Skenix Infotech.

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.