Skip to article content
FrontendXLesson 28 / 30

Custom properties and design tokens

Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.

advanced · about 40 minutes · 2 exercises

  • Implement
  • Debug

What you will be able to do

#
  • Define and consume custom properties for a token system.
  • Explain inheritance and fallback behavior of custom properties.

Overview

#

Custom properties are author-defined values that participate in the cascade and inherit. They begin with two hyphens and are read with var(). Because they cascade and inherit, you can define a token once on :root and override it in a subtree, which is how theming works.

snippet.css
:root {
  --color-bg: #ffffff;
  --color-fg: #171717;
  --space-2: 0.5rem;
  --radius: 8px;
}

[data-theme="dark"] {
  --color-bg: #0a0a0a;
  --color-fg: #ededed;
}

.card {
  background: var(--color-bg);
  color: var(--color-fg);
  padding: var(--space-2);
  border-radius: var(--radius);
}

Unlike preprocessor variables, custom properties are resolved at runtime by the browser. That means they can change in response to a class, a media query or a user interaction, and descendants update automatically. They are the foundation of a theme switch that needs no JavaScript beyond toggling an attribute.

var() accepts a fallback as a second argument, which is used when the property is not defined or is invalid:

snippet.css
.button {
  background: var(--brand, #0072f5);
}

Two caveats matter. First, custom properties are not type-checked at definition time; an invalid value only fails when used. Second, they inherit, so a token set on a parent leaks to all descendants unless overridden. Use @property to give a custom property a syntax, an initial value and inheritance control when you need type safety or want to animate it.

snippet.css
@property --angle {
  syntax: "<angle>";
  inherits: false;
  initial-value: 0deg;
}

Name tokens by role, not by appearance. --color-primary survives a rebrand; --color-blue does not.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Define light tokens on :root and a dark override on [data-theme=dark] for background and foreground, then consume them on a .card.

    starter.css
    :root { /* tokens */ }
    .card { /* consume */ }

    Reveal the reference answer
    reference.css
    :root {
      --color-bg: #ffffff;
      --color-fg: #171717;
    }
    [data-theme="dark"] {
      --color-bg: #0a0a0a;
      --color-fg: #ededed;
    }
    .card {
      background: var(--color-bg);
      color: var(--color-fg);
    }

    Authored expected output: The card uses the light tokens by default and the dark tokens when the data-theme attribute is dark.

    Custom properties inherit, so overriding them on an ancestor changes every descendant that consumes them.

  2. Exercise 2Debug

    This rule has no effect. Explain why and fix it.

    starter.css
    .card { background: var(--color-bg #ffffff); }

    Reveal the reference answer
    reference.css
    The fallback needs a comma: var(--color-bg, #ffffff).

    Authored expected output: The var() call uses a comma before the fallback value.

    Without the comma the whole token name is invalid and the declaration is dropped.

Common misconceptions

#
  • Custom properties are replaced at build time like preprocessor variables.
  • A custom property with an invalid value fails immediately at definition.

Follow-up practice

#
  • html-css-29-transitions-animations

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.