liveBook · Manning

CSS variable

As explained in Web Components in Action

A CSS variable is a custom CSS property whose value can be defined and substituted throughout a stylesheet or component style.

How can a page change a component’s colors without reaching into its Shadow DOM? A CSS variable—also called a custom property—lets you define a value with a name such as `--text-color` and use it with `var(--text-color)`. Custom properties can cross the shadow boundary, so a component can expose styling choices while keeping its internal rules encapsulated. section 10.2

Define a value, then use it

Set `--text-color: blue` on `body`, set it to `red` on a nested `.container`, and give `.child` the rule `color: var(--text-color)`. The child uses the value defined on its nearer container. In a Web Component, the same pattern works across the shadow boundary: set `--text-color` on `<sample-component>` and use `color: var(--text-color)` in its shadow stylesheet. section 10.2

For a small theme, put shared values on `:root`—for example, text color, label color, font, and heading size—then reference them in component styles. Changing those root values switches the appearance of several components without editing each rule. A larger design vocabulary can also name text sizes, colors, border widths, and border radius; slider, coordinate-picker, and color-picker styles can then use the same choices. section 10.2 section 11.4

Listing 11.15. CSS vars to use throughout a hypothetical app and our components
:root {
   --text-xsmall: .5em;                               #1
   --text-small: .7em;
   --text-medium: 1em;
   --text-large: 1.3em;
   --text-xlarge: 1.5em;

   --color-pureblack: black;                          #2
   --color-black: #2a2a2a;
   --color-lightblack: #4a4a4a;
   --color-darkgrey: #6a6a6a;
   --color-grey: #7a7a7a;
   --color-lightgrey: #9a9a9a;
   --color-darkwhite: #dadada;
   --color-white: #fafafa;
   --color-purewhite: #ffffff;

   --text-color: var(--color-lightblack);             #3
   --text-inverted-color: var(--color-white);
   --border-color: var(--color-lightblack);
   --border-color-light: var(--color-darkwhite);
   --border-inverted-color: var(--color-white);
   --background-color: var(--color-white);
   --background-inverted-color: var(--color-lightblack);

   --border-radius: 6px;                              #4
   --border-width-thick: 3px;
   --border-width: 1px;
   --padding-medium: 5px;
}

The root-level variable list shows how basic colors and text sizes become named choices, including values defined using other variables. section 11.4

Variables handle values, not every shared style rule. Importable CSS modules are another option for distributing common rules, such as input-field styling, among components. Shadow Parts are also being explored for component styling, but their proposed approach was still evolving. For IE11, CSS variables need additional support: the demonstrated `css-vars-ponyfill` setup loads the ponyfill and calls `cssVars()` when available. section 11.4 section 10.2 section 12.5

How Web Components in Action covers it

How deep it goes
Works through declarations, `var()` references, shadow-boundary use, and component-wide theme changes; does not detail CSS variable resolution internals.
Examples
Shows nested text colors, a shadow-styled component, a root-level green theme, and shared values in slider and color-picker styles.
What it assumes
Assumes basic HTML and CSS rules; introduces custom-property declaration and `var()` use before applying them to Web Components.
What you can do after
Operate/configure it to change shared component styling through named CSS values and add a ponyfill when supporting IE11.
How it connects
Connects CSS custom properties to Shadow DOM, Web Components, design systems, Shadow Parts, and importable CSS modules.

Also explained in

Compare how 4 books explain CSS variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.