Skip to content

CSS Custom Property (Variable) Not Working Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about CSS Custom Property (Variable) Not Working Fix. We cover key concepts, practical examples, and best practices.

The Problem

You define a CSS custom property with --primary: blue but the var(--primary) reference does not apply. The property shows as transparent or the browser uses the fallback value. CSS variables are inheritance-sensitive and have strict syntax rules.

Quick Fix

Step 1: Define the variable with double dash prefix

/* Wrong — missing double dash */
primary: blue;

/* Wrong — SCSS variable syntax */
$primary: blue;

/* Right */
:root {
    --primary: blue;
}

Expected output: The variable --primary is now available globally with value blue.

Step 2: Use var() to reference the variable

.button {
    /* Wrong — using variable name directly */
    background-color: --primary;
    /* Right */
    background-color: var(--primary);
}

Expected output: The button background is blue.

Step 3: Check the variable scope

Variables are inherited. A variable defined in a selector is only available to its descendants:

/* Wrong — defined in narrow scope */
.card {
    --accent: red;
}
/* Outside .card, --accent is not available */
.button {
    background-color: var(--accent, gray);
}

/* Right — define in :root for global access */
:root {
    --accent: red;
}

Expected output: The button gets gray fallback — or red once the variable is defined globally.

Step 4: Provide a fallback value

Always provide a fallback in var():

.element {
    /* Wrong — no fallback */
    color: var(--undefined-var);
    /* Right — with fallback */
    color: var(--undefined-var, black);
}

Expected output: The element renders black instead of transparent when the variable is missing.

Step 5: Debug invalid computed values

If a variable holds an invalid value for the property, the browser ignores it:

:root {
    --invalid: 10px;
}
.element {
    /* Wrong — 10px is invalid for color */
    color: var(--invalid);
    /* Right — use correct types */
    --valid: red;
    color: var(--valid);
}

Expected output: The element color is red. With --invalid, the browser would use initial (black for color).

Step 6: Verify the variable is not overwritten by a more specific selector

:root { --btn-bg: blue; }
.warning { --btn-bg: red; }
.button { background: var(--btn-bg); }
/* A button inside .warning gets red, outside gets blue */

Expected output: The button inside .warning is red due to variable overriding.

Prevention

  • Define global variables in :root
  • Always supply a fallback in var() calls
  • Use browser DevTools Computed panel to see which variable value is active
  • Prefer var(--name, fallback) over relying on the cascade

Common Mistakes with variable not working

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks

These mistakes appear frequently in real-world CSS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Can I use CSS variables in media queries?

No. Media query conditions cannot use var(). You must hardcode breakpoint values in @media rules. CSS variables only work in property values, not in conditional expressions.

Why does my variable work in one file but not another?

CSS variables are inherited, not global by default. If the variable is defined in a file that is loaded in a different scope (e.g., inside a Shadow DOM), it will not be available outside. Define variables in :root for cross-file access.

What happens when a CSS variable is undefined?

The property uses its initial or inherited value. If you provided a fallback in var(--name, fallback), the fallback is used instead. If no fallback is given, the property becomes initial or inherit, which may differ from the expected default.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro