Shadow DOM: how to apply CSS style only if the host element is <body>?

Anton Moiseev

I have to apply special CSS style to my web component if it's a direct child of <body> element. Here is what I've tried so far:

/* applies even if the component isn't a direct child */
:host(body) {
    color: red;
}

/* never applies */
:host(body:host) {
    color: red;
}

/* never applies */    
:host(body:host > my-component) {
    color: red;
}

/* never applies */    
:host(body > my-component:host) {
    color: red;
}

Browsers: Chrome Stable (32.0.1700.107), Chrome Canary (34.0.1843.3).

robdodson

I don't think this is currently possible without a parent selector in CSS.

You could do something like this in Chrome 32:

/* @polyfill body > :host h1 */

This works in Chrome 32 because the @polyfill directive adds a document level style that says: body > polymer-foo h1. However, this does not work in Chrome 34 because document level styles are ignored by the Shadow DOM.

Also, :host will now only match the host element itself. If you want to try matching the ancestor you can use :ancestor(). Unfortunately :ancestor(body) > :host h1 will not work. :ancestor(body) gets translated to any node which is a descendant of body so the above snippet would be rewritten as polymer-foo > polymer-foo h1.

It's disappointing that this is not possible today but Shadow DOM styles are still in their infancy and hopefully we'll be able to be more expressive in the future.

For future reference, the work in progress spec on Shadow DOM styling can be found here: http://dev.w3.org/csswg/shadow-styling

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Angular 2: How to style host element of the component?

From Java

How can i check whether an element can host Shadow DOM?

From Java

Apply CSS style only to specific li element

From Dev

How to I apply a style to only the first row (matching a class) of a table using pure css?

From Dev

How to render shadow DOM in a polymer custom element

From Dev

How to apply style to a specific element in JQuery array

From Dev

How to Apply SlimScroll to Body Element?

From Dev

Apply CSS style to last visible element

From Dev

How to keep css style for only one element

From Dev

Why doesn't the z-index style apply to the <body> element?

From Dev

How to apply drop-shadow filter via CSS to SVG specific element/path

From Dev

CSS: apply style only on <a> without inner <img>

From Dev

How to apply different css style for IE 10 and 11 only?

From Dev

Can't style element in shadow dom in media query

From Dev

How to apply ngStyle to :host element in component?

From Dev

CSS style on an element only of a table with a specific class

From Dev

Custom CSS does not apply to Body element with jQuery

From Dev

How to apply CSS generated by colorzilla to the body element

From Dev

How to handle only scroll on child DOM element instead of body element?

From Dev

Apply css style to child element of parent class

From Dev

Javascript/CSS -- apply css style on clicking unrelated element

From Dev

Apply CSS style to last visible element

From Dev

JavaScript styling of DOM shadow host

From Dev

How to inline style child DOM element React?

From Dev

My css ::after apply only on the last element

From Dev

Apply pseudo-element CSS selector inside shadow DOM

From Dev

Is it possible to apply a CSS style to the container element?

From Dev

Polymer: apply style to an element under Shadow DOM

From Dev

how to only target a css class if it is the first element within the body tag?

Related Related

  1. 1

    Angular 2: How to style host element of the component?

  2. 2

    How can i check whether an element can host Shadow DOM?

  3. 3

    Apply CSS style only to specific li element

  4. 4

    How to I apply a style to only the first row (matching a class) of a table using pure css?

  5. 5

    How to render shadow DOM in a polymer custom element

  6. 6

    How to apply style to a specific element in JQuery array

  7. 7

    How to Apply SlimScroll to Body Element?

  8. 8

    Apply CSS style to last visible element

  9. 9

    How to keep css style for only one element

  10. 10

    Why doesn't the z-index style apply to the <body> element?

  11. 11

    How to apply drop-shadow filter via CSS to SVG specific element/path

  12. 12

    CSS: apply style only on <a> without inner <img>

  13. 13

    How to apply different css style for IE 10 and 11 only?

  14. 14

    Can't style element in shadow dom in media query

  15. 15

    How to apply ngStyle to :host element in component?

  16. 16

    CSS style on an element only of a table with a specific class

  17. 17

    Custom CSS does not apply to Body element with jQuery

  18. 18

    How to apply CSS generated by colorzilla to the body element

  19. 19

    How to handle only scroll on child DOM element instead of body element?

  20. 20

    Apply css style to child element of parent class

  21. 21

    Javascript/CSS -- apply css style on clicking unrelated element

  22. 22

    Apply CSS style to last visible element

  23. 23

    JavaScript styling of DOM shadow host

  24. 24

    How to inline style child DOM element React?

  25. 25

    My css ::after apply only on the last element

  26. 26

    Apply pseudo-element CSS selector inside shadow DOM

  27. 27

    Is it possible to apply a CSS style to the container element?

  28. 28

    Polymer: apply style to an element under Shadow DOM

  29. 29

    how to only target a css class if it is the first element within the body tag?

HotTag

Archive