Use a remote stylesheet inside a template tag (with shadow dom)

后端 未结 4 741
生来不讨喜
生来不讨喜 2021-01-01 22:13

I am trying to make a semi-resuseable widget but I am running into a problem. I am trying to encapsulate a some CSS code inside a shadow root so that it does not affect the

4条回答
  •  青春惊慌失措
    2021-01-01 22:29

    I added the stylesheet's link element directly to the shadow root this way:

        let link = document.createElement('link');
        link.setAttribute('rel', 'stylesheet');
        link.setAttribute('href', 'whatever.css');
        this.shadowRoot.appendChild(link);
    

    It seems to work fine. (I called this from the constructor of the component.)

提交回复
热议问题