liveBook · Manning

CSS variable

As explained in Tiny CSS Projects

A CSS custom property is an author-defined property whose value can be stored and reused in CSS through the var() function.

A CSS variable, also called a custom property, stores a value you can reuse in CSS. For a profile card, one image size can serve several calculations, while shared colors, fonts, and backgrounds keep the styling consistent. section 6.2

Define one inside a CSS rule with a name beginning `--`, a colon, and a value: `--imageSize: 200px;`. Use it in another declaration with `var(--imageSize)`. Properties defined on `body` are available to the body and its descendants. The value need not be a color: it can also be a size, font family, or gradient. section 6.2

Listing 6.2 Defining CSS custom properties
body {
  --primary: #de3c4b;                    #1
  --primary-contrast: white;
  --secondary: #717777;                  #2
  --font: Helvetica, Arial, sans-serif;
  --text-color: #2D3142;                 #3
  --card-background: #ffffff;
  --technologies-background: #ffdadd;
  --page-background: linear-gradient(#4F5D75, #2D3142);
  --imageSize: 200px;
 
  background: var(--page-background);
  font-family: var(--font);
  color: var(--text-color);
}

See several custom properties defined together on `body`, then used for its background, font family, and text color. section 6.2

A form error color

A form can define `--error: #dd1c1aff;` and use `.error { color: var(--error); }`. The same value can color an invalid field’s border. Pair the red error text with an icon or other cue: color alone may not make the message clear to everyone. section 10.1 section 10.8

CSS custom properties are not the same as Sass variables. Sass uses `$name` and handles variables at compile time; custom properties are dynamic and participate in CSS inheritance. In a comparison where a custom property is reassigned on `body`, both paragraphs receive its later border value, while earlier uses of a Sass color retain their original value. section 12.2

How Tiny CSS Projects covers it

How deep it goes
Explains declaration and `var()` syntax, descendant access, value types, and a behavioral contrast with Sass; does not explore browser internals.
Examples
Shows a profile-card palette and image size, a form’s shared error color, and contrasting CSS and Sass variable assignments.
What it assumes
Introduces `--name` and `var()` from the beginning while using familiar CSS rules and declarations as the starting point.
What you can do after
Build it from scratch by defining custom properties in a CSS rule and reusing them with `var()` in styling declarations.
How it connects
Links CSS inheritance, gradients, form validation, accessibility, and Sass variables to reusable styling values.

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 Tiny CSS Projects. The section links point to the parts of the book it draws on.