DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
← From Beginner to Expert in Fundamentals of Frontend
  • The Box Model
  • Specificity & The Cascade
  • CSS Units Deep Dive
  • Stacking Contexts & Z-Index
  • Flexbox Mastery
  • CSS Grid Mastery
  • Centering, Subgrid & Container Queries
  • CSS Custom Properties & Variables
  • Selectors: :has(), Pseudo-classes & Pseudo-elements
  • Responsive Functions: calc(), clamp(), min(), max()
  • Media Queries & Mobile-First Design
  • Transitions & Animations
  • Transforms, Filters & Clipping
  • CSS Methodologies: BEM, CSS-in-JS & Tailwind
  • Typography, Color Spaces & Dark Mode
  • CSS Performance & Optimization
  • CSS Interview Questions — Part 1
  • CSS Interview Questions — Part 2
← From Beginner to Expert in Fundamentals of Frontend

CSS Custom Properties & Variables

16 min read
CSS VariablesCustom PropertiesThemingDark Mode:rootvar()JavaScript Integration

The Power of CSS Variables

CSS Custom Properties (variables) are not just a convenience — they are a runtime feature that can be read and updated with JavaScript, cascade through the DOM, and enable theming systems.

CSS Variables vs Sass Variables

FeatureCSS VariablesSass Variables
When resolvedRuntime (browser)Compile time (build)
Can change dynamically✅ Yes (JS, media queries)❌ No
Cascade/inherit✅ Yes❌ No
ScopeDOM tree (CSS selectors)File/block scope
Fallback values✅ var(--x, fallback)N/A
PerformanceTiny overheadZero (compiled away)

Scoping

Variables cascade and inherit just like regular CSS properties. Define them on :root for global scope, or on specific selectors for component scope.

Dark Mode with CSS Variables

The most elegant dark mode implementation: define colors as variables, swap them with a selector or media query.

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.