Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shadow Dom inheriting parent page CSS [Chrome] [duplicate]

Everything I've read indicates that the Shadow Dom is supposed to be 'safe' from its parent page CSS. I.E. if I have all divs styled to have purple font:

<style>
  div{color: purple}
</style>

The divs in my Shadow Dom should have the browser default color.

I am writing a chrome extension that injects html into any given page. Unless this html is protected by either Shadow Dom or Iframe, it will inherit all the page's CSS.

The advice to solve this problem in this question, was to use the Shadow Dom. So I implemented a solution, but noticed it was inheriting the page's CSS still. I thought this might have been an issue with using it in a Chrome extension, so I hijacked a jsBin from some Shadow Dom examples (and threw it in another live coding app for good measure).

https://codepen.io/hyrumwhite/pen/xPRexQ

Same result. My shadow DOM inherits the page CSS, and my divs (and weirdly my h1) are purple.

It looks like the children in the Shadow Dom will inherit any styling applied to the host element.

Is this working as designed? Is there a way to prevent this? Or is the shadow DOM new enough that this is a bug and I should expect similar bugs as I keep using it?

like image 667
SethWhite Avatar asked Nov 08 '17 21:11

SethWhite


People also ask

Does shadow DOM inherit CSS?

Theming and custom propertiesA shadow tree inherits CSS properties from its host. To let users customize your element, you can expose specific styling properties using CSS custom properties and custom property mixins. Custom properties provide a way to add a styling API to your element.

How do I change the CSS of shadow DOM?

CSS hooks with custom properties There's no selector that can directly affect shadow DOM styles from the document. But just as we expose methods to interact with our component, we can expose CSS variables (custom CSS properties) to style it. Custom CSS properties exist on all levels, both in light and shadow.

How do I get shadow DOM on Google Chrome?

To enable shadow dom in chrome browser open Developer tools by Ctrl+Shift+I. Click on 3 vertical dots on the top right corner of dev tools before close button. Now click on settings there you will find “Show user agent shadow DOM” checkbox under the Elements section in General Tab.

How do I get rid of shadow DOM?

In Chrome: Press F12, DevTool will open. Click gear icon in DevTool. Uncheck "show user agent shadow DOM" checkbox.


2 Answers

For me, adding :host { all: initial } as the first CSS rule within the ShadowDOM styles prevented inheritance without affecting other CSS defined within the ShadowDOM.

Using * { all: initial } proved to be too broad and overrode most of my CSS defined within the ShadowDOM.

Ref: Section marked #reset in WebFundamentals project ShadowDOM document.

like image 80
user650881 Avatar answered Sep 17 '22 12:09

user650881


Inherited properties will be inherited as usual. It's better to think of the shadow boundary as affecting the cascade, namely the scope of selectors and the importance of rules.

To isolate shadow content from the page, consider the all property.

document.getElementById("example_control").attachShadow({mode:'open'}).innerHTML=`
  <h1>shadow dom header</h1>
  <div>shadow dom div</div>`;
document.getElementById("example_initial").attachShadow({mode:'open'}).innerHTML=`
  <style>*{all:initial}</style>
  <h1>shadow dom header</h1>
  <div>shadow dom div</div>`;
document.getElementById("example_unset").attachShadow({mode:'open'}).innerHTML=`
  <style>*{all:unset}</style>
  <h1>shadow dom header</h1>
  <div>shadow dom div</div>`;
div{color:purple}
div{border:1px solid}
<p>control:
<div id=example_control></div>

<p>initial:
<div id=example_initial></div>

<p>unset
<div id=example_unset></div>
like image 42
Josh Lee Avatar answered Sep 20 '22 12:09

Josh Lee