Full answer
A full rewrite of a large stylesheet is slow and risky, because nothing ships until it is finished, and the visual differences are hard to find. An incremental plan keeps the site working at every step.
A practical plan:
- Measure first. Collect the size, the number of rules, unused rules, the highest specificity values and the count of
!important. Use the coverage tool in the browser and a script overdocument.styleSheets. Write down the goals, for example fewer overrides and smaller files. - Protect the look. Add visual regression tests for the most important pages and components before changing anything, so every step can be compared.
- Contain the legacy code. Put the old CSS into a low layer, for example
@layer legacy. Because layer order beats specificity, new rules in a later layer win over old rules that use long, strong selectors, with no!important. Declare the layer order once at the top. - Introduce tokens. Add custom properties for colours, spacing and type, and let new components use them. Old rules can start reading tokens too, one at a time.
- Migrate by component. Pick one component, write it in a new layer, switch the markup to use it, check the visual tests, and delete the old rules for it. Start with the most reused and the most painful parts.
- Stop the flow of new problems. Add a linter, a rule that forbids ids in selectors and
!important, and a review rule that new code goes into the new layers. - Remove dead code regularly, and track the numbers from step 1.
Risks to plan for:
- Unlayered CSS beats all layered CSS. The old CSS must be moved into a layer, or be the only unlayered CSS on purpose. A third-party stylesheet that is not layered can also win, so wrap it with
@import url() layer(vendor)or a@layerblock. - Rules inside a layer keep their own specificity battles, so a very strong legacy rule still beats a weak legacy rule in the same layer.
!importantreverses the layer order, so an important rule inlegacybeats an important rule in a later layer.
In the first case, the old rule is more specific, but it is in the earlier layer, so the new blue rule wins. In the second case, the old rule is not in any layer, so it beats the layered component and the button stays gray. This is why step 3 matters.
Report progress with numbers, so the team can see the migration working.
<style>
@layer legacy, components;
@layer legacy { #page .btn.btn-primary { background: gray; } }
@layer components { .btn { background: #0a58ca; color: #fff; } }
#page2 .btn.btn-primary { background: gray; }
</style>
<div id="page"><button class="btn btn-primary" id="a" type="button">Layered legacy</button></div>
<div id="page2"><button class="btn btn-primary" id="b" type="button">Unlayered legacy</button></div>Checked in Chrome 156 only. No real legacy stylesheet or visual regression tool was run. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Do not rewrite everything at once.
- Measure and add visual tests first.
- Put legacy CSS in a low layer; new layers win.
- Migrate one component at a time and delete the old rules.
- Prevent new problems with linting and review rules.
Common mistakes
- Starting a full rewrite with no visual tests.
- Leaving legacy CSS unlayered, so it beats the new layers.
- Not stopping new legacy-style code during the migration.
Follow-ups
- Why does unlayered CSS beat layered CSS?
- How do you measure progress?
Tips
- Give the steps in order: measure, protect, contain, tokens, migrate, prevent, remove.
- Explain why the layer for legacy code makes the migration safe.