|
| 1 | +--- |
| 2 | +group: 代码规约 |
| 3 | +--- |
| 4 | + |
1 | 5 | # CSS 编码规约
|
2 | 6 |
|
3 | 7 | 本规约涉及 CSS 及其预编译语言(Sass、Less)的编码风格和最佳实践,部分规则可通过 [stylelint](https://stylelint.io/) 工具落地。
|
|
11 | 15 | 上图是一张符合规约要求编码风格的速览图,详细规则如下:
|
12 | 16 |
|
13 | 17 | - 1.1.1【强制】所有声明都应该以分号结尾,不能省略。stylelint: [declaration-block-trailing-semicolon](https://stylelint.io/user-guide/rules/declaration-block-trailing-semicolon)
|
14 |
| - |
| 18 | + |
15 | 19 | 虽然 CSS 语法中最后一条声明的分号是可选的,但是使用分号可以增加代码的一致性和易用性。
|
16 | 20 |
|
17 | 21 | ```css
|
|
42 | 46 | }
|
43 | 47 | ```
|
44 | 48 |
|
45 |
| -- 1.1.3【推荐】选择器和 `{` 之间保留一个空格。stylelint: [block-opening-brace-space-before](https://stylelint.io/user-guide/rules/block-opening-brace-space-before) |
| 49 | +- 1.1.3【推荐】选择器和 `{` 之间保留一个空格。stylelint: [block-opening-brace-space-before](https://stylelint.io/user-guide/rules/block-opening-brace-space-before) |
46 | 50 |
|
47 | 51 | ```css
|
48 | 52 | /* bad */
|
|
56 | 60 | }
|
57 | 61 | ```
|
58 | 62 |
|
59 |
| -- 1.1.4【推荐】属性名和 `:` 之前无空格,`:` 和属性值之间保留一个空格。stylelint: [declaration-colon-space-after](https://stylelint.io/user-guide/rules/declaration-colon-space-after) [declaration-colon-space-before](https://stylelint.io/user-guide/rules/declaration-colon-space-before) |
| 63 | +- 1.1.4【推荐】属性名和 `:` 之前无空格,`:` 和属性值之间保留一个空格。stylelint: [declaration-colon-space-after](https://stylelint.io/user-guide/rules/declaration-colon-space-after) [declaration-colon-space-before](https://stylelint.io/user-guide/rules/declaration-colon-space-before) |
60 | 64 |
|
61 | 65 | ```css
|
62 | 66 | /* bad */
|
|
180 | 184 | ```
|
181 | 185 |
|
182 | 186 | - 1.1.11【参考】使用多个选择器时,每个选择器应该单独成行。stylelint: [selector-list-comma-newline-after](https://stylelint.io/user-guide/rules/selector-list-comma-newline-after)
|
183 |
| - |
| 187 | + |
184 | 188 | ```css
|
185 | 189 | /* bad */
|
186 | 190 | .selector, .selector-secondary, .selector-third {
|
|
234 | 238 | ### 1.2 选择器
|
235 | 239 |
|
236 | 240 | - 1.2.1【参考】不要使用 id 选择器。stylelint: [selector-max-id](https://stylelint.io/user-guide/rules/selector-max-id)
|
237 |
| - |
| 241 | + |
238 | 242 | id 会带来过高的[选择器优先级](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity),使得后续很难进行样式覆盖(继而引发使用 `!important` 覆盖样式的恶性循环)。
|
239 | 243 |
|
240 | 244 | ```css
|
|
290 | 294 | > - 伪类(伪元素)选择器,比如 `a:hover`、`a::before`
|
291 | 295 |
|
292 | 296 | ### 1.3 属性和属性值
|
293 |
| - |
| 297 | + |
294 | 298 | - 1.3.1【推荐】使用尽可能短的十六进制值。stylelint: [color-hex-length](https://stylelint.io/user-guide/rules/color-hex-length)
|
295 | 299 |
|
296 | 300 | ```css
|
|
412 | 416 | ```
|
413 | 417 |
|
414 | 418 | 更多 CSS 属性顺序参考如下列表:
|
415 |
| - |
| 419 | + |
416 | 420 | |第一组|第二组|第三组|第四组|第五组|第六组|第七组|
|
417 | 421 | |----|----|----|----|----|----|----|
|
418 | 422 | |content |box-sizing|background* |font*|opacity |unicode-bidi|transition*|
|
|
449 | 453 | - `border-radius`
|
450 | 454 |
|
451 | 455 | 使用简写属性时,需要显式地设置所有值。我们应该在真正需要设置所有值或大多数值时才使用简写属性。
|
452 |
| - |
| 456 | + |
453 | 457 | 如果只是想设置某一个属性,则不应该使用简写属性:
|
454 | 458 |
|
455 | 459 | ```css
|
|
483 | 487 | ## 2 Sass 和 Less
|
484 | 488 |
|
485 | 489 | > 对于 CSS 而言,可以在新项目中尝试放弃使用 Sass、Less 这样的处理器语言,因为:
|
486 |
| -> |
| 490 | +> |
487 | 491 | >* 这些处理器语言是在一定历史条件下的产物,虽然这些产物在一定程度上提高开发者的开发效率,但不同的处理器语言也同时增加了项目的维护成本(特别是多人协作,多团队协作的时候)。
|
488 | 492 | >* 更建议使用 PostCSS 处理器,它类似于 CSS 中的 Babel,不但具备 Sass 和 Less 的功能,而且社区繁荣,同时还可以根据自己的需求扩展相关的插件。
|
489 | 493 | >* 随着 CSS 的一些新特性出现,Sass 和 Less 以往的优势也会慢慢消失。
|
|
0 commit comments