4

the following code is a view from chrome dev tools

<textarea>
   #shadow-root (user-agent)
     <p> This I want to restyle </p>
 <textarea>

what CSS selector I have to use if I would like to restyle element in shadow DOM ?

thank you

Pavel Franta
  • 1,273
  • 2
  • 11
  • 24
  • 1
    Get deep into that; https://gist.github.com/praveenpuglia/0832da687ed5a5d7a0907046c9ef1813, ...but answering your question it's impossible to style it with any selector. – Jakub Chlebowicz Nov 22 '18 at 12:47
  • 2
    You cannot access the content of a user-agent Shadow DOM. https://stackoverflow.com/a/38736220/4600982 – Supersharp Nov 22 '18 at 13:44
  • @connexo :) actually in my case textarea has two divs, one for placeholder and second for the content itself. To my surprise, I could restyle placeholder directly from CSS with this ::placeholder, but still struggling with the content div restyling – Pavel Franta Nov 26 '18 at 09:41
  • @connexo I didn't customize anything, it is like it was, plain HTML element textarea. So it should be valid i guess :D – Pavel Franta Nov 26 '18 at 13:08
  • @connexo ? If I inspect plain, empty textarea in chrome dev tools, it contains at least empty
    in shadow-root ... what exactly is unclear about that ? Question was how to restyle this div... if you dont know any useful answer so please don't waste your time with my question :)
    – Pavel Franta Nov 27 '18 at 15:06

1 Answers1

7

ShadowDOM was designed to prevent CSS from leaking INTO or OUT OF the shadowDOM. It is kindof a replacement for <iframe> which had the same limitations on it. Any CSS in the <iframe> can not affect the content outside of the <iframe> and the CSS outside the <iframe> can not affect the content inside <iframe>.

But you can affect the inner CSS by using one of the following options:

None of the options below work for existing HTML elements. These examples are only for custom elements you write.

The first way to style an element in shadowDOM is by placing the styles in the shadowDOM with the content.

class MyEl extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({mode:'open'}).innerHTML = `
    <style>
      p { background-color: #A00; color: white; }
    </style>
    <p>inner content</p>`;
  }
}

customElements.define('my-el', MyEl);
<my-el></my-el>

The second, more limited way, is to use CSS variables:

class MyEl extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({mode:'open'}).innerHTML = `
    <style>
      p { background-color: var(--bgcolor, #A00); color: var(--color, white); }
    </style>
    <p>inner content</p>`;
  }
}

customElements.define('my-el', MyEl);
body {
  --bgcolor: yellow;
  --color: navy;
}
<my-el></my-el>

The third way, also limited, is through attributes or properties:

class MyEl extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({mode:'open'}).innerHTML = `
    <p>inner content</p>`;
  }
  
  set bgColor(val) {
    this.shadowRoot.querySelector('p').style.backgroundColor = val;
  }

  set color(val) {
    this.shadowRoot.querySelector('p').style.color = val;
  }
}

customElements.define('my-el', MyEl);

const myEl = document.querySelector('my-el');
myEl.bgColor = '#090';
myEl.color = 'white';
<my-el></my-el>
Intervalia
  • 10,248
  • 2
  • 30
  • 60
  • 1
    Doesn't each of these methods require that you have control over the source code of the element you are trying to style? What about mis-behaving third party components? for example i had a Auto Complete Search component that didn't have a z-index correctly set so the suggestions appeared behind other elements. We should be able to explicitly target this component. – Ryan B Mar 18 '19 at 03:38
  • You _should_ be able to change the `z-index` on the element itself and the shadowDOM should move with it. – Intervalia Mar 18 '19 at 14:36