Apparently, the problem is that you are trying to use global CSS to style the elements of a shadow tree.
You can use the :host pseudo-selector, however, for this you will need to put the style in the shadow tree of the content.
Make the following changes to your javascript:
class El extends HTMLElement { constructor() { super(); var shadow = this.attachShadow({mode:'open'}); var innerHTML = ''; innerHTML += '<style>'; innerHTML += ':host(element-el.red) span {color: red}'; innerHTML += ':host(element-el.green) span {color: green}'; innerHTML += ':host(element-el.blue) span {color: blue}'; innerHTML += '</style>'; innerHTML += '<span class="x">X</span>'; shadow.innerHTML = innerHTML; } } customElements.define ('element-el',El);
Check out the functional example in updated codepen .
source share