Global vs. component styling, with CSS variables

Writing up Sara Soueidan's article on style settings with CSS variables, the thing that resonates is the split between CSS that belongs globally and CSS that belongs at the component level. The mechanics matter less than the fact that the pattern exists at all.

In her approach, components ship with only light default styling. What they do carry is a set of CSS custom properties waiting to receive values if you decide to supply them.

For each pattern, I’ve found myself modifying the same properties whenever I needed to use it — like the font, colors (text, background, border), box shadow, spacing, etc. So I figured it would be useful and time-saving if I created variables for those properties, define those variables in the ‘root’ of the component, and ‘pass in’ the values for these variables when I use the pattern as I need. This way I can customize or theme the component by changing the property values in one rule set, instead of having to jump between multiple ones to do so.

Her motivation is repetitiveness: the same properties — font, text and background colors, borders, box shadow, spacing — kept getting rewritten per use. Declaring variables at the component's root and passing values in lets a single rule set handle theming instead of scattering edits across several.

Full article: Style Settings with CSS Variables.