Suppose I have a html element with a shadow root.
<my-element> #shadow-root <div class='need-target-this' /> </my-element>
How to position the div inside the Shadow root?
I tried using
:host(my-element.need-target-this)
But that doesn't help. What am I missing here?
P粉5210131232024-03-31 10:24:38
Use the tag within shadowDOM to set the style of shadowDOM
See also::part: https://developer.mozilla.org/en-US/docs/Web/CSS/::part
See also: : :slotted CSS selector for nested children in ShadowDOM slots
customElements.define("my-element",class extends HTMLElement{ constructor(){ super().attachShadow({mode:"open"}).innerHTML = ``; } connectedCallback(){ this.shadowRoot.querySelector("span").innerHTML = `Web Component!`; } });
Hello
P粉2327937652024-03-31 00:21:30
In case it helps someone: I wrapped my element with div
, added ref
and went to
const Shadow = ref.current.querySelector('my-element').shadowRoot
const target = Shadow?.querySelector('.need-target-this')
target.style.whatever = 'value';