CSS Variables Migrate Workflow
Migrate hardcoded CSS values to custom properties for consistent theming and easier maintenance.
Objective
Replace hardcoded colors, spacing, and fonts with CSS custom properties for theme support.
Environment
- A project with CSS files containing hardcoded values
- A design token file or variables file
- CSS custom property browser support
Steps
1. Identify hardcoded values
Search CSS files for hardcoded colors, font sizes, and spacing values.
Expected state: A list of all hardcoded values is compiled.
2. Create variables
Define CSS custom properties in a :root block for each unique value.
Expected state: A complete set of custom properties is defined.
3. Replace usages
Replace each hardcoded value in the CSS with the corresponding var() reference.
Expected state: All hardcoded values are replaced with var() calls.
4. Verify visually
Open the application and confirm that the visual appearance is unchanged.
Expected state: The application looks identical to before the migration.
Decision points
Group variables by type or by component?
- By type (colors, spacing)
- By component usage
Group by type for global tokens; create component-specific variables only when needed.
Expected outcome
All hardcoded CSS values are replaced with custom properties, and the visual appearance is unchanged.
Verification
Compare screenshots before and after migration to confirm no visual regressions.
Cleanup
No cleanup needed; the variables file should be committed.
Variables enable theming
CSS custom properties make it trivial to support dark mode or rebrand without finding and replacing hardcoded values.
:root { --color-primary: #0066cc; --spacing-unit: 8px; }
.button { color: var(--color-primary); padding: var(--spacing-unit); }
Implementation Notes
This workflow provides a structured approach to ensure consistent results across team members and environments.
Common Pitfalls
Skipping verification steps or rushing through the process can lead to incomplete results. Always follow each step in order and verify before proceeding.