Configuring
Stylelint expects a configuration object, and looks for one in a:
stylelint.config.js
or.stylelintrc.js
file- Which module system to use depends on your default module system configuration for Node.js (e.g.,
"type": "module"
inpackage.json
).
- Which module system to use depends on your default module system configuration for Node.js (e.g.,
stylelint.config.mjs
or.stylelintrc.mjs
file usingexport default
(ES module)stylelint.config.cjs
or.stylelintrc.cjs
file usingmodule.exports
(CommonJS).stylelintrc.json
,.stylelintrc.yml
, or.stylelintrc.yaml
file.stylelintrc
file in JSON or YAML format- We recommend adding an extension (e.g.,
.json
) to help your editor provide syntax checking and highlighting.
- We recommend adding an extension (e.g.,
stylelint
property inpackage.json
ES module example:
/** @type {import('stylelint').Config} */
export default {
rules: {
"block-no-empty": true
}
};
The @type
JSDoc annotation enables Typescript to autocomplete and type-check.
CommonJS example:
module.exports = {
rules: {
"block-no-empty": true
}
};
JSON example:
{
"rules": {
"block-no-empty": true
}
}
Starting from the current working directory, Stylelint stops searching when one of these is found. Alternatively, you can use the --config
or configFile
option to short-circuit the search.
The configuration object has the following properties:
rules
Rules determine what the linter looks for and complains about. There are over 100 rules built into Stylelint. No rules are turned on by default.
The rules
property is an object whose keys are rule names and values are rule configurations. For example:
{
"rules": {
"color-no-invalid-hex": true
}
}
Each rule configuration fits one of the following formats:
null
(to turn the rule off)- a single value (the primary option)
- an array with two values (
[primary option, secondary options]
)
Specifying a primary option turns on a rule.
Many rules provide secondary options for further customization. To set secondary options, use a two-member array. For example:
{
"rules": {
"selector-pseudo-class-no-unknown": [
true,
{
"ignorePseudoClasses": ["global"]
}
]
}
}
You can add any number of keys to the object. For example, you can:
- turn off
block-no-empty
- turn on
unit-allowed-list
with a primary option - turn on
alpha-value-notation
with a primary and secondary option
{
"rules": {
"block-no-empty": null,
"unit-allowed-list": ["em", "rem", "%", "s"],
"alpha-value-notation": ["percentage", { "exceptProperties": ["opacity"] }]
}
}
Some rules and options accept regex. You can enforce these common cases:
- kebab-case:
^([a-z][a-z0-9]*)(-[a-z0-9]+)*$
- lowerCamelCase:
^[a-z][a-zA-Z0-9]+$
- snake_case:
^([a-z][a-z0-9]*)(_[a-z0-9]+)*$
- UpperCamelCase:
^[A-Z][a-zA-Z0-9]+$
Or enforce a prefix using a positive lookbehind regex. For example, (?<=foo-)
to prefix with foo-
.
disableFix
You can set the disableFix
secondary option to disable autofix on a per-rule basis.
For example:
{
"rules": {
"color-function-notation": ["modern", { "disableFix": true }]
}
}
message
You can use the message
secondary option to deliver a custom message when a rule is violated.
For example, the following rule configuration would substitute in custom messages:
{
"rules": {
"custom-property-pattern": [
"^([a-z][a-z0-9]*)(-[a-z0-9]+)*$",
{
"message": "Expected custom property name to be kebab-case"
}
]
}
}
Alternately, you can write a custom formatter for maximum control if you need serious customization.
Experimental feature: some rules support message arguments. For example, when configuring the color-no-hex
rule, the hex color can be used in the message string:
.stylelintrc.js
:
{
'color-no-hex': [true, {
message: (hex) => `Don't use hex colors like "${hex}"`,
}]
}
.stylelintrc.json
:
{
"color-no-hex": [true, {
"message": "Don't use hex colors like \"%s\""
}]
}