Sub topic 11
Enforcing conventions with Stylelint
This chapter assumes you know CSS selectors and specificity, have read the chapters on BEM and design tokens, and can run npm scripts. Conventions that live only in a wiki erode quickly. A linter turns them into checks that fail a build or flag a line in your editor.
What Stylelint does
Stylelint is a linter for CSS. It parses your stylesheets and reports rules that your configuration forbids or requires. It catches syntax errors, risky patterns, and style drift. It can also fix some problems automatically.
Stylelint is not a formatter in the strict sense. Many teams pair it with Prettier for whitespace and use Stylelint for rules about meaning, such as which selectors are allowed.
Install and run it
npm install --save-dev stylelint stylelint-config-standard
npx stylelint "src/**/*.css"
npx stylelint "src/**/*.css" --fixThe first command adds the tool and a shared preset. The second lints every CSS file under src. The third also applies safe automatic fixes.
Configure the rules
Put a configuration in .stylelintrc.json at the project root. This one extends the standard preset and adds three rules that protect the low-specificity approach from earlier chapters.
{
"extends": "stylelint-config-standard",
"rules": {
"selector-max-id": 0,
"selector-max-specificity": "0,2,0",
"declaration-no-important": true
}
}Each rule does one job:
| Rule | Effect |
|---|---|
| `selector-max-id` set to `0` | Rejects any ID selector such as `#header`. |
| `selector-max-specificity` set to `"0,2,0"` | Rejects selectors that outweigh two classes. |
| `declaration-no-important` set to `true` | Rejects `!important`. |
The extends key loads a shared preset first. Entries in rules then override or add to it.
See a failure
This file breaks all three rules.
#nav .menu .item a {
color: red !important;
}Running npx stylelint on it reports the ID selector, the specificity above 0,2,0, and the !important. A BEM version fixes all three at once.
.nav__link {
color: red;
}If something truly needs to be overridden, use [cascade layers](#) order or a later layer, not a higher-specificity selector.
Enforce naming
You can check BEM names with selector-class-pattern. The pattern below accepts card, card__title, and card--featured.
{
"extends": "stylelint-config-standard",
"rules": {
"selector-class-pattern": [
"^[a-z][a-z0-9]*(-[a-z0-9]+)*(__[a-z0-9]+(-[a-z0-9]+)*)?(--[a-z0-9]+(-[a-z0-9]+)*)?$",
{
"message": "Use BEM names such as block, block__element or block--modifier."
}
]
}
}The preset's default class pattern expects kebab-case and rejects the double underscore, so you must replace it when you adopt BEM. The message option replaces the default error with advice your team can act on.
Where to run it
Linting helps most when it runs in more than one place.
- In the editor, through the Stylelint extension, so errors show as you type.
- In
package.json, as a script, so everyone uses the same command. - In a pre-commit hook or CI job, so nothing that fails reaches the main branch.
{
"scripts": {
"lint:css": "stylelint \"src/**/*.css\"",
"lint:css:fix": "stylelint \"src/**/*.css\" --fix"
}
}Common errors
- Linting generated files. Add build output and vendor folders to
ignoreFilesin the configuration, or to a.stylelintignorefile. - Rules that conflict with Prettier. If both tools format whitespace, they fight. Let Prettier own formatting and keep Stylelint on rules about meaning.
- Disabling rules inline too freely. A comment such as
/* stylelint-disable-next-line */is acceptable for a real exception. Name the rule and explain why, so reviewers can judge it. - Turning on every rule at once in an old codebase. Start with the rules above, fix what they report, then tighten.
Exercise
A teammate adds #sidebar .list .item span { margin: 0 !important; }. Which rules from this chapter's configuration report it, and what is the simplest way to rewrite it?
Show answer
selector-max-id reports #sidebar, selector-max-specificity reports a selector above 0,2,0, and declaration-no-important reports !important. Give the element a single BEM class such as .sidebar__item-label and set margin: 0 without !important. If an override is needed, place the rule in a later layer instead.