CSS to Token Migration — Converting Hardcoded Values to Design Tokens
Migrate from hardcoded CSS values to a design token system. Learn strategies for auditing existing styles, extracting tokens, and refactoring components.
Detailed Explanation
Migrating from CSS to Design Tokens
Most projects start with hardcoded values scattered across stylesheets. Migrating to design tokens is a multi-step process that can be done incrementally.
Step 1: Audit Existing Values
Scan your codebase for hardcoded values. Look for:
- Colors: Hex codes, RGB values, named colors
- Spacing: Pixel values in margin, padding, gap
- Font sizes: Arbitrary px/rem values
- Border radii: Mixed corner values
- Shadows: Inline box-shadow declarations
Step 2: Group and Deduplicate
You'll likely find many near-duplicates:
#333333 used 47 times
#323232 used 12 times -> consolidate to one token
#2d2d2d used 8 times
Step 3: Define Token Names
Map groups to semantic names:
/* Before */
.header { color: #333333; }
.sidebar { color: #333333; }
.footer { color: #323232; }
/* After */
:root { --colors-text: #333333; }
.header { color: var(--colors-text); }
.sidebar { color: var(--colors-text); }
.footer { color: var(--colors-text); }
Step 4: Incremental Rollout
Don't refactor everything at once. Migrate category by category:
- Colors first (most impactful, easiest to audit)
- Spacing (second most common hardcoded values)
- Typography (font sizes, weights, line heights)
- Borders and shadows (least frequent)
Step 5: Enforce Token Usage
Add lint rules to prevent new hardcoded values:
- stylelint: Ban hex codes outside
:root - ESLint: Flag inline style objects with raw color values
- PR reviews: Check for token compliance
Common Pitfalls
- Too many tokens: Start with 30-50, not 300
- Too-specific names:
--header-text-coloris too narrow;--colors-textis better - Forgetting dark mode: Plan token pairs from the start
Use Case
Use this migration guide when refactoring an existing project from hardcoded CSS values to a systematic design token approach, especially before adding features like dark mode or multi-brand theming.
Try It — Design Tokens Generator
Related Topics
Dark Mode Token System — Light and Dark Theme Token Architecture
System Tokens
Material Design Token System — Complete Color and Spacing Tokens
Color Tokens
Spacing Scale System — Consistent Margin and Padding Tokens
Spacing Tokens
Typography Token Scale — Font Size, Weight, and Line Height Tokens
Typography Tokens