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 linter that helps you avoid errors and enforce conventions in your styles.
Features
It's mighty because it:
- understands the latest CSS syntax including custom properties and level 4 selectors
- extracts embedded styles from HTML, markdown and CSS-in-JS object & template literals
- parses CSS-like syntaxes like SCSS, Sass, Less and SugarSS
- has over 170 built-in rules to catch errors, apply limits and enforce stylistic conventions
- supports plugins so you can create your own rules or make use of plugins written by the community
- automatically fixes some violations (experimental feature)
- is well tested with over 10000 unit tests
- supports shareable configs that you can extend or create your own of
- is unopinionated so you can tailor the linter to your exact needs
- has a growing community and is used by Facebook, GitHub and WordPress
Example output
Getting started
It's easy to get started.
First, decide how you want to use stylelint:
Then create your configuration object. You can either extend a shared configuration or craft your own.
Extend a shared configuration
This is the quickest way to get started. We suggest you extend either:
The recommended config turns on just the possible error rules. The standard config extends it by turning on 60 stylistic rules. We suggest you extend the:
- recommended config if you use a pretty printer like prettier
- standard config if you want stylelint to enforce stylistic conventions
You may want to add rules to your config that limit language features as these will be specific to your team and/or project.
If you use language extensions, for example @if
and @extends
, you can use a community config like stylelint-config-recommended-scss
instead.
Craft your own config
Alternatively, you can learn about the rules and then either:
Guides
You'll find detailed information on customising stylelint in our guides:
Need help?
Read our FAQ first.
If the answer to your problem isn't there, then post it on stackoverflow.
Create a new issue if:
- you think you've found a bug
- you have a feature request
If you're upgrading, read our CHANGELOG to learn what changes to expect in the latest version.
Help out
To help out, you can:
Our VISION document guides our work.
Semantic Versioning Policy
We have a semantic versioning policy. Any minor update may report more errors than the previous release. As such, we recommend using the tilde (~
) in package.json
e.g. "stylelint": "~7.2.0"
to guarantee the results of your builds.
License
The MIT License.
Contributors
This project exists thanks to all these people. Contribute.
Backers
Thank you to all our backers! Become a backer.
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. Become a sponsor.