跳到主要内容

CSS 规则

提示

优先使用以下方案,减少 CSS 代码的编写:

  • 原子化 CSS:如Tailwind CSS,通过 utility 类直接在 HTML 中编写样式,减少了自定义 CSS 的需求。
  • UI 组件库:主流的 UI 组件库(如 Ant Design、Material-UI)通常已经内置了良好的组件化和样式隔离方案,开发者直接使用即可。
// stylelint.config.js

module.exports = {
extends: [
'stylelint-config-twbs-bootstrap',
],
};
注意

stylelint 规则参考 stylelint-config-twbs-bootstrap,但是由于需要整理的内容非常多,在开发过程中,仍以实际的 stylelint 检查为准。本章内容仅供参考

规则一览

来源规则名称配置值描述
stylelint-config-recommendedannotation-no-unknowntrue禁止未知的注解。
stylelint-config-recommendedat-rule-no-unknowntrue禁止未知的 at 规则。
stylelint-config-recommendedblock-no-emptytrue禁止空块。
stylelint-config-recommendedcolor-no-invalid-hextrue禁止无效的十六进制颜色。
stylelint-config-recommendedcomment-no-emptytrue禁止空注释。
stylelint-config-recommendedcustom-property-no-missing-var-functiontrue禁止在自定义属性中缺少 var 函数。
stylelint-config-recommendeddeclaration-block-no-duplicate-custom-propertiestrue禁止在声明块中重复自定义属性。
stylelint-config-recommendeddeclaration-block-no-duplicate-properties[true, { ignore: ['consecutive-duplicates-with-different-syntaxes'] }]禁止在声明块中重复属性,忽略具有不同语法的连续重复。
stylelint-config-recommendeddeclaration-block-no-shorthand-property-overridestrue禁止简写属性覆盖相关longhand属性。
stylelint-config-recommendedfont-family-no-duplicate-namestrue禁止在 font-family 中重复字体名称。
stylelint-config-recommendedfont-family-no-missing-generic-family-keywordtrue禁止在 font-family 中缺少通用字体系列关键字。
stylelint-config-recommendedfunction-calc-no-unspaced-operatortrue禁止在 calc 函数中使用无间隔的运算符。
stylelint-config-recommendedfunction-linear-gradient-no-nonstandard-directiontrue禁止在 linear-gradient 函数中使用非标准方向。
stylelint-config-recommendedfunction-no-unknowntrue禁止未知的函数。
stylelint-config-recommendedkeyframe-block-no-duplicate-selectorstrue禁止在关键帧块中重复选择器。
stylelint-config-recommendedkeyframe-declaration-no-importanttrue禁止在关键帧声明中使用 !important。
stylelint-config-recommendedmedia-feature-name-no-unknowntrue禁止未知的媒体特性名称。
stylelint-config-recommendednamed-grid-areas-no-invalidtrue禁止无效的命名网格区域。
stylelint-config-recommendedno-duplicate-at-import-rulestrue禁止重复的 @import 规则。
stylelint-config-recommendedno-duplicate-selectorstrue禁止重复的选择器。
stylelint-config-recommendedno-empty-sourcetrue禁止空源。
stylelint-config-recommendedno-invalid-double-slash-commentstrue禁止无效的双斜线注释。
stylelint-config-recommendedno-invalid-position-at-import-ruletrue禁止 @import 规则位于无效位置。
stylelint-config-recommendedno-irregular-whitespacetrue禁止不规则空白。
stylelint-config-recommendedproperty-no-unknowntrue禁止未知的属性。
stylelint-config-recommendedselector-anb-no-unmatchabletrue禁止无法匹配的 :nth-child() 等选择器。
stylelint-config-recommendedselector-pseudo-class-no-unknowntrue禁止未知的伪类选择器。
stylelint-config-recommendedselector-pseudo-element-no-unknowntrue禁止未知的伪元素选择器。
stylelint-config-recommendedselector-type-no-unknown[true, { ignore: ['custom-elements'] }]禁止未知的类型选择器,忽略自定义元素。
stylelint-config-recommendedstring-no-newlinetrue禁止字符串中的换行符。
stylelint-config-recommendedunit-no-unknowntrue禁止未知的单位。
stylelint-config-standardat-rule-no-vendor-prefixtrue禁止 at 规则使用供应商前缀。
stylelint-config-standardcolor-hex-length'short'指定十六进制颜色使用短格式。
stylelint-config-standardcomment-empty-line-before['always', { except: ['first-nested'], ignore: ['stylelint-commands'] }]在注释前要求空行,除了嵌套内的第一条和 stylelint 命令。
stylelint-config-standardcomment-whitespace-inside'always'要求注释内部有空白。
stylelint-config-standardcustom-media-pattern['^([a-z][a-z0-9]*)(-[a-z0-9]+)*$']指定自定义媒体查询的模式。
stylelint-config-standarddeclaration-block-single-line-max-declarations1单行声明块中最多允许的声明数量。
stylelint-config-standardfont-family-name-quotes'always-where-recommended'指定字体系列名称使用引号的情况。
stylelint-config-standardfunction-name-case'lower'指定函数名称为小写。
stylelint-config-standardfunction-url-quotes'always'要求 url 函数使用引号。
stylelint-config-standardhue-degree-notation'angle'指定色调使用角度表示法。
stylelint-config-standardimport-notation'url'指定 @import 规则使用 url 表示法。
stylelint-config-standardkeyframe-selector-notation'percentage-unless-within-keyword-only-block'指定关键帧选择器使用百分比表示法,除非在仅关键字块内。
stylelint-config-standardkeyframes-name-pattern['^([a-z][a-z0-9]*)(-[a-z0-9]+)*$']指定关键帧名称的模式。
stylelint-config-standardlength-zero-no-unit[true, { ignore: ['custom-properties'] }]长度为零时禁止使用单位,忽略自定义属性。
stylelint-config-standardlightness-notation'percentage'指定亮度使用百分比表示法。
stylelint-config-standardmedia-feature-name-no-vendor-prefixtrue禁止媒体特性名称使用供应商前缀。
stylelint-config-standardproperty-no-vendor-prefixtrue禁止属性使用供应商前缀。
stylelint-config-standardselector-attribute-quotes'always'要求属性选择器使用引号。
stylelint-config-standardselector-class-pattern['^([a-z][a-z0-9]*)(-[a-z0-9]+)*$']指定类选择器的模式。
stylelint-config-standardselector-id-pattern['^([a-z][a-z0-9]*)(-[a-z0-9]+)*$']指定 ID 选择器的模式。
stylelint-config-standardselector-no-vendor-prefixtrue禁止选择器使用供应商前缀。
stylelint-config-standardselector-pseudo-element-colon-notation'double'指定伪元素使用双冒号表示法。
stylelint-config-standardselector-type-case'lower'指定类型选择器为小写。
stylelint-config-standardshorthand-property-no-redundant-valuestrue禁止简写属性中使用冗余值。
stylelint-config-standardvalue-keyword-case'lower'指定值关键字为小写。
stylelint-config-standardvalue-no-vendor-prefix[true, { ignoreValues: ['box', 'inline-box'] }]禁止值使用供应商前缀,忽略 'box' 和 'inline-box'。
@stylistic/stylelint-config@stylistic/at-rule-name-case'lower'指定 at 规则名称为小写。
@stylistic/stylelint-config@stylistic/at-rule-semicolon-newline-after'always'在 at 规则分号后要求换行。
@stylistic/stylelint-config@stylistic/block-closing-brace-newline-before'always-multi-line'在多行块的关闭大括号前要求换行。
@stylistic/stylelint-config@stylistic/block-closing-brace-space-before'always-single-line'在单行块的关闭大括号前要求空格。
@stylistic/stylelint-config@stylistic/block-opening-brace-newline-after'always-multi-line'在多行块的打开大括号后要求换行。
@stylistic/stylelint-config@stylistic/block-opening-brace-space-after'always-single-line'在单行块的打开大括号后要求空格。
@stylistic/stylelint-config@stylistic/color-hex-case'lower'指定十六进制颜色为小写。
@stylistic/stylelint-config@stylistic/declaration-bang-space-after'never'在声明感叹号后禁止空格。
@stylistic/stylelint-config@stylistic/declaration-bang-space-before'always'在声明感叹号前要求空格。
@stylistic/stylelint-config@stylistic/declaration-block-semicolon-newline-after'always-multi-line'在多行声明块分号后要求换行。
@stylistic/stylelint-config@stylistic/declaration-block-semicolon-space-after'always-single-line'在单行声明块分号后要求空格。
@stylistic/stylelint-config@stylistic/declaration-block-semicolon-space-before'never'在声明块分号前禁止空格。
@stylistic/stylelint-config@stylistic/declaration-block-trailing-semicolon'always'要求声明块有尾随分号。
@stylistic/stylelint-config@stylistic/declaration-colon-newline-after'always-multi-line'在多行声明冒号后要求换行。
@stylistic/stylelint-config@stylistic/declaration-colon-space-after'always-single-line'在单行声明冒号后要求空格。
@stylistic/stylelint-config@stylistic/declaration-colon-space-before'never'在声明冒号前禁止空格。
@stylistic/stylelint-config@stylistic/function-comma-newline-after'always-multi-line'在多行函数逗号后要求换行。
@stylistic/stylelint-config@stylistic/function-comma-space-after'always-single-line'在单行函数逗号后要求空格。
@stylistic/stylelint-config@stylistic/function-comma-space-before'never'在函数逗号前禁止空格。
@stylistic/stylelint-config@stylistic/function-max-empty-lines0函数中最多允许的空行数。
@stylistic/stylelint-config@stylistic/function-parentheses-newline-inside'always-multi-line'在多行函数括号内要求换行。
@stylistic/stylelint-config@stylistic/function-parentheses-space-inside'never-single-line'在单行函数括号内禁止空格。
@stylistic/stylelint-config@stylistic/function-whitespace-after'always'在函数后要求空白。
@stylistic/stylelint-config@stylistic/indentation2指定缩进为 2 个空格。
@stylistic/stylelint-config@stylistic/media-feature-colon-space-after'always'在媒体特性冒号后要求空格。
@stylistic/stylelint-config@stylistic/media-feature-colon-space-before'never'在媒体特性冒号前禁止空格。
@stylistic/stylelint-config@stylistic/media-feature-name-case'lower'指定媒体特性名称为小写。
@stylistic/stylelint-config@stylistic/media-feature-parentheses-space-inside'never'在媒体特性括号内禁止空格。
@stylistic/stylelint-config@stylistic/media-feature-range-operator-space-after'always'在媒体特性范围运算符后要求空格。
@stylistic/stylelint-config@stylistic/media-feature-range-operator-space-before'always'在媒体特性范围运算符前要求空格。
@stylistic/stylelint-config@stylistic/media-query-list-comma-newline-after'always-multi-line'在多行媒体查询列表逗号后要求换行。
@stylistic/stylelint-config@stylistic/media-query-list-comma-space-after'always-single-line'在单行媒体查询列表逗号后要求空格。
@stylistic/stylelint-config@stylistic/media-query-list-comma-space-before'never'在媒体查询列表逗号前禁止空格。
@stylistic/stylelint-config@stylistic/no-empty-first-linetrue禁止空首行。
@stylistic/stylelint-config@stylistic/no-eol-whitespacetrue禁止行尾空白。
@stylistic/stylelint-config@stylistic/no-extra-semicolonstrue禁止多余的分号。
@stylistic/stylelint-config@stylistic/no-missing-end-of-source-newlinetrue要求源末尾有换行。
@stylistic/stylelint-config@stylistic/number-no-trailing-zerostrue禁止数字中的尾随零。
@stylistic/stylelint-config@stylistic/property-case'lower'指定属性为小写。
@stylistic/stylelint-config@stylistic/selector-attribute-brackets-space-inside'never'在属性选择器括号内禁止空格。
@stylistic/stylelint-config@stylistic/selector-attribute-operator-space-after'never'在属性选择器运算符后禁止空格。
@stylistic/stylelint-config@stylistic/selector-attribute-operator-space-before'never'在属性选择器运算符前禁止空格。
@stylistic/stylelint-config@stylistic/selector-combinator-space-after'always'在选择器组合器后要求空格。
@stylistic/stylelint-config@stylistic/selector-combinator-space-before'always'在选择器组合器前要求空格。
@stylistic/stylelint-config@stylistic/selector-descendant-combinator-no-non-spacetrue禁止后代组合器使用非空格字符。
@stylistic/stylelint-config@stylistic/selector-list-comma-newline-after'always'在选择器列表逗号后要求换行。
@stylistic/stylelint-config@stylistic/selector-max-empty-lines0选择器中最多允许的空行数。
@stylistic/stylelint-config@stylistic/selector-pseudo-class-case'lower'指定伪类选择器为小写。
@stylistic/stylelint-config@stylistic/selector-pseudo-class-parentheses-space-inside'never'在伪类选择器括号内禁止空格。
@stylistic/stylelint-config@stylistic/selector-pseudo-element-case'lower'指定伪元素选择器为小写。
@stylistic/stylelint-config@stylistic/string-quotes'double'指定字符串使用双引号。
@stylistic/stylelint-config@stylistic/unit-case'lower'指定单位为小写。
@stylistic/stylelint-config@stylistic/value-list-comma-space-before'never'在值列表逗号前禁止空格。
@stylistic/stylelint-config@stylistic/value-list-max-empty-lines0值列表中最多允许的空行数。
stylelint-config-twbs-bootstrapalpha-value-notationnull禁用 alpha 值表示法规则。
stylelint-config-twbs-bootstrapat-rule-empty-line-beforenull禁用 at 规则前空行规则。
stylelint-config-twbs-bootstrapcolor-function-notationnull禁用颜色函数表示法规则。
stylelint-config-twbs-bootstrapcolor-named'never'禁止使用命名颜色。
stylelint-config-twbs-bootstrapcustom-property-empty-line-beforenull禁用自定义属性前空行规则。
stylelint-config-twbs-bootstrapcustom-property-patternnull禁用自定义属性模式规则。
stylelint-config-twbs-bootstrapdeclaration-block-no-redundant-longhand-propertiesnull禁用声明块中冗余 longhand 属性规则。
stylelint-config-twbs-bootstrapdeclaration-empty-line-beforenull禁用声明前空行规则。
stylelint-config-twbs-bootstrapdeclaration-no-importanttrue禁止声明中使用 !important。
stylelint-config-twbs-bootstrapfont-weight-notation['numeric', { 'ignore': ['relative'] }]指定字体重量使用数字表示法,忽略相对值。
stylelint-config-twbs-bootstrapfunction-url-no-scheme-relativetrue禁止 url 函数中使用协议相对 URL。
stylelint-config-twbs-bootstrapmedia-feature-range-notationnull禁用媒体特性范围表示法规则。
stylelint-config-twbs-bootstrapmedia-query-no-invalidnull禁用无效媒体查询规则。
stylelint-config-twbs-bootstrapno-descending-specificitynull禁用特异性降序规则。
stylelint-config-twbs-bootstrapnumber-max-precisionnull禁用数字最大精度规则。
stylelint-config-twbs-bootstraprule-empty-line-beforenull禁用规则前空行规则。
stylelint-config-twbs-bootstrapselector-max-attribute2限制选择器中属性的最大数量。
stylelint-config-twbs-bootstrapselector-max-class4限制选择器中类的最大数量。
stylelint-config-twbs-bootstrapselector-max-combinators4限制选择器中组合器的最大数量。
stylelint-config-twbs-bootstrapselector-max-compound-selectors4限制选择器中复合选择器的最大数量。
stylelint-config-twbs-bootstrapselector-max-id0限制选择器中 ID 的最大数量。
stylelint-config-twbs-bootstrapselector-max-specificitynull禁用选择器最大特异性规则。
stylelint-config-twbs-bootstrapselector-max-type2限制选择器中类型的最大数量。
stylelint-config-twbs-bootstrapselector-max-universal1限制选择器中通用选择器的最大数量。
stylelint-config-twbs-bootstrapselector-no-qualifying-typetrue禁止选择器中使用限定类型。
stylelint-config-twbs-bootstrapselector-not-notationnull禁用 :not() 表示法规则。
stylelint-config-twbs-bootstrap@stylistic/at-rule-name-space-after'always'在 at 规则名称后要求空格。
stylelint-config-twbs-bootstrap@stylistic/at-rule-semicolon-space-before'never'在 at 规则分号前禁止空格。
stylelint-config-twbs-bootstrap@stylistic/block-closing-brace-empty-line-beforenull禁用块关闭大括号前空行规则。
stylelint-config-twbs-bootstrap@stylistic/block-closing-brace-newline-afternull禁用块关闭大括号后换行规则。
stylelint-config-twbs-bootstrap@stylistic/block-opening-brace-space-beforenull禁用块打开大括号前空格规则。
stylelint-config-twbs-bootstrap@stylistic/declaration-block-semicolon-newline-before'never-multi-line'在多行声明块分号前禁止换行。
stylelint-config-twbs-bootstrap@stylistic/max-empty-lines2最多允许的空行数。
stylelint-config-twbs-bootstrap@stylistic/max-line-lengthnull禁用最大行长度规则。
stylelint-config-twbs-bootstrap@stylistic/number-leading-zero'never'禁止数字中的前导零。
stylelint-config-twbs-bootstrap@stylistic/selector-list-comma-newline-before'never-multi-line'在多行选择器列表逗号前禁止换行。
stylelint-config-twbs-bootstrap@stylistic/selector-list-comma-space-after'always-single-line'在单行选择器列表逗号后要求空格。
stylelint-config-twbs-bootstrap@stylistic/selector-list-comma-space-before'never-single-line'在单行选择器列表逗号前禁止空格。
stylelint-config-twbs-bootstrap@stylistic/unicode-bom'never'禁止使用 Unicode BOM。
stylelint-config-twbs-bootstrap@stylistic/value-list-comma-newline-after'never-multi-line'在多行值列表逗号后禁止换行。
stylelint-config-twbs-bootstrap@stylistic/value-list-comma-newline-before'never-multi-line'在多行值列表逗号前禁止换行。
stylelint-config-twbs-bootstrap@stylistic/value-list-comma-space-after'always'在值列表逗号后要求空格。

属性排序

CSS Modules 组合规则

全局重置

定位

显示模式

弹性盒子

网格布局

间距

布局对齐

顺序

盒模型

排版

可访问性与交互

图像、背景和边框

遮罩

SVG 表现

过渡与动画

分页媒体

参考 MDN - CSS 分页媒体

Vue 专属规则

// stylelint.config.js

module.exports = {
extends: [
'stylelint-config-recommended-vue',
],
};
来源规则名称配置值描述
stylelint-config-recommended-vueselector-pseudo-class-no-unknown[true, { ignorePseudoClasses: ["deep", "global", "slotted"] }]禁止未知的伪类选择器,但忽略 Vue 特定的伪类(如 deepglobalslotted
stylelint-config-recommended-vueselector-pseudo-element-no-unknown[true, { ignorePseudoElements: ["v-deep", "v-global", "v-slotted"] }]禁止未知的伪元素选择器,但忽略 Vue 特定的伪元素(如 v-deepv-globalv-slotted
stylelint-config-recommended-vuedeclaration-property-value-no-unknown[true, { ignoreProperties: { "/.*/": "/v-bind\\(.+\\)/" } }]禁止未知的声明属性值,但忽略包含 v-bind 表达式的值
stylelint-config-recommended-vuefunction-no-unknown[true, { ignoreFunctions: ["v-bind"] }]禁止未知的 CSS 函数,但忽略 Vue 的 v-bind 函数