CSS custom properties (variables) have transformed how we manage colors in design systems. Gone are the days of find-and-replace across hundreds of files. Modern color systems are dynamic, themeable, and maintainable—if you structure them correctly.
Benefits of CSS Custom Properties
- Maintainability: Change colors in one place, update everywhere
- Theming: Switch between light/dark mode with ease
- Runtime Updates: Change colors with JavaScript without recompiling
- Inheritance: Cascade values through your component tree
- calc() Support: Perform calculations on color properties
Naming Conventions
Your naming strategy determines how maintainable your system will be. There are two main approaches:
1. Semantic Names (Recommended)
Name variables based on their purpose, not their value:
2. Literal Names
Name variables after their color value (less flexible):
Pro Tip: Use both! Define literal color scales, then reference them with semantic names.
Two-Tier System
The best approach combines both strategies:
Dark Mode Implementation
CSS variables make dark mode trivial to implement:
Complete Design System Example
Organizing Your Variables
Structure matters for maintainability. Here's a recommended organization:
- Primitive Colors: Raw color values (scales)
- Semantic Tokens: Purpose-based references
- Component Tokens: Component-specific overrides
JavaScript Integration
CSS variables can be read and modified with JavaScript:
Performance Considerations
- CSS variables have minimal performance impact
- They're faster than inline styles or className changes
- Browser support is excellent (IE11 is the only exception)
- No build step required—they work in vanilla CSS
Best Practices
- Always define variables in :root for global access
- Use semantic names for consumer-facing variables
- Document your color system in code comments
- Create a visual style guide alongside code
- Test all color combinations for accessibility
- Version your design tokens
- Keep primitive and semantic tokens separate
Conclusion
CSS custom properties are the foundation of modern, maintainable color systems. By combining literal color scales with semantic naming, you create flexible systems that adapt to theming, scale with your product, and remain easy to maintain as your team grows.
Start with a solid structure, document your decisions, and your future self (and team) will thank you.