KodingKaro
CSS · Intermediate · CSS Variables

Declaring and using custom properties

CSS custom properties let a design reuse a value and update it consistently. They are especially helpful for colors and spacing that recur across components.

✓ What you will learn in this topic

  • Explain the key idea: Custom properties share values and inherit through the document tree.
  • Apply the concept: The demo uses the shared brand color and spacing token; the card reads its surface, text, and radius from named variables.
  • Avoid this common mistake:

Concept explanation

Declare a custom property with a name beginning with two hyphens, commonly on :root for document-wide tokens. Read it with var(--name). Custom properties inherit, so a value can be overridden on a component or theme container.

Custom properties store token values, not arbitrary preprocessor code. A fallback can be supplied as the second argument to var() when a missing value should still produce a useful style.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

:root {
  --brand: #1d4ed8;
  --space-card: 1.25rem;
}
.demo {
  color: var(--brand);
  padding: var(--space-card);
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
:root {
  --brand: #1d4ed8;
  --space-card: 1.25rem;
}
.demo {
  color: var(--brand);
  padding: var(--space-card);
}

Expected result: The demo uses the shared brand color and spacing token; the card reads its surface, text, and radius from named variables.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
:root {
  --surface: #fff;
  --ink: #172033;
  --radius-card: 1rem;
}
.card {
  color: var(--ink);
  background: var(--surface);
  border-radius: var(--radius-card);
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Custom properties are case-sensitive and resolve where they are used; a variable defined in an unrelated scope may not be available.

Best practice

Name tokens by purpose, define shared values at a deliberate scope, and use var() fallbacks when they improve resilience.

Practice exercise

Create design tokens for brand color, surface, spacing, and radius, then apply them to a reusable card. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Custom properties share values and inherit through the document tree.
  • Name tokens by purpose, define shared values at a deliberate scope, and use var() fallbacks when they improve resilience.

Knowledge check

Which syntax reads a CSS custom property?

Reveal the explanation

Custom properties are referenced with var() and names that begin with two hyphens.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
:root {
  --brand: #1d4ed8;
  --space-card: 1.25rem;
}
.demo {
  color: var(--brand);
  padding: var(--space-card);
}

Expected result:

The demo uses the shared brand color and spacing token; the card reads its surface, text, and radius from named variables.

Hands-on practice task

Create design tokens for brand color, surface, spacing, and radius, then apply them to a reusable card.

In this section

Other lessons in CSS Variables

Broaden your knowledge

Related learning topics