Custom Properties vs Preprocessor Variables
You'll learn: When to choose CSS custom properties over Sass/Less variables based on whether you need flexibility at runtime or just convenience during development.
The Core Difference
CSS custom properties and preprocessor variables (like Sass $variables or Less @variables) might look similar, but they work at fundamentally different times:
Preprocessor variables are compile-time only. When Sass or Less processes your code, it replaces all variable references with their actual values, then outputs plain CSS. Once your CSS file reaches the browser, those variables are gone—baked into fixed values.
Custom properties are runtime values. They exist in the browser as living, changeable properties that respond to cascade, inheritance, and JavaScript manipulation in real-time.
When to Use Each
Use preprocessor variables when:
- You need loops, conditionals, or complex calculations during build time
- You're defining values that truly never change (like a breakpoint system)
- You want to compute values before the CSS even ships
Use CSS custom properties when:
- You need JavaScript to update styles dynamically (dark mode toggles, theme switching)
- You want values to respond to media queries or container queries
- You need inheritance and scoping in the cascade
- You want one value change to update multiple dependent properties instantly in the browser
The Analogy
Preprocessor variables are like a recipe card you follow once while cooking—helpful for organizing your process, but the dish doesn't remember the recipe. Custom properties are like adjustable knobs on the stove—you can turn them up or down while cooking, changing the result in real-time.
Key Takeaway: Use preprocessor variables for build-time convenience and computation; use CSS custom properties for runtime flexibility, theming, and dynamic updates.