Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do /deep/ and ::shadow mean in a CSS selector?

In looking at Polymer, I see the following CSS selector in the Styles tab of Chrome 37's developer tools:

enter image description here

I've also seen a selector with pseudo selector ::shadow.

So, what do /deep/ and ::shadow in a CSS selector mean?

like image 285
Drew Noakes Avatar asked Sep 01 '14 16:09

Drew Noakes


People also ask

What is :: deep in CSS?

::ng-deep selectorThe ::ng-deep combinator works to any depth of nested components, and it applies to both the view children and content children of the component. The following example targets all <h3> elements, from the host element down through this component to all of its child elements in the DOM.

What is CSS shadow parts?

CSS Shadow Parts allow developers to style CSS properties on an element inside of a shadow tree. This is extremely useful in customizing Ionic Framework Shadow DOM components.

What is shadow host CSS?

The :host CSS pseudo-class selects the shadow host of the shadow DOM containing the CSS it is used inside — in other words, this allows you to select a custom element from inside its shadow DOM.

What is shadow DOM in HTML?

Shadow DOM allows hidden DOM trees to be attached to elements in the regular DOM tree — this shadow DOM tree starts with a shadow root, underneath which you can attach any element, in the same way as the normal DOM.


Video Answer


1 Answers


As Joel H. points out in the comments, Chrome has since deprecated the /deep/ combinator, and it gives a syntax error in IE.


HTML5 Web Components offer full encapsulation of CSS styles.

This means that:

  • styles defined within a component cannot leak out and effect the rest of the page
  • styles defined at the page level do not modify the component's own styles

However sometimes you want to have page-level rules to manipulate the presentation of component elements defined within their shadow DOM. In order to do this, you add /deep/ to the CSS selector.

So in the example shown, html /deep/ [self-end] is selecting all elements under the html (top level) element that have the self-end attribute, including those buried inside web components' shadow DOMs roots.

If you require a selected element to live within a shadow root, then you can use the ::shadow pseudo selector on its parent element.

Consider:

<div>   <span>Outer</span>   #shadow-root   <my-component>     <span>Inner</span>   </my-component> </div> 

The selector html /deep/ span will select both <span> elements.

The selector ::shadow span will select only the inner <span> element.

Read more about this in the W3C's CSS Scoping Module specification.

like image 86
Drew Noakes Avatar answered Sep 30 '22 22:09

Drew Noakes