Is there a substitute for the /deep selector in CSS shadow dom?

It seems that the /deep selector is no longer an option for selecting shadow DOM children, so I'm in search of an alternative solution.

CSS scoping offers solutions for ascending selectors, but not for descending ones.

Considering this DOM structure:

<script>
    $('.child').addClass('reached');
</script>
<div id="parent">
    #shadow-root
        <div class="child"></div>
    /shadow-root
</div>

What would be the correct selector to target the .child element within the script?

Your assistance is greatly appreciated.

Answer №1

How do I select the .child element using script?

To target an element in the Shadow DOM, you need to utilize the shadowRoot property on the specific element.

var parent = document.querySelector( '#parent' )
var child = parent.shadowRoot.querySelector( '#child' )
child.classList.add( 'reached' )

Note: Make sure the Shadow DOM has been created in open mode.

var sh = parent.attachShadow( { mode: 'open' } )

var parent = document.querySelector( '#parent' )
var sh = parent.attachShadow( { mode: 'open' } )
sh.innerHTML = `<style>
                    div.reached { color: green }
                </style>
                <div id="child">Child</div>
                `
var child = parent.shadowRoot.querySelector( '#child' )
child.classList.add( 'reached' )
<div id="parent">
</div>

Note: The use of ::slotted is only necessary when elements from the light DOM are exposed with <slot>.


Are there alternatives to the /deep selector?

In short, no. As the main purpose of the Shadow DOM is to isolate it from the main page, the /deep was considered somewhat controversial.

A very recent proposal, involving ::part and ::theme pseudo-elements may offer some level of control, but implementation might take some time.

Until then, a common workaround is to make use of CSS custom properties.

It's important to note that both solutions must be implemented by the Web Component designer and cannot be easily overridden.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I properly integrate jQuery libraries, along with CSS, in a create-react-app project?

I'm currently working on incorporating the select2 jquery library into my React application, which was created using create-react-app. After adding jquery and select2 to my package.json file, I was able to get the javascript functionality to work pro ...

Ensure the left column extends all the way down

I've been struggling with this issue for almost three days now. After reading numerous articles on creating a three-column layout and experimenting with absolute and relative positioning, I'm still not able to achieve the desired result. What I& ...

5 Ways to Incorporate Font Awesome Icons into CSS Pseudo Elements

I'm attempting to incorporate FontAwesome icons into CSS pseudo elements. When I add the icon using HTML in the traditional manner, it works fine. However, I'm trying to integrate the fonts into a bootstrap accordion so that the icon changes whe ...

Guidance on marking a menu item as selected when choosing its sub-item

I've been tasked with creating a menu and I want to ensure that the menu item stays selected when its subchild is selected. Below is a snippet from my CSS file: #wrapper { width:100%; height:500px; } h2 { color:#787878; } // more ...

Shifting a division using insertAfter

Hey there! I'm having a bit of trouble using the insertAfter function. In each product in my store, I need to position an "add to cart" button after the price. This is the code I tried: <Script type="text/javascript" > jQuery(document).read ...

Issues with navigating sliders

I've encountered a problem with my slider arrows while following a YouTube tutorial. They are supposed to appear next to the picture and testimonial, but instead, they are showing up at the top. This issue wasn't present initially, but after enco ...

Is it time to delete a bootstrap attribute?

I have implemented a bootstrap class for ul/li items that adds a border around an li item when clicked. However, I am facing an issue where only the first list item is highlighted on all 4 sides when clicked, while the rest of the list items only have 3 ...

ensuring that the brief description on my thumbnail is consistently centered

I am facing an issue with aligning text consistently in the middle of thumbnails on my website. Regardless of whether the text spans 1, 2, or 3 lines, I want it to always be positioned inline with the center of the thumbnail. I have tried various methods s ...

Obtain the bootstrap CSS file

In all my various projects, I have been utilizing Bootstrap 3 and now I am interested in incorporating Bootstrap 4 cards into my work. However, I am struggling to locate the cards in a css format rather than the scss format. I personally don't think ...

Could it be that grid-area does not function properly with the attr function, or is this intentional?

The following examples showcase the functionality: It's impressive how I'm utilizing content: attr(class) to streamline the labeling process. Impressive! section { outline: 1px solid red; display: grid; grid-gap: 10px; grid-template-a ...

What is the proper location to insert CSS within Laravel?

What is the recommended location for storing CSS or JS files in Laravel? More specifically, what are the differences between placing CSS files in /public versus /resources/css? Which directory is preferable for storing my CSS files? ...

How to align text to the left in Bootstrap 4 with equal column widths reminiscent of a

Below is a snippet of code that creates a list of links: <h3>Hours</h3> <div class="hours-display pb-2"> <div class="mb-0"><span class="mr-2"><strong>Mon</strong></span><span class="mr-1">9:30</sp ...

How can I center align my loader inside app-root in Angular2+?

I've successfully added a basic spinner to my <app-root> in the index.html file. This gives the appearance that something is happening behind the scenes while waiting for my app to fully load, rather than showing a blank white page. However, I& ...

The website appears to be experiencing technical difficulties as the complete content

Check out this website: The full page content is not displaying properly. The footer content seems to be hidden and only appears after refreshing multiple times. Can anyone diagnose if there might be an issue with the CSS or JavaScript? ...

What could be causing my cross fade to not repeat as intended?

I created a basic background image cross fader using the code found at http://jsfiddle.net/jRDkm/2/. This code was inspired by . However, I'm encountering an issue where the slideshow only repeats once before fading to white. How can I modify the cod ...

The elements in the list are too large for the designated area on Internet Explorer and Chrome browsers on Mac computers

I am facing an issue with my navigation menu on Internet Explorer and Chrome on MAC. The last element of the list is not fitting into the given width (which is fixed at 1000px). It works fine on Firefox, Opera, and Windows Chrome. I cannot test Safari at t ...

Is there a way to deactivate option C if both A and B are chosen from the same dropdown menu?

I have set up a multiple drop down menu. Is there a way to deactivate option "C" when both "A" and "B" are chosen at the same time? Also, how can I add a button that resets the selected value to "-Gender-" and reload the default chart? It would be greatl ...

Shifting Icon to the Right within the Drawer Navigator Toolbar

While modifying the example code for Material UI's drawer navigator, I decided to enhance it by adding a notification icon and a checkout icon with the Admin Panel typography in the toolbar. However, I encountered an issue where the checkout icon app ...

What is causing my table to refuse to center?

I'm having trouble keeping my table of images contained within the content area of my page. Additionally, I can't seem to get my footer to stay at the bottom of the page. Take a look at this image for reference: body { margin: 0px; padding ...

How to modify the floating label color using Bootstrap 5

I'm currently utilizing bootstrap 5 to craft a form featuring floating input functionality. Below is an example of the input I am utilizing: <div class="form-floating mx-auto mb-3 w-25"> <input type="text" class=" ...