Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use parent CSS from Shadow DOM

I have css from parent application that I want to use inside a web-component made by shadow dom. I don't want to copy css from parent aplication to web-component, but right now, the web-component can't see the parent application css, how can I do that?

parent app:

<style>
.pretty-button {
  color: green
}

</style>
<body>
  <button class="pretty-button">Got It</button>
  <custom-element></custom-element>
</body>

web-component made by shadow dom: 

<!--doesn't work because the shadow dom can't use parent css class-->
<body>
  <button class="pretty-button">Got it from shadow dom</button>
</body>
like image 782
Cledson Araújo Avatar asked Sep 20 '25 05:09

Cledson Araújo


1 Answers

Shadow DOM is protected from outside CSS. This is by design.

TL;DR:

If you want the outside CSS to affect DOM inside the shadowRoot of a custom element then you need to either:

  1. Use a <slot> or
  2. Copy the CSS into the shadowDOM

Here are three answers I have given on similar questions:

  • I have an element that i have declared as a shadow dom but the style is affecting the other elelemts
  • Why does my Web Component CSS not show? I am not using shadowDOM
  • slot selector limit in webcomponent
like image 162
Intervalia Avatar answered Sep 22 '25 01:09

Intervalia