One of the more common questions I get around Web Components is how to style them. The Shadow DOM is a big part of why Web Components are often hard to style. Because it encapsulates your HTML from the main DOM, your nested HTML doesn't inherit your global CSS. When you have a robust design system, this makes everything a lot harder. So much so that I consider the Shadow DOM an anti-pattern in most situations. The trick to stress-free styling of Web Components is to keep all of your HTML in the light DOM. Either nest it in your custom element and progressively enhance it…
Or render it into the light DOM instead of the shadow DOM…
Your HTML will inherit your global CSS automatically. If you need to add some custom styles or overrides, you can use the custom HTML element to add a bit more specificity.
In a future article, I'll talk about when the shadow DOM actually makes sense. Cheers, Want to share this with others or read it later? View it in a browser. |
0 Komentar untuk "[Go Make Things] Styling web components"