Sub topic 12
Refactoring and removing dead CSS safely
This chapter assumes you know BEM naming, cascade layers with @layer, and a basic Stylelint setup from earlier chapters. You should also be comfortable running commands in a terminal.
Why deleting CSS is risky
CSS has no compiler that tells you a rule is unused. A selector can match elements you never see in the page you are testing. Class names may be built in JavaScript ("card--" + status). Markup may come from a CMS, an email template, or another team's repository. Deleting a rule that looks unused can break a page you did not open.
Three kinds of dead CSS
| Kind | What it means | How to find it |
|---|---|---|
| Unused | No element ever matches the selector | DevTools Coverage panel, a PurgeCSS report |
| Overridden | The selector matches, but every declaration loses to another rule | DevTools "Styles" pane, Stylelint duplicate rules |
| Redundant | The declaration repeats a value that is already inherited or default | Code review, Stylelint |
Unused CSS is the riskiest to remove because proving a negative is hard. Overridden and redundant CSS can be checked by reading the cascade, so they are the safer place to start.
A removal workflow
- Add a safety net. Run Stylelint in CI and, if you can, visual regression screenshots of key pages.
- Find candidates. Record Coverage while you click through real states, such as open menus, error messages, and logged-in views.
- Search the whole codebase. Check HTML, templates, and scripts. Search for the stable part of a dynamic name too.
- Stage the deletion. Do not remove the rule yet. Isolate it first.
- Delete in a separate commit. Then a revert is one step.
Search for both the full class and its prefix:
grep -rn --include="*.html" --include="*.js" --include="*.jsx" --include="*.vue" "card--" src/Isolating legacy code with a layer
If you have a layer order, move suspect rules into a low-priority legacy layer. Everything in later layers now wins over it, so you can see what breaks without touching the selectors.
@layer legacy, components, utilities;
@layer legacy {
.old-btn {
padding: 4px 8px;
}
}
@layer components {
.button {
padding: 0.5rem 1rem;
}
}If nothing visibly changes after the move and nothing uses .old-btn, the rule is a good candidate for deletion.
Catching overridden and redundant rules
Stylelint can flag rules that repeat themselves:
{
"extends": "stylelint-config-standard",
"rules": {
"no-duplicate-selectors": true,
"declaration-block-no-duplicate-properties": true,
"block-no-empty": true
}
}Refactoring a class name
Rename in three steps so no page is ever unstyled:
- Add the new class beside the old one in the CSS, with the same declarations.
- Update the markup to the new class, then confirm nothing uses the old one.
- Remove the old selector.
Common errors
- Deleting a class that JavaScript adds at runtime, such as
is-open. - Removing a rule in the same commit as unrelated changes, which makes reverts hard.
- Running an automatic purge tool without a safelist for dynamic class names.
- Assuming a rule is dead because it is overridden on one page, when it still wins on another.
Exercise
A grep for .alert--warning finds nothing in your templates, but the codebase contains the JavaScript line el.className = "alert alert--" + level;. Is it safe to delete .alert--warning? What should you do?
Show answer
No. The class is built at runtime from level, so a search for the full name misses it. Search for the prefix alert--, find the possible values of level, and check whether "warning" is one of them. If you cannot confirm it is unused, move the rule to a legacy layer or mark it deprecated, ship, and delete it later in a separate commit.