Powerful ESLint rules for CSS
The rules work with the @eslint/css language plugin.
eslint-plugin-unicorn has more rules that also lint CSS, such as no-zero-fractions and no-shorthand-property-overrides.
We do not accept pull requests because of too much AI slop.
npm install --save-dev eslint @eslint/css eslint-cssicornRequires ESLint >=10.4, flat config, and ESM.
Use a preset config or configure each rule in eslint.config.js.
If you don't use a preset, set language: 'css/css' and add the @eslint/css plugin, as shown below.
import css from '@eslint/css';
import cssicorn from 'eslint-cssicorn';
import {defineConfig} from 'eslint/config';
export default defineConfig([
{
files: ['**/*.css'],
plugins: {
css,
cssicorn,
},
language: 'css/css',
rules: {
'cssicorn/prefer-short-hex-color': 'error',
'cssicorn/…': 'error',
},
},
// …
]);💼 Configurations enabled in.
✅ Set in the recommended configuration.
☑️ Set in the unopinionated configuration.
🔧 Automatically fixable by the --fix CLI option.
💡 Manually fixable by editor suggestions.
| Name | Description | 💼 | 🔧 | 💡 |
|---|---|---|---|---|
| lowercase | Enforce lowercase CSS syntax. | ✅ | 🔧 | |
| no-declarations-after-nested-rules | Disallow declarations after nested rules. | ✅ ☑️ | ||
| no-deprecated-features | Disallow deprecated CSS features. | ✅ | 🔧 | 💡 |
| no-descending-specificity | Disallow lower-specificity selectors from following higher-specificity selectors that set the same property. | |||
| no-duplicate-font-family-names | Disallow duplicate font family names. | ✅ ☑️ | 🔧 | |
| no-duplicate-properties | Disallow duplicate properties within CSS declaration blocks. | ✅ | 💡 | |
| no-duplicate-selectors | Disallow duplicate CSS selectors. | ✅ | 🔧 | |
| no-invalid-media-features | Disallow unknown media features and invalid values for known media features. | ✅ ☑️ | ||
| no-nesting-with-mixed-specificity | Disallow nesting under selector lists with mixed specificity. | ✅ | ||
| no-redundant-longhand-properties | Disallow longhand properties that can be combined into a shorthand. | ✅ | 🔧 | |
| no-redundant-nested-style-rules | Disallow nested style rules that do not modify the parent selector. | ✅ | 🔧 | |
| no-redundant-shorthand-values | Disallow redundant values in CSS shorthand properties. | ✅ | 🔧 | |
| no-self-referencing-custom-properties | Disallow self-references in CSS custom properties. | ✅ ☑️ | ||
| no-unknown-animations | Disallow unknown animations. | |||
| no-unknown-annotations | Disallow unknown and noncanonical CSS annotations. | ✅ ☑️ | 💡 | |
| no-unknown-pseudo-selectors | Disallow unknown pseudo-class and pseudo-element selectors. | ✅ ☑️ | ||
| no-unscoped-nesting-selector | Disallow unscoped CSS nesting selectors. | ✅ ☑️ | ||
| no-zero-length-unit | Disallow units on zero CSS lengths. | ✅ | 🔧 | |
| prefer-explicit-viewport-units | Prefer explicit viewport units. | ✅ | 💡 | |
| prefer-media-feature-range-syntax | Prefer modern media feature range syntax. | ✅ | 🔧 | 💡 |
| prefer-modern-syntax | Prefer modern CSS color and pseudo-element syntax. | ✅ | 🔧 | |
| prefer-short-hex-color | Prefer short hexadecimal color notation. | ✅ | 🔧 | |
| require-property-descriptors | Require descriptors in CSS @property rules. |
✅ ☑️ |
See the ESLint docs for more information about extending config files.
Note: The preset configs apply to **/*.css files. They also set language: 'css/css' and add the @eslint/css plugin.
This plugin exports a recommended config that enforces good practices.
import cssicorn from 'eslint-cssicorn';
import {defineConfig} from 'eslint/config';
export default defineConfig([
// …
cssicorn.configs.recommended,
{
files: ['**/*.css'],
rules: {
'cssicorn/no-unknown-animations': 'error',
},
},
]);This plugin exports an unopinionated config. It enables only the rules that catch bugs, without the style rules from the recommended config.
import cssicorn from 'eslint-cssicorn';
import {defineConfig} from 'eslint/config';
export default defineConfig([
// …
cssicorn.configs.unopinionated,
]);This plugin exports an all config that enables every rule.
import cssicorn from 'eslint-cssicorn';
import {defineConfig} from 'eslint/config';
export default defineConfig([
// …
cssicorn.configs.all,
{
files: ['**/*.css'],
rules: {
'cssicorn/prefer-short-hex-color': 'off',
},
},
]);- eslint-plugin-unicorn — More than 300 powerful ESLint rules. Some of them also lint CSS.
- eslint-node-test — ESLint rules for the Node.js built-in test runner.
- eslint-package-json — Powerful ESLint rules for
package.json.