01. CSS Custom Properties (--variable-name, var(), Fallbacks & Scoping)

🟢 Beginner

1. Definition

CSS Custom Properties (CSS Variables) store reusable design tokens prefixed with -- (e.g. --primary-color: #2563eb) and retrieved via var(--primary-color, fallback), supporting DOM cascade inheritance, component scoping, and theme switching.

2. Why It Is Used

Custom Properties allow updating an entire site's color palette, spacing scale, or dark mode theme by modifying variable definitions at the :root level or container level.

3. How It Works

Unlike preprocessor variables (Sass/Less), CSS Custom Properties are dynamic and live in the DOM. JavaScript can read and write CSS variables in real time using element.style.setProperty('--name', value).

4. Syntax

:root {
  --primary-color: #2563eb;
  --card-padding: 1.5rem;
}

.card {
  background-color: var(--primary-color);
  padding: var(--card-padding, 20px);
}

5. Custom Properties vs Preprocessor Variables Table

FeatureCSS Custom PropertiesSass / Less Variables
DOM Cascade & InheritanceYes (Inherits down DOM tree)No (Compiled away at build)
Dynamic JS ManipulationYes (setProperty())No
Media Query ScopingYes (Can re-declare inside @media)No

6. Beginner Example

:root {
  --main-blue: #0284c7;
}

h1 {
  color: var(--main-blue);
}

7. Intermediate Example

/* Dark mode theme switching via variables */
:root {
  --bg-color: #ffffff;
  --text-color: #0f172a;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-color: #0f172a;
    --text-color: #f8fafc;
  }
}

8. Advanced Component Token Scoping Example

.btn {
  --btn-bg: #2563eb;
  background-color: var(--btn-bg);
}

.btn-danger {
  --btn-bg: #ef4444; /* Local variable override */
}

9. Line-by-Line Code Explanation

  • --btn-bg: #ef4444;: Overrides the local variable definition for danger buttons while preserving shared .btn layout rules.

10. Expected Terminal / Browser Output

A button component system where color themes update cleanly without duplicating layout declarations.

11. Common Mistakes & Pitfalls

  • Forgetting double hyphens: Writing var(primary-color) instead of var(--primary-color) is invalid.

12. Important Rules & Limitations

  • Variable names are case-sensitive (--primaryColor vs --primarycolor).

13. Multilingual Explanation: English

CSS Custom Properties store dynamic tokens using --variable-name and var(), inheriting through the DOM cascade.

14. Multilingual Explanation: Hindi (Roman Script)

CSS Variables ko --name se define aur var() se access karte hain. Dark mode themeing ke liye ye best hain.

15. Multilingual Explanation: Marathi (Roman Script)

CSS Variables --variable-name dwara tayar kele jaatat. Color themes change karnyasathi variables vapara.

16. Practice Questions

  1. Why are CSS Custom Properties superior to Sass variables for dark mode themeing?

17. Practical Coding Challenge

Declare global variables --brand-color and --brand-radius at :root level and apply them to a card component.

18. Related Topics