What is stylelint?
Stylelint is a powerful, modern linter that helps you avoid errors and enforce consistent conventions in your stylesheets. It is configurable and supports the latest CSS syntax as well as CSS-like syntaxes, such as SCSS.
What are stylelint's main functionalities?
Linting CSS files
This command will lint all CSS files in the 'src' directory and its subdirectories. It will check for errors and code quality issues based on the rules defined in the configuration.
"stylelint 'src/**/*.css'"
Fixing CSS files
This command will not only lint the CSS files but also attempt to fix any fixable issues, such as formatting inconsistencies, automatically.
"stylelint 'src/**/*.css' --fix"
Custom configuration
This JSON represents a custom Stylelint configuration object where specific rules are defined, such as disallowing invalid hex colors, setting indentation preferences, and enforcing no leading zero for numbers.
{ "rules": { "color-no-invalid-hex": true, "indentation": [2, { "except": ["block"] }], "number-leading-zero": "never" } }
Extending configurations
This JSON represents a Stylelint configuration that extends a shared configuration, in this case, 'stylelint-config-standard', which is a popular set of rules that enforce common stylistic conventions.
{ "extends": "stylelint-config-standard" }
Using plugins
This JSON represents a Stylelint configuration that includes a plugin, 'stylelint-scss', which adds SCSS-specific linting rules to Stylelint. The configuration then enables a rule from that plugin to disallow unknown at-rules in SCSS.
{ "plugins": ["stylelint-scss"], "rules": { "scss/at-rule-no-unknown": true } }
Other packages similar to stylelint
eslint
ESLint is a static code analysis tool for identifying problematic patterns in JavaScript code. It is similar to Stylelint in its extensibility and plugin ecosystem but is focused on JavaScript rather than stylesheets.
prettier
Prettier is an opinionated code formatter that supports many languages, including CSS. Unlike Stylelint, which can both lint and fix code, Prettier is solely focused on code formatting and does not provide linting functionalities.
sass-lint
Sass-lint is a node-only Sass linter for both sass and scss syntax. It is similar to Stylelint when working with SCSS files but does not support plain CSS or other CSS-like syntaxes.
csslint
CSSLint is a tool that helps to point out problems with your CSS code. It is less configurable than Stylelint and has a smaller set of rules and plugins, but it serves a similar purpose in linting CSS files.
stylelint
A mighty, modern CSS linter that helps you enforce consistent conventions and avoid errors in your stylesheets.
Upgrading? Please read our CHANGELOG to learn what changes to expect in the latest version, whether that's new features, bug fixes, renamed rules, or whatever else.
Features
- Over one hundred and thirty rules: Including those that:
- catch errors: e.g. invalid hex colors, indistinguishable colors, or overriding shorthand properties.
- enforce best practices: e.g. keeping specificity low or disallowing vendor prefixes in your source code.
- control what languages features can be used: e.g. whitelisting specific units, properties and functions, or disallowing certain selector types.
- enforce code style conventions: e.g. checking the spacing around the colon in declarations or specifying patterns for class selectors.
- Support for the latest CSS syntax: Including custom properties, range context for media features, calc() and nesting.
- Understands CSS-like syntaxes: The linter is powered by PostCSS, so it understands any syntax that PostCSS can parse, including SCSS, SugarSS, and experimental support for Less.
- Completely unopinionated: Only enable the rules you want, and configure them with options that tailor the linter to your needs.
- Automatically fix some stylistic warnings: By using stylefmt which supports stylelint configuration files.
- Shareable configs: If you don't want to craft your own config, you can extend a shareable config.
- Support for plugins: It's easy to create your own rules and add them to the linter.
- Options validator: So that you can be confident that your config is valid.
Example output
Quick start
With stylelint, it's easy to start linting your CSS:
- Decide how you want to use stylelint:
- Create your configuration object (probably as a
.stylelintrc
file) by either crafting your own config or extending a pre-written, shared config.
- If you want to use a pre-written config, just find one and extend it. We recommend trying
stylelint-config-standard
, which includes around 60 of stylelint's rules with sensible defaults. (You can always override specific rules after extending a config.) - To craft your own from the ground up, learn about rules. All of the rules are off by default, so you only have to learn about the rules you want to turn on and enforce. That way you can start small, growing your config over time as you have a chance to explore more of the rules. Alternately, copy-paste this example configuration, which lists all of stylelint's rules and their primary options, then remove (or turn off) the rules you don't want and edit the primary option of each rule to your liking.
- Lint!
Be sure to specify the syntax if you're using non-standard syntax (e.g. SCSS, SugarSS or Less).
Guides
You'll find more detailed information on using stylelint and tailoring it to your needs in our guides:
- User guide - Usage, configuration, FAQ and complementary tools.
- Developer guide - Contributing to stylelint and writing your own plugins & formatters.
Help out
There is always a lot of work to do, and already well over 100 rules to maintain. So please help out in any way that you can:
- Create, enhance, and debug rules (see our guide to "Working on rules").
- Improve documentation.
- Chime in on any open issue or pull request.
- Open new issues about your ideas for making stylelint better, and pull requests to show us how your idea works.
- Add new tests to absolutely anything.
- Work on improving performance of rules.
- Create or contribute to ecosystem tools, like the plugins for Atom and Sublime Text.
- Spread the word.
We communicate via issues and pull requests.
There is also stackoverflow, which is our preferred QA forum. Tag your post with "stylelint".
Important documents