An ESLint plugin that provides rules to verify CSS definition objects.
npm install eslint-plugin-csseslint-plugin-css is an ESLint plugin that provides rules to verify CSS definition objects.









::: WORKING IN PROGRESS :::
This ESLint plugin provides linting rules to verify CSS definition objects.
- Find the wrong usage of CSS definition objects, and their hints.
- Support for Vue and JSX (React).
- Partial support for [styled-components] style objects.
You can check on the Online DEMO.


[styled-components]: https://styled-components.com/docs/advanced#style-objects
[Stylelint] partially supports CSS in JS, but some issues haven't been resolved for a long time.
Also, CSS definitions using template literals are similar to CSS syntax, but CSS definitions using JavaScript objects are not. ESLint may work better for linting JavaScript objects.
[Stylelint]: https://stylelint.io
See documents.
``bash`
npm install --save-dev eslint eslint-plugin-css
> Requirements
>
> - ESLint v7.0.0 and above
> - Node.js v12.22.x, v14.17.x, v16.x and above
Add css to the plugins section of your .eslintrc configuration file (you can omit the eslint-plugin- prefix)recommended
and either use one of the two configurations available ( or all) or configure the rules you want:
The plugin.configs["flat/recommended"] config enables a subset of the rules that should be most useful to most users.
See lib/configs/flat/recommended.ts for more details.
`js`
// eslint.config.js
import * as cssPlugin from "eslint-plugin-css"
export default [
cssPlugin.configs["flat/recommended"],
];
The plugin:css/recommended config enables a subset of the rules that should be most useful to most users.
See lib/configs/recommended.ts for more details.
`js`
// .eslintrc.js
module.exports = {
"plugins": [
"css"
],
"extends": [
// add more generic rulesets here, such as:
// 'eslint:recommended',
"plugin:css/recommended"
]
}
The plugin.configs["flat/standard"] config enables a subset of the rules and superset of plugin.configs["flat/recommended"] config that apply a subjective style.
See lib/configs/flat/standard.ts for more details.
`js`
// eslint.config.js
import * as cssPlugin from "eslint-plugin-css"
export default [
cssPlugin.configs["flat/standard"],
];
The plugin:css/standard config enables a subset of the rules and superset of plugin:css/recommended config that apply a subjective style.
See lib/configs/standard.ts for more details.
`js`
// .eslintrc.js
module.exports = {
"plugins": [
"css"
],
"extends": [
"plugin:css/standard"
]
}
Override/add specific rules configurations. See also: http://eslint.org/docs/user-guide/configuring.
`js`
// eslint.config.js
import * as cssPlugin from "eslint-plugin-css"
export default [
{
plugins: { css: cssPlugin },
rules: {
// Override/add rules settings here, such as:
"css/rule-name": "error"
}
}
];
`js`
// .eslintrc.js
module.exports = {
"plugins": [
"css"
],
"rules": {
// Override/add rules settings here, such as:
"css/rule-name": "error"
}
}
The plugin.configs["flat/all"] / plugin:css/all config enables all rules. It's meant for testing, not for production use because it changes with every minor and major version of the plugin. Use it at your own risk.
All CSS-related rules are applied to code that passes any of the following checks:
- style={ {} } JSX attribute expression
`jsx`
const jsx = style={ {/ JSX attribute expression /} }
/>
- v-bind:style="{}" Vue directive
`vue`
v-bind:style="{/ Vue directive /}"
/>
- CSS definition function call for styled-components
e.g.
`js
import styled, { css, createGlobalStyle } from 'styled-components'
styled.input({/ CSS /})
styled.input.attrs({})({/ CSS /})
css({/ CSS /})
createGlobalStyle({/ CSS /})
`
- According to settings.css.target settings.
However, if you want to take advantage of the rules in any of your custom objects that are CSS objects, you might need to use the special comment // @css that marks an object in the next line as a CSS object in any file, e.g.:
`js`
// @css
const myStyle = {
height: '100px'
}
The --fix option on the command line automatically fixes problems reported by rules which have a wrench :wrench: below. plugin:css/recommended
The rules with the following star :star: are included in the config and the plugin:css/standard config. plugin:css/standard
The rules with the following lipstick :lipstick: are included in the config.
| Rule ID | Description | |
|:--------|:------------|:---|
| css/no-dupe-properties | disallow duplicate properties | :star: |
| css/no-invalid-color-hex | disallow invalid hex colors | :star: |
| css/no-shorthand-property-overrides | disallow shorthand properties that override related longhand properties | :star: |
| css/no-unknown-property | disallow unknown properties | :star: |
| css/no-unknown-unit | disallow unknown units | :star: |
| Rule ID | Description | |
|:--------|:------------|:---|
| css/named-color | enforce named colors | :wrench: |
| css/no-length-zero-unit | disallow units for zero lengths | :lipstick::wrench: |
| css/no-useless-color-alpha | disallow unnecessary alpha-channel transparency value | :star::wrench: |
| css/prefer-reduce-shorthand-property-box-values | require reduction in box values of shorthand property | :lipstick::wrench: |
| Rule ID | Description | |
|:--------|:------------|:---|
| css/color-hex-style | enforce hex color style | :lipstick::wrench: |
| css/no-number-trailing-zeros | disallow trailing zeros in numbers. | :lipstick::wrench: |
| css/number-leading-zero | require or disallow a leading zero for fractional numbers less than 1 | :lipstick::wrench: |
| css/property-casing | enforce specific casing for CSS properties | :lipstick::wrench: |
See Settings.
Welcome contributing!
Please use GitHub's Issues/PRs.
See CONTRIBUTING.md.
- npm test runs tests and measures coverage.npm run update
- runs in order to update readme and configurations.npm run new [new rule name]
- runs to create the files needed for the new rule.npm run docs:watch` starts the website locally.
-
See the LICENSE file for license rights and limitations (MIT).