For the last week, we've been looking at why styling elements in the shadow DOM sucks so bad, and what to do about it. So far, we've looked at using inline styles and external stylesheets loaded into the shadow DOM. Today, let's talk about CSS variables. Our example elementJust to recap, we have a
Which, when the Web Component JavaScript loads, renders HTML like this…
And it's styled to look like this…
Currently, the only way for a developer to change the styles of those elements is by directly modifying the Web Component's JS. Let's fix that! CSS variables can be accessed from the light DOMWhile global CSS does not cascade into the Shadow DOM, CSS variables do! If you want to give developers the ability to style certain aspects of your web component, but not everything, CSS variables (officially called custom properties) provide a useful way to do that. We can update our inline styles to use CSS variables. Here, I'm also passing a fallback value into the
In my global stylesheet, I can now modify all of these properties using CSS variables on my If I want to style different web components uniquely, I can even target them more specifically by class, ID, or an attribute like
Cheers, Want to share this with others or read it later? View it in a browser. |
Beranda » Tanpa Label » [Go Make Things] Styling the shadow DOM with CSS variables in Web Components
0 Komentar untuk "[Go Make Things] Styling the shadow DOM with CSS variables in Web Components"