SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Welcome to CSS
CSS Values and Responsive SizingCSS UnitsCSS Custom PropertiesCSS FunctionsFluid SizingDesign Tokens Choosing units and tracing values
Values and Units

CSS Custom Properties

Declare CSS custom properties, trace how their values inherit, and supply fallbacks when a value is missing.

CSS custom properties, often called CSS variables, let you give a value a name and reuse it throughout a stylesheet. A named color or size can represent a shared design choice: instead of repeating the value in each rule, you refer to the name. These reusable design values can serve as design tokens, helping keep styles consistent and easier to update.

Declare and use a custom property

Declare a custom property inside a CSS rule using a name that begins with --. To use its value, pass the name to var():

:root {
  --primary-color: #3498db;
  --padding-size: 20px;
}

.button {
  background-color: var(--primary-color);
  padding: var(--padding-size);
}

The :root selector matches the document's root element and is a common place for values used throughout a page. Here, var(--primary-color) supplies the button's background color, and var(--padding-size) supplies its padding.

The root supplies the value of --primary-color. Reading it with var() gives the button its blue background.

A descriptive name such as --primary-color communicates the value's purpose. If several rules use that property, changing its declaration updates each use without editing the same color in multiple places.

Scope, inheritance, and the cascade

A custom property is available on the element where it is declared and can be inherited by its descendants. A property declared on :root is therefore available throughout the page, while one declared on a smaller section can provide a value for that section.

:root {
  --theme-color: blue;
}

.theme-dark {
  --theme-color: darkblue;
}

.header {
  color: var(--theme-color);
}

A .header inside a .theme-dark element inherits darkblue from that ancestor. A header outside that section can inherit blue from the root. This lets one section use a different theme value without changing the root declaration.

A header outside .theme-dark uses the root’s blue value. Inside .theme-dark it inherits darkblue from the nearer ancestor.

Custom properties still follow the cascade: when multiple declarations apply to the same element, CSS determines which one wins. A value declared on a child can also replace the value it would otherwise inherit.

Provide a fallback

You can add a fallback as the second argument to var(). It is used when the custom property is not defined:

:root {
  --primary-color: #17232a;
}

.button {
  background-color: var(--secondary-color, #95a5a6);
  color: var(--primary-color);
}

Because --secondary-color is not defined here, the button uses #95a5a6 for its background. The text uses the defined --primary-color. The fallback does not check whether a defined value is suitable for the property using it, so choose and validate values carefully.

Define constraints with @property

The @property rule can define the kind of value a custom property accepts, whether it inherits, and its initial value:

@property --border-radius {
  syntax: '<length>';
  inherits: false;
  initial-value: 0px;
}

.box {
  --border-radius: 10px;
  border-radius: var(--border-radius);
}

Here, syntax: '<length>' specifies a length value, inherits: false means the property does not inherit from a parent, and initial-value: 0px supplies its initial value. The .box sets the value to 10px and uses it for its border radius. A child that never sets this registered property starts at 0px. Browsers without registration support ignore the at-rule and treat the name as an ordinary inheriting custom property, so a child can receive the parent’s 10px instead. MDN compatibility data for @property.

Try it yourself

Try it yourself

Trace custom property values

HTML + CSS

Loading code editor…

Live preview

Updating…

Final output

No comparison yetChecks after 3s without edits
Loading your draft…

Key Takeaways

  • Custom properties: Declare reusable values with names beginning with --, then reference them with var().
  • Design tokens: Named, shared design values make styles more consistent and easier to change.
  • Scope and inheritance: Properties declared on an element are available to it and can be inherited by descendants; the cascade still applies.
  • Fallbacks: A second var() argument provides a value when the custom property is not defined.
  • @property: You can specify an accepted value type, inheritance behavior, and an initial value.

How is this lesson?

Previous lessonCSS UnitsNext lessonCSS Functions
Edit on GitHubReport on GitHub
On this page
%

On this page

Declare and use a custom propertyScope, inheritance, and the cascadeProvide a fallbackDefine constraints with @propertyTry it yourselfKey Takeaways