css编码规范

317 阅读5分钟

1.前言

本文档的目标是使研发中心的各位同学css能够按照统一规范进行编写,使其风格保持一致,便于理解和维护。在使用sass,less,stylus也遵循本文档。

2.代码风格

2.1 缩进,空格

  1. [强制] 使用 2 个空格做为一个缩进层级,不允许使用 4 个空格 或 tab 字符。
  2. [强制] 选择器 与 { 之间必须包含空格。
  3. [强制] 属性名 与之后的 : 之间不允许包含空格, : 与 属性值 之间必须包含空格。
  4. [强制] 列表型属性值 书写在单行时,, 后必须跟一个空格

示例:

.selector {
  margin: 0;
  padding: 0;
  font-family: Arial, sans-serif;
}

2.2 行长度

[强制] 每行不得超过 120 个字符,除非单行不可分割。

示例:

/* 不同属性值按逻辑分组 */
background:
  transparent url(aVeryVeryVeryLongUrlIsPlacedHere)
  no-repeat 0 0;

/* 可重复多次的属性,每次重复一行 */
background-image:
  url(aVeryVeryVeryLongUrlIsPlacedHere)
  url(anotherVeryVeryVeryLongUrlIsPlacedHere);

/* 类似函数的属性值可以根据函数调用的缩进进行 */
background-image: -webkit-gradient(
  linear,
  left bottom,
  left top,
  color-stop(0.04, rgb(88,94,124)),
  color-stop(0.52, rgb(115,123,162))
);

2.3 选择器

[强制] 当一个 rule 包含多个 selector 时,每个选择器声明必须独占一行。

示例:

/* good */
.post,
.page,
.comment {
  line-height: 1.5;
}

/* bad */
.post, .page, .comment {
  line-height: 1.5;
}

2.4 有符号的空格

[强制] >、+、~ 选择器的两边各保留一个空格。

示例:

/* good */
main > nav {
  padding: 10px;
}

label + input {
  margin-left: 5px;
}

input:checked ~ button {
  background-color: #69C;
}

/* bad */
main>nav {
  padding: 10px;
}

label+input {
  margin-left: 5px;
}

input:checked~button {
  background-color: #69C;
}

2.5 属性选择器

[强制] 属性选择器中的值必须用双引号包围。

示例:

/* good */
article[character="juliet"] {
  voice-family: "Vivien Leigh", victoria, female;
}

/* bad */
article[character='juliet'] {
  voice-family: "Vivien Leigh", victoria, female;
}

2.6 属性

  1. [强制] 属性定义必须另起一行。
  2. [强制] 属性定义后必须以分号结尾。

示例:

/* good */
.selector {
  margin: 0;
  padding: 0;
}

/* bad */
.selector { margin: 0; padding: 0; }

/* good */
.selector {
  margin: 0;
}

/* bad */
.selector {
  margin: 0
}

3.通用

3.1 选择器

  1. [强制] 如无必要,不得为 id、class 选择器添加类型选择器进行限定。(在性能和维护性上,都有一定的影响。)
  2. [建议] 选择器的嵌套层级应不大于 3 级,位置靠后的限定条件应尽可能精确。

示例:

/* good */
#error,
.danger-message {
  font-color: #c00;
}

/* bad */
dialog#error,
p.danger-message {
  font-color: #c00;
}

/* good */
#username input {}
.comment .avatar {}

/* bad */
.page .header .login #username input {}
.comment div * {}

3.2 属性缩写

[建议] 在可以使用缩写的情况下,尽量使用属性缩写。

3.3 清除浮动

[建议] 当元素需要撑起高度以包含内部的浮动元素时,通过对伪类设置 clear 或触发 BFC 的方式进行 clearfix。尽量不使用增加空标签的方式。

触发 BFC 的方式很多,常见的有:

  1. float 非 none
  2. position 非 static
  3. overflow 非 visible

另需注意,对已经触发 BFC 的元素不需要再进行 clearfix。

3.4 !important

[建议] 尽量不使用 !important 声明。 当需要强制指定样式且不允许任何场景覆盖时,通过标签内联和 !important 定义样式。

3.5 z-index

  1. [建议] 将 z-index 进行分层,对文档流外绝对定位元素的视觉层级关系进行管理。同层的多个元素,如多个由用户输入触发的 Dialog,在该层级内使用相同的 z-index 或递增 z-index。
  2. [建议] 在可控环境下,期望显示在最上层的元素,z-index 指定为 999999。

4.值与单位

4.1 文本

[强制] 文本内容必须用双引号包围。

示例:

/* good */
html[lang|="zh"] q:before {
  font-family: "Microsoft YaHei", sans-serif;
  content: "“";
}

html[lang|="zh"] q:after {
  font-family: "Microsoft YaHei", sans-serif;
  content: "”";
}

/* bad */
html[lang|=zh] q:before {
  font-family: 'Microsoft YaHei', sans-serif;
  content: '“';
}


html[lang|=zh] q:after {
  font-family: "Microsoft YaHei", sans-serif;
  content: "”";
}

4.2 数值

[强制] 当数值为 0 - 1 之间的小数时,省略整数部分的 0。。

示例:

/* good */
panel {
  opacity: .8;
}

/* bad */
panel {
  opacity: 0.8;
}

4.3 url()

[强制] url() 函数中的路径不加引号。

示例:

body {
  background: url(bg.png);
}

4.4 长度

[强制] 长度为 0 时须省略单位。 (也只有长度单位可省)。

示例:

/* good */
body {
  padding: 0 5px;
}

/* bad */
body {
  padding: 0px 5px;
}

4.4 颜色

  1. [强制] RGB颜色值必须使用十六进制记号形式 #rrggbb。不允许使用 rgb()。 (注:带有alpha的颜色信息可以使用 rgba()。使用 rgba() 时每个逗号后必须保留一个空格。)
  2. [强制] 颜色值不允许使用命名色值。
  3. 建议] 颜色值中的英文字符采用小写。

示例:

/* good */
.success {
  box-shadow: 0 0 2px rgba(0, 128, 0, .3);
  border-color: #008000;
}

/* bad */
.success {
  box-shadow: 0 0 2px rgba(0,128,0,.3);
  border-color: rgb(0, 128, 0);
}

/* bad */
.success {
  color: lightgreen;
}

/* bad */
.success {
  background-color: #ACA;
  color: #90ee90;
}

5 文本编排

5.1 字号

[强制] 需要在 Windows 平台显示的中文内容,其字号应不小于 12px。(注:由于 Windows 的字体渲染机制,小于 12px 的文字显示效果极差、难以辨认。)

5.2 字重

[强制] font-weight 属性必须使用数值方式描述。(注: CSS 的字重分 100 – 900 共九档,但目前受字体本身质量和浏览器的限制,实际上支持 400 和 700 两档,分别等价于关键词 normal 和 bold。 浏览器本身使用一系列启发式规则来进行匹配,在 <700 时一般匹配字体的 Regular 字重,>=700 时匹配 Bold 字重。但已有浏览器开始支持 =600 时匹配 Semibold 字重 (见此表),故使用数值描述增加了灵活性,也更简短。)

示例:

/* good */
h1 {
  font-weight: 700;
}

/* bad */
h1 {
  font-weight: bold;
}

5.2 行高

[建议] line-height 在定义文本段落时,应使用数值。(注: 将 line-height 设置为数值,浏览器会基于当前元素设置的 font-size 进行再次计算。在不同字号的文本段落组合中,能达到较为舒适的行间间隔效果,避免在每个设置了 font-size 都需要设置 line-height。 当 line-height 用于控制垂直居中时,还是应该设置成与容器高度一致。)

示例:

/* good */
h1 {
  font-weight: 700;
}

/* bad */
h1 {
  font-weight: bold;
}