Migrating legacy CSS without breaking things
The "wrap and replace" pattern
You inherit a 5000-line legacy stylesheet. You want to migrate to @layer + tokens. You CAN'T rewrite it all at once.
Strategy:
@layer legacy, modern;
@layer legacy {
/* Wrap the entire 5000-line legacy file */
@import url("/legacy.css") layer(legacy);
}
@layer modern {
/* New code goes here. ALWAYS wins over legacy. */
.new-card { ... }
}The legacy layer is at the BOTTOM of priority. Any modern rule beats it without needing !important or higher specificity.
Replace incrementally
- Build a new component in
modernlayer. - Apply it to a single page.
- Verify visual parity.
- Apply to the next page.
- Eventually delete the legacy rules for that component.
Months later: legacy layer is empty. Delete the import.
Don't fight the cascade
If a legacy rule applies an unwanted style to your modern component, DON'T add !important or stack 7 classes. Just put it in the modern layer:
@layer modern {
.new-card { color: blue; } /* Beats any legacy color */
}When this doesn't work
- If legacy uses
!importantAND you can't edit it.!importantcuts ACROSS layers. You'd need a HIGHER-priority!important. - If legacy uses inline styles. Inline beats EVERYTHING except
!importanton a normal rule.
In both cases, edit the source if you can. If you can't, you're stuck doing CSS-fu the old way.
Sign up to start coding
Theory is open to everyone. The interactive editor, live preview, and check are unlocked with a 7-day free trial β card required, cancel anytime.
Sign up β free trial βFirst 10 lessons in each track are free. No card needed for those.