CSS Academy · Lesson

Fallback Values and var()

Provide fallback values in var() for when a custom property is undefined or invalid.

Lesson 2 of 413 steps

Fallback Values and var() is a free CSS Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, and your progress syncs across the web and the CoddyKit app. The CSS Academy course includes 4 lessons in total.

var() Fallback Syntax

The var() function accepts an optional second argument as a fallback value, used when the custom property is not defined.

<button class="button">Fallback: royalblue</button>

When Fallbacks Apply

The fallback is used when:

  • The property is not defined in scope
  • The property has the value initial
  • The property was declared with an invalid value for its context

Multiple Levels of Fallback

Nest var() calls to chain multiple fallback levels:

<p class="text">Falls through two levels to #3b82f6</p>

Fallback Can Be Any Value

Fallbacks can be any valid value for the property — including other variables, computed values, or literals:

<div class="card">16px / 50%</div>

The initial Keyword

Setting a custom property to initial makes it as if it was never set. Combined with fallback, this lets components opt out of an inherited custom property.

<div class="no-theme">
  <button class="button">Falls back to gray</button>
</div>

Guaranteed Invalid Value

Using initial as a custom property value is a technique to guarantee the fallback triggers. This can create optional feature flags in CSS.

Custom Property + @property

The @property rule (Houdini) registers a typed custom property with a guaranteed initial value. This is more robust than relying on fallbacks.

<div class="track">
  <div class="bar"></div>
</div>

Fallback for Animation-Safe Values

Ensure transitions work even when a custom property is not set by providing a safe fallback that can be transitioned from:

<div class="modal">Modal shown via --modal-opacity: 1 (fallback is 0 when unset)</div>

Debugging Missing Variables

If a custom property is missing and no fallback is provided, the property computes to its initial value (often initial keyword). For colors, this may produce unexpected transparent or black results. Always add fallbacks to critical properties.

var() in Custom Properties

Custom property values can themselves reference other custom properties, creating a chain of tokens:

<div class="swatch swatch-primary"></div>
<div class="swatch swatch-light"></div>

Fallback and Specificity

Fallbacks have nothing to do with specificity — they only apply when the referenced custom property is undefined or invalid. Specificity determines which rule sets the custom property, not which fallback wins.

Quick Check

What does color: var(--theme-text, #111) do when --theme-text is not defined?

Recap

var(--name, fallback) provides a fallback when the custom property is not defined. Fallbacks can be nested for multiple levels. Use @property for typed custom properties with guaranteed initial values. Always provide fallbacks for critical visual properties.

Free to start

Learn CSS with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Fallback Values and var()” lesson free?

Yes — the full text of “Fallback Values and var()” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO. The CSS Academy course includes 4 lessons in total.

What will I learn in “Fallback Values and var()”?

Provide fallback values in var() for when a custom property is undefined or invalid. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CSS Academy?

No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Fallback Values and var()” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CSS Academy lesson?

Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declaring and Using Custom Properties
  2. Fallback Values and var()
  3. Scoping Variables to Components
  4. Updating Variables with JavaScript
← Back to CSS Academy