Rules enforcing best practices and consistency using Tailwind CSS v2.1.2
🎉 Since v1.5.0, the plugin will parse the
tailwind.config.jsfile and use the correct values based on your own settings.👍 Most of the new JIT mode features are also supported.
-
FIX: Formating HTML files is now possible using
@angular-eslint/template-parser -
FIX: New default for
cssFilesoption used byno-custom-classname -
Add
cssFilesRefreshRateoption forno-custom-classnamerule -
Handling
Literalattribute values (e.g.className={'...'}mind the curly brackets) instead of ignoring them -
FIX: issue with trim and
TemplateLiteralcausing unwanted concatenation of classnames -
Support for per-side border-color in JIT
-
Better support for JIT mode arbitrary values
-
Support for color opacity shorthand
Learn more about each supported rules by reading their documentation:
classnames-order: order classnames by target properties then by variants ([size:][theme:][state:])no-custom-classname: only allow classnames from Tailwind CSS and the values from thewhitelistoptionno-contradicting-classname: e.g. avoidp-2 p-3, different Tailwind CSS classnames (pt-2&pt-3) but targeting the same property several times for the same variant.
Using ESLint extension for Visual Studio Code, you will get these messages

You can can the same information on your favorite command line software as well.
You'll first need to install ESLint:
$ npm i eslint --save-dev
Next, install eslint-plugin-tailwindcss:
$ npm i eslint-plugin-tailwindcss --save-dev
Add tailwindcss to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:
{
"plugins": ["tailwindcss"]
}Use our preset to get reasonable defaults:
"extends": [
"plugin:tailwindcss/recommended"
]
If you do not use a preset you will need to specify individual rules and add extra configuration:
Configure the rules you want to use under the rules section.
The following lines are matching the configuration saved in the
recommendedpreset...
{
"rules": {
"tailwindcss/classnames-order": "warn",
"tailwindcss/no-custom-classname": "warn",
"tailwindcss/no-contradicting-classname": "error"
}
}Learn more about Configuring Rules in ESLint.
Most rules shares the same settings, instead of duplicating some options...
You should also specify settings that will be shared across all the plugin rules. More about eslint shared settings.
All these settings have nice default values that are explained in each rules' documentation. I'm listing them in the code below just to show them.
{
"settings": {
"tailwindcss": {
// These are the default values but feel free to customize
"callees": ["classnames", "clsx", "ctl"],
"config": "tailwind.config.js",
"cssFiles": ["**/*.css", "!**/node_modules", "!**/.*", "!**/dist", "!**/build"],
"groupByResponsive": false,
"groups": defaultGroups, // imported from groups.js
"prependCustom": false,
"removeDuplicates": true,
"whitelist": []
}
}
}The plugin will look for each setting value in this order and stop looking as soon as it finds the settings:
- In the rule option argument (rule level)
- In the shared settings (plugin level)
- Default value of the requested setting (plugin level)...
-
no-redundant-variant: e.g. avoidmx-5 sm:mx-5, no need to redefinemxinsm:variant as it uses the same value (5) -
only-valid-arbitrary-values:- e.g. avoid
top-[42], only0value can be unitless. - e.g. avoid
text-[rgba(10%,20%,30,50%)], can't mix%and0-255.
- e.g. avoid
I wrote this plugin after searching for existing tools which perform the same task but didn't satisfied my needs:
- eslint-plugin-tailwind, not bad but no support (yet) for variants sorting
- Headwind, only works within Visual Studio Code
You are welcome to contribute to this project by reporting issues, feature requests or even opening Pull Requests.
Learn more about contributing to ESLint-plugin-TailwindCSS.
