Agent Wranglers

CSS-26Recommended

Lint styles with one shared config

Run a style linter such as stylelint over every stylesheet in the build, with one config file in the repository. It rejects unknown and duplicate CSS, holds every exception with its reason, and fails the build on any error or warning.

How to do it

Put each limit from this playbook in the config once, such as the cap in Cap selector specificity and keep ids out of selectors. Turn on the rules for unknown properties, values, at-rules and type selectors, and for duplicate selectors and properties. The browser ignores a typo such as dispaly: grid without a sound, and a second .order-card block splits one component in two. Allow vendor prefixes only through the build tool. Write every exception as an override in the config, with a file path and a comment giving the reason. Inline disable comments spread and nobody can list them, so source files hold none. Make the linter report disable comments that are not needed or have no reason. Run it with a zero-warning limit in the same script as the tests, as Check every rule in code requires. Editors and AI agents read the same file, so they get the same answer as the build.

Examples

JavaScript example

// stylelint.config.mjs: one config, every limit and every exception
export default {
  extends: ['stylelint-config-standard'],
  reportNeedlessDisables: true,
  reportDescriptionlessDisables: true,
  rules: {
    'selector-max-specificity': '0,2,0',
    'selector-max-id': 0,
    'selector-class-pattern': '^[a-z]+(-[a-z0-9]+)*$',
    'declaration-no-important': true,
    'property-no-unknown': true,
    'declaration-property-value-no-unknown': true,
    'at-rule-no-unknown': true,
    'selector-type-no-unknown': true,
    'no-duplicate-selectors': true,
    'declaration-block-no-duplicate-properties': true,
    'property-no-vendor-prefix': true,
  },
  overrides: [
    {
      // Must win over every component, so it is the one allowed !important
      files: ['styles/utilities/visually-hidden.css'],
      rules: { 'declaration-no-important': null },
    },
  ],
};

JSON example

{
  "scripts": {
    "lint:css": "stylelint \"styles/**/*.css\" --max-warnings 0",
    "build": "npm run lint:css && vite build"
  }
}

Python example

# Build check: no linter is switched off inside a source file
import pathlib, re, sys
DISABLE = re.compile(r"(stylelint|html-validate)-disable")
hits = [str(p) for p in pathlib.Path("src").rglob("*") if p.suffix in {".css", ".html"} and DISABLE.search(p.read_text())]
print("\n".join(f"FAIL {hit} turns a linter off" for hit in hits))
sys.exit(1 if hits else 0)

CSS example

/* Each line is planted on purpose to prove the gate works. The build must stop. */
#header { }
.order-card { dispaly: grid; }
/* stylelint-disable-next-line selector-max-id */
#promo { }

How to check

The repository has exactly 1 style linter config, and it turns on every rule in the first example. The build runs the linter with a warning limit of 0. Adding #header { }, dispaly: grid or a second .order-card block to a component file stops the build. A search of the stylesheets and templates finds 0 stylelint-disable or html-validate-disable comments. Every override in the config names a file path, with its reason on the line above.

Sources

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/style-linter">Agent Wranglers HTML and CSS playbook</a>. In code you write, add a comment next to it with the same link.