Tutorials Logic, IN info@tutorialslogic.com

CSS Variables Custom Properties var

CSS Variables Custom Properties var

CSS variables, also called custom properties, let you name important design values such as colors, spacing, shadows, and component sizes. The browser keeps them live, so changing one variable can update many rules instantly.

They are especially helpful for themes and component variants. A card can define its own accent color, a dark theme can override global colors, and a media query can adjust spacing values without rewriting every selector.

Add one worked example that compares the normal path with the boundary case for CSS Variables Custom Properties var.

CSS Variables Custom Properties var should be studied as a practical CSS lesson, not as a label. Start by naming the input, the rule that changes the input, and the result a learner should be able to predict after reading the page.

In the css > css-variables page, the notes should connect the definition with a working scenario, a mistake that beginners actually make, and the exact check that proves the fix. That makes the topic useful for coding, debugging, and interview revision.

What are CSS Variables?

CSS Variables (officially called Custom Properties) let you store values that can be reused throughout your stylesheet. They are defined with --name and accessed with var(--name). Unlike preprocessor variables (Sass, Less), CSS variables are live - they can be changed at runtime with JavaScript and respond to media queries.

  • Defined with -- prefix: --primary-color: #3498db;
  • Used with var(): color: var(--primary-color);
  • Scoped to the element where they're defined (and its descendants)
  • Can have fallback values: var(--color, blue)

CSS Variables - Basic Usage

CSS Variables - Basic Usage
/* Define variables in :root - available globally */
:root {
    --primary-color: #3498db;
    --secondary-color: #2ecc71;
    --danger-color: #e74c3c;
    --text-color: #2c3e50;
    --bg-color: #ecf0f1;
    --font-main: 'Roboto', sans-serif;
    --spacing-sm: 8px;
    --spacing-md: 16px;
    --spacing-lg: 24px;
    --border-radius: 4px;
    --transition-speed: 0.3s;
}

/* Use variables with var() */
body {
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: var(--font-main);
}

.btn-primary {
    background-color: var(--primary-color);
    padding: var(--spacing-md);
    border-radius: var(--border-radius);
    transition: all var(--transition-speed);
}

.btn-primary:hover {
    background-color: var(--secondary-color);
}

/* Fallback value - used if variable is not defined */
.card {
    border: 1px solid var(--border-color, #ddd);
}

/* Scoped variables - override in specific contexts */
.dark-theme {
    --bg-color: #1a1a2e;
    --text-color: #eee;
    --primary-color: #64b5f6;
}

/* Calculations with variables */
.container {
    padding: calc(var(--spacing-md) * 2);  /* 32px */
    margin-top: calc(var(--spacing-lg) + 10px);  /* 34px */
}

Dynamic Theming with CSS Variables

Variables follow normal CSS inheritance. A value defined on :root is available everywhere, but a value defined on .pricing-card applies only to that element and its children. This makes them useful for local component customization.

Dark Mode Toggle with CSS Variables + JavaScript

Dark Mode Toggle with CSS Variables + JavaScript
/* Light theme (default) */
:root {
    --bg: #ffffff;
    --text: #2c3e50;
    --card-bg: #f8f9fa;
    --border: #dee2e6;
}

/* Dark theme - override variables */
[data-theme="dark"] {
    --bg: #1a1a2e;
    --text: #eee;
    --card-bg: #16213e;
    --border: #0f3460;
}

/* Use variables everywhere */
body {
    background-color: var(--bg);
    color: var(--text);
    transition: background-color 0.3s, color 0.3s;
}

.card {
    background-color: var(--card-bg);
    border: 1px solid var(--border);
}

Dynamic Theming with CSS Variables

Dynamic Theming with CSS Variables
// Toggle theme
const toggleBtn = document.getElementById('theme-toggle');
toggleBtn.addEventListener('click', () => {
    const current = document.documentElement.getAttribute('data-theme');
    const next = current === 'dark' ? 'light' : 'dark';
    document.documentElement.setAttribute('data-theme', next);
    localStorage.setItem('theme', next);
});

// Load saved theme
const saved = localStorage.getItem('theme');
if (saved) document.documentElement.setAttribute('data-theme', saved);

Component-Level Variables

Instead of creating many modifier classes that repeat full declarations, define the parts that change as variables. The component keeps its structure, while each variant changes only the custom properties it owns.

Button variants with local custom properties

Button variants with local custom properties
.button {
    --button-bg: #2563eb;
    --button-text: #ffffff;
    --button-border: #2563eb;

    background: var(--button-bg);
    color: var(--button-text);
    border: 1px solid var(--button-border);
    padding: 0.75rem 1rem;
    border-radius: 0.5rem;
}

.button.secondary {
    --button-bg: #ffffff;
    --button-text: #2563eb;
}

.button.danger {
    --button-bg: #dc2626;
    --button-border: #dc2626;
}

CSS Variables Custom Properties var in Real Work

CSS Variables Custom Properties var matters in CSS because it changes how a program is written, tested, or debugged. The page should explain the normal flow first: what the developer writes, what the runtime or platform does, and what result should appear.

When teaching CSS Variables Custom Properties var, avoid stopping at syntax. Show the surrounding decision: why this feature is chosen, what problem it removes, and what would become harder if the feature were not used.

  • Identify the concrete problem solved by CSS Variables Custom Properties var.
  • Show the normal input, operation, and output for css.
  • Mention the nearby alternative a beginner may confuse with this topic.
  • Tie the explanation to a real project task, command, component, query, or debugging step.

CSS Variables Custom Properties var CSS normal case

CSS Variables Custom Properties var CSS normal case
.lesson-box {
  display: block;
  max-width: 42rem;
  padding: 1rem;
}

CSS Variables Custom Properties var CSS fallback case

CSS Variables Custom Properties var CSS fallback case
.lesson-box:empty::before {
  content: "CSS Variables Custom Properties var: add visible content";
}
Key Takeaways
  • Name variables by purpose, such as --color-primary, not by one temporary value.
  • Keep global tokens on :root and component-specific variables on the component selector.
  • Provide var() fallbacks when a variable may be optional.
  • Use variables inside media queries to adjust design values cleanly.
  • Remember that variables inherit, so a nested override can affect all children.
Common Mistakes to Avoid
WRONG Naming a variable --blue when it represents the primary brand color.
RIGHT Use --color-primary so the name still makes sense if the color changes later.
Purpose-based names are easier to maintain than color-based names.
WRONG Defining every variable globally even when only one component uses it.
RIGHT Place component-only variables on the component selector.
Local variables keep the design system cleaner.
WRONG Using var(--space-large) without a fallback when the variable is optional.
RIGHT Use var(--space-large, 2rem) for a safe default.
Fallbacks prevent invalid computed values.
WRONG Memorizing CSS Variables Custom Properties var without the situation where it is useful.
RIGHT Connect CSS Variables Custom Properties var to a concrete CSS task.
Purpose makes syntax easier to recall.

Practice Tasks

  • Create a light and dark theme by overriding color variables on [data-theme].
  • Build a card component where only --card-accent changes between variants.
  • Use a spacing variable inside calc() to double a container padding value.
  • Add a fallback value to a missing variable and confirm the style still renders.
  • Write a small example that uses CSS Variables Custom Properties var in a realistic CSS scenario.

Frequently Asked Questions

No. Sass variables are compiled before the browser sees the CSS. CSS variables exist in the browser and can change at runtime.

Yes. JavaScript can call document.documentElement.style.setProperty("--name", value) to update a variable.

You cannot use them as media query conditions, but you can redefine variable values inside a media query.

Check spelling, scope, inheritance, and whether the final value is valid for that CSS property.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.