JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionscss
PrevNext

Learn the concept

CSS Custom Properties (Variables)

css
mid
custom-properties

What are CSS custom properties (variables) and how do they differ from Sass variables?

custom-properties
variables
theming
dark-mode
sass
Quick Answer

CSS custom properties (--my-var) are native CSS variables that cascade, can be scoped to selectors, updated at runtime with JavaScript, and inherited by child elements. Sass variables ($my-var) are compile-time only and produce static output. CSS variables enable theming, dark mode, and dynamic styling without preprocessors.

Detailed Explanation

CSS Custom Properties:\n- Declared with -- prefix: --color-primary: #3b82f6\n- Used with var(): color: var(--color-primary)\n- Cascade and inherit like regular CSS properties\n- Can be scoped to any selector\n- Can be updated at runtime with JavaScript\n- Support fallback values: var(--color, #000)\n\nvs Sass Variables:\n| Feature | CSS Variables | Sass Variables |\n|---------|---------------|---------------|\n| Runtime | Yes (dynamic) | No (compile-time) |\n| Cascade | Yes | No |\n| Scope | Any selector | Block scope |\n| JS access | Yes | No |\n| Media queries | Yes | No |\n| Browser support | Modern browsers | Any (compiles to CSS) |\n\nUse cases:\n- Theming (light/dark mode)\n- Component variants\n- Responsive values via media queries\n- JavaScript-driven dynamic styles

Code Examples

CSS variables for themingCSS
/* Define variables on :root (global) */
:root {
  --color-primary: #3b82f6;
  --color-bg: #ffffff;
  --color-text: #111827;
  --spacing-md: 1rem;
  --radius: 8px;
}

/* Dark mode override */
[data-theme="dark"] {
  --color-primary: #60a5fa;
  --color-bg: #111827;
  --color-text: #f9fafb;
}

/* Usage */
.card {
  background: var(--color-bg);
  color: var(--color-text);
  border-radius: var(--radius);
  padding: var(--spacing-md);
}

.button {
  background: var(--color-primary);
  /* Fallback value */
  border-color: var(--border-color, transparent);
}

Real-World Applications

Use Cases

Implementing dynamic theming in a web application

Allowing users to switch between light and dark modes, or choose from a palette of primary colors, by simply changing a few CSS variable values.

Creating a reusable component library

Where components can adapt their styling (e.g., spacing, colors, font sizes) based on parent-defined CSS variables, providing flexibility and consistency.

Developing responsive designs with fluid typography and spacing

By defining scale factors or base units in CSS variables that can be dynamically updated via JavaScript or media queries to create a harmonious design system.

Mini Projects

Dynamic Theme Switcher

beginner

Build a web page with a button that toggles between light and dark themes using CSS custom properties.

Component-Based Card Design

intermediate

Create a flexible card component whose padding, border-radius, and colors are controlled by CSS variables, demonstrating easy customization.

Industry Examples

Bootstrap

Modern versions of Bootstrap extensively use CSS custom properties for theming, allowing developers to easily customize default styles without overriding complex Sass variables and recompiling.

Material Design

Google's Material Design system often leverages CSS variables to define and manage a consistent set of design tokens (colors, typography, spacing) across its components and applications.

Resources

MDN - CSS custom properties

docs

CSS-Tricks - A Complete Guide to Custom Properties

article

Related Questions

What are CSS selectors and how does specificity work?

junior
selectors
Previous
What is the difference between CSS transitions and animations?
Next
Explain the CSS Flexbox axis model and how to solve common layout challenges with it.
PrevNext