Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 8949

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T05:53:07+00:00 2024-11-28T05:53:07+00:00

What is the Shadow DOM

  • 60k

Missing style encapsulation

As a web developer, you've probably dealt with the headaches of missing style encapsulation. A single broad CSS selector can unexpectedly mess up the UI somewhere else on your site.

Adding third-party UI libraries only complicates things. Do their CSS class names clash with yours? Will their buttons break yours? Maybe.

The partial solution

Frameworks like Angular or Vue.js offer some form of component-based style encapsulation. Others, like React, let you choose from various style encapsulation libraries, such as styled-components.[1]

However, their solutions typically offer only pseudo style encapsulation. Instead of fully isolating styles, they often just add prefixes or suffixes to CSS class names following some patterns that hopefully prevent name clashes.

Doesn't this feel like a workaround? It does to me.

Meme: Fixing broken water tank with flex tape

Shadow DOM to the rescue

The shadow DOM attempts to fix the issue of missing encapsulation.[2] It allows you to attach additional subtrees to the main DOM. With this API, you take an HTML node from your DOM and you simply attach a new subtree to it. We call the root node of this new subtree the shadow root. The neat part: styles don't cross this shadow root.

Meme: Gandalf shouting

This means, the styles of your subtree don't leak out into the main DOM. Also, the styles of your main DOM don't affect the encapsulated content of the shadow DOM. Moreover, the same encapsulation also applies to browser events. An onclick event originating from the shadow DOM will not bubble up to the root of the main DOM. Instead, it will end at the shadow root.

How the shadow DOM works

There are two different ways how you can set up the shadow DOM. If you want to understand the concept better, take a look at the imperative API. Otherwise, feel free to jump ahead to the declarative API since that one is usually preferred by most developers nowadays.

Imperative API

Heads up: the imperative API does not work for server-side rendering (SSR). If you need SSR, use the declarative approach instead.

Let's assume, we have a very basic HTML structure:

<body>   <div><span>OUTSIDE the shadow DOM</span></div>   <div id="host"></div> </body> 
Enter fullscreen mode Exit fullscreen mode

Let's go over this. Our DOM has a <body> element that contains two HTML nodes:

  • a <div> containing a <span> with the text OUTSIDE the shadow DOM,
  • a <div> with the ID host. This will be our host element to which we'll attach the shadow DOM.

Diagram of the initial HTML structure

Next, we get the reference to this <div> element and attach a shadow DOM to it. Then, for the sake of demonstration, we create another <span> and append it to the shadow DOM:

// get the reference to the div const hostElement = document.querySelector("#host");  // attach a shadow DOM const shadowDom = hostElement.attachShadow({ mode: "open" });  // create a new HTML node (just for demonstration) const exampleElement = document.createElement("span"); exampleElement.textContent = "INSIDE the shadow DOM";  // append the new HTML node to the shadow DOM shadowDom.appendChild(exampleElement); 
Enter fullscreen mode Exit fullscreen mode

Now, the new structure looks like this:

Diagram of the HTML structure with added shadow DOM

If you inspect how this is rendered in the browser, you will see something like this:

<body>   <div><span>OUTSIDE the shadow DOM</span></div>   <div id="host">     #shadow-root (open)       <span>INSIDE the shadow DOM</span>       </div> </body> 
Enter fullscreen mode Exit fullscreen mode

You can see all these steps together in this short animated diagram:

Your browser does not support the video tag.

Declarative API

Heads up: the declarative shadow DOM has only recently been implemented in the last remaining browsers and is not fully adopted yet.[3] If you use the declarative API, add the required polyfill to your application.[4]

With the declarative shadow DOM API, you can do the same that we did in the imperative example above in plain HTML:

<body>     <span>Outside the shadow DOM</span>     <div id="host">       <template shadowrootmode="open">         <span>Inside the shadow DOM</span>       </template>     </div> </body> 
Enter fullscreen mode Exit fullscreen mode

Notice that we used the <template> element here. The reason for this is that the <template> element has its own mechanism for rendering its content. If you want to learn more about it, check out my template API article.

References

  1. Library: styled-components
  2. MDN Docs: Using shadow DOM
  3. Can I Use: Declarative shadow DOM
  4. web.dev: Declarative shadow DOM polyfill

javascriptwebcomponentswebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.