Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding CSS of div inside a #shadow-root

We are using a third party plug-in and I'm trying to override the CSS of one div in particular. The problem is that I don't understand how you are supposed to work with #shadow-root:

Shadow-root code representation in Devtools DOM inspector shows sc-choice, the host element the #shadow-root (open) that contains div with "part" attribute set to "base" value, and in it there are two slot elements with different names and one label element in between, with "part" attribute set to "content".

I have tried using regular CSS to style to override the .choice--size-small div styling but its not working

div.choice--size-small {
  display: none !important;
}

My understanding is that #shadow-root purpose is to isolate everything that is inside so it doesn't get affected by other CSS, so it makes sense.

Can it be done? what is the best way to achieve this? Keep in mind this is a third party software and we can only us CSS and JS to achieve this.

like image 327
JPD Avatar asked Oct 26 '25 08:10

JPD


1 Answers

That 3rd party has provided "part" attributes

But only on the <div part="base"> and <label part="content"> elements

You can use ::part to style the inside of the shadowRoot, without using JavaScript.
But only the nodes the part attribute is set on!

See: https://developer.mozilla.org/en-US/docs/Web/CSS/::part

so your (global) CSS can only target that one specific part:

sc-choice::part(base) {
  display: none;
}

When an open shadowRoot has no part definitions. You can "get in" with:

document
  .querySelector("sc-choice")
  .shadowRoot
  .append(
           Object.assign(
                          document
                            .createElement("STYLE") , 
                             {
                               innerText : `div.choice--size-small {
                                              display:none
                                            }`
                              })
)
like image 106
Danny '365CSI' Engelman Avatar answered Oct 28 '25 21:10

Danny '365CSI' Engelman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!