search

Home  >  Q&A  >  body text

How to target a class in shadow root without JS?

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粉610028841P粉610028841233 days ago416

reply all(2)I'll reply

  • P粉521013123

    P粉5210131232024-03-31 10:24:38

    • Use the `; } connectedCallback(){ this.shadowRoot.querySelector("span").innerHTML = `Web Component!`; } });

      
      
      Hello
      
      

      reply
      0
  • P粉232793765

    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';

    reply
    0
  • Cancelreply