CSS-28Recommended
Check the built CSS for limits and unused selectors
Run the style linter on the CSS the build produces as well as on the source. Fail the build when a class or attribute selector matches no element in any built page. The only exceptions sit on one list of classes that scripts add.
How to do it
Nesting, mixins and preprocessors change selectors on the way out. A rule nested three levels deep in the source becomes .order-card .order-card-body .order-card-price in the output, at 0,3,0. Running the linter on dist/ with the same limits catches what the source hides, and it also covers Declare the layer order once. Dead CSS makes every later change slower, because nobody dares delete what they cannot trace. Collect every class used in the built HTML and the templates, and compare them with the selectors in the built CSS. Keep the exception list in one file and give each entry its reason. The check that one class styles one kind of element is in Fence each component's styles in.
Examples
JavaScript example
import { readFileSync, readdirSync } from 'node:fs';
const ALLOWED_UNUSED = new Set(['basket-toast']); // added by basket.js after a purchase
const pages = readdirSync('dist').filter(f => f.endsWith('.html')).map(f => readFileSync(`dist/${f}`, 'utf8')).join('\n');
const used = new Set([...pages.matchAll(/class="([^"]+)"/g)].flatMap(([, list]) => list.split(/\s+/)));
const styled = new Set([...readFileSync('dist/main.css', 'utf8').matchAll(/\.([a-z][\w-]*)/g)].map(([, name]) => name));
const unused = [...styled].filter(name => !used.has(name) && !ALLOWED_UNUSED.has(name));
unused.forEach(name => console.log(`FAIL .${name} matches no element`));
process.exit(unused.length ? 1 : 0);
Python example
import pathlib, re, sys
ALLOWED_UNUSED = {"basket-toast"} # added by basket.js after a purchase
pages = "\n".join(p.read_text() for p in pathlib.Path("dist").glob("*.html"))
used = {name for group in re.findall(r'class="([^"]+)"', pages) for name in group.split()}
styled = set(re.findall(r"\.([a-z][\w-]*)", pathlib.Path("dist/main.css").read_text()))
unused = sorted(styled - used - ALLOWED_UNUSED)
for name in unused:
print(f"FAIL .{name} matches no element")
sys.exit(1 if unused else 0)
JSON example
{
"scripts": {
"lint:css:built": "stylelint \"dist/**/*.css\" --config stylelint.config.mjs --max-warnings 0"
}
}
How to check
The build runs the style linter on the output file in dist/ with the same config as the source and a warning limit of 0. It reports 0 classes that appear in the built CSS but in no built page and not on the exception list. Adding .unused-test { } to a component file stops the build.
Related rules
- CSS-33 Nest only states and queries one level deep in HTML and CSS
- CSS-6 Declare the layer order once in HTML and CSS
- CSS-10 Fence each component's styles in in HTML and CSS
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/html-css/checks-in-code/unused-selectors">Agent Wranglers HTML and CSS playbook</a>. In code you write, add a comment next to it with the same link.