Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling a child of a shadow root in Shadow DOM

I'm trying to style a child element of a shadow DOM root.
This defines a custom element called element-el, which have a span class-named 'x' with the letter x in it, which I want, for the state of the matter, to be red.

class El extends HTMLElement {
    constructor() {
        super();
        var shadow = this.attachShadow({mode:'open'});
        shadow.innerHTML = '<span class="x">X</span>';
    }
}
customElements.define ('element-el',El);

I've tried those CSS styles:

element-el::slotted(.x) {
  color:red;
}
element-el::host .x {
  color:red;
}
element-el:host .x {
  color:red;
}
element-el::shadow .x {
  color:red;
}
element-el /deep/ .x {
  color: red;
}
element-el::content .x {
  color:red;
}

The X does not become red. I'm using Chrome 56, which is supposed to support this...

I want to style it without putting a style element inside the shadow DOM.
Here is a codepen: http://codepen.io/anon/pen/OpRLVG?editors=1111

EDIT:
This article suggests that it is possible to style shadow children from an external CSS file -- are they simply wrong?

like image 712
Yuval A. Avatar asked Mar 06 '17 01:03

Yuval A.


People also ask

How to use custom CSS in Shadow DOM?

Custom CSS properties pierce through shadow DOM, they are visible everywhere, so the inner .field rule will make use of it. Shadow DOM can include styles, such as <style> or <link rel="stylesheet">. slotted elements (coming from light DOM), ::slotted (selector) allows to select slotted elements themselves, but not their children.

What is a shadow root in HTML?

The shadow root is a document fragment that is attached to the host element and it has a host property that identifies its host element. The act of attaching a shadow root is how the element gets its shadow DOM. There are a few bits of shadow DOM terminology that you need to be aware of:

Can we get built-in Shadow DOM elements with regular JavaScript calls?

We can’t get built-in shadow DOM elements with regular JavaScript calls or selectors. These are not regular children but a strong technique for encapsulation. Shadow Root: A shadow tree is a node tree whose root called as a shadow root. A shadow root is always attached through its host to another node tree.

What is the Shadow root of a subtree?

The ShadowRoot interface of the Shadow DOM API is the root node of a DOM subtree that is rendered separately from a document's main DOM tree. You can retrieve a reference to an element's shadow root using its Element.shadowRoot property, provided it was created using Element.attachShadow () with the mode option set to open.


Video Answer


1 Answers

Apparently the problem is the fact that you are trying to use the global CSS to style the shadow tree elements.

You can use the :host pseudo selector, however in order to do so you will have to place the style inside the shadow tree content.

Make the following changes in 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 a functional example in your updated codepen.

like image 121
Romulo Avatar answered Oct 19 '22 03:10

Romulo