CSS编码标准
本文档的目标是使CSS代码风格保持一致,容易被懂得和被保护,假如本人没有这种习惯,请好好选中你的IDE,别再用“文本编纂器”。
本文档虽针对CSS设计的,但是在使用各种CSS的预编译器(如less、sass、stylus等)时,适用的部分也应尽量遵照本文档的约定。
1 代码风格
1.1 文件
[倡议] CSS 文件使用无 BOM 的 UTF-8 编码。
说明:
UTF-8 编码具有更广泛的顺应性。BOM 在使用程序或工具处置文件时大概造成不必要的干扰。
1.2 缩进
[强迫] 使用 2 个空格做为一个缩进层级,不同意使用tab 字符。
示例:
.selector { margin: 0; padding: 0; }
1.3 空格
[强迫] 选中器 与 { 之间必需包括空格。
示例:
.selector { }
[强迫] 属性名 与之后的 : 之间不同意包括空格, : 与 属性值 之间必需包括空格。
示例:
margin: 0;
[强迫] 列表型属性值 书写在单行时,, 后必需跟一个空格。
示例:
font-family: Arial, sans-serif;
1.4 行长度
[强迫] 每行不得超越 120 个字符,除非单行不成分割。
说明:
常见不成分割的场景为URL超长。
[倡议] 关于超长的样式,在样式值的 空格 处或 , 后换行,倡议按逻辑分组。
示例:
/* 不一样属性值按逻辑分组 */ 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)) );
1.5 选中器
[强迫] 当一个 rule 包括多个 selector 时,每个选中器声明必需独占一行。
示例:
/* good */ .post, .page, .comment { line-height: 1.5; } /* bad */ .post, .page, .comment { line-height: 1.5; }
[强迫] >、+、~ 选中器的两边各保存一个空格。
示例:
/* 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; }
[强迫] 属性选中器中的值必需用双引号包抄。
说明:
不同意使用单引号,不同意不使用引号。
示例:
/* good */ article[character="juliet"] { voice-family: "Vivien Leigh", victoria, female; } /* bad */ article[character='juliet'] { voice-family: "Vivien Leigh", victoria, female; }
1.6 属性
[强迫] 多个属性定义必需另起一行。
示例:
/* good */ .selector { margin: 0; padding: 0; } /* bad */ .selector { margin: 0; padding: 0; }
[强迫] 属性定义后必需以分号结尾。
示例:
/* good */ .selector { margin: 0; } /* bad */ .selector { margin: 0 }
2 通用
2.1 选中器
[强迫] 如无必要,不得为 id、class 选中器增加 类型选中器 停止限制。
说明:
在机能和保护性上,都有必然的影响。
示例:
/* good */ #error, .danger-message { font-color: #c00; } /* bad */ dialog#error, p.danger-message { font-color: #c00; }
[倡议] 选中器的嵌套层级应不大于 4 级,位置靠后的限制前提应尽大概准确。
示例:
/* good */ #username input {} .comment .avatar {} /* bad */ .page .header .login #username input {} .comment div * {}
2.2 属性缩写
[倡议] 在可以使用缩写的状况下,尽量使用属性缩写。
示例:
/* good */ .post { font: 12px/1.5 arial, sans-serif; } /* bad */ .post { font-family: arial, sans-serif; font-size: 12px; line-height: 1.5; }
[倡议] 使用 border / margin / padding 等缩写时,应留意隐含值对实际数值的影响,确实需要设定多个标的目的的值时才使用缩写。
说明:
border / margin / padding 等缩写会同时设定多个属性的值,容易覆盖不需要覆盖的设定。如某些标的目的需要继承其他声明的值,则应当分开设定。
示例:
/* centering <article class="page"> horizontally and highlight featured ones */ article { margin: 5px; border: 1px solid #999; } /* good */ .page { margin-right: auto; margin-left: auto; } .featured { border-color: #69c; } /* bad */ .page { margin: 5px auto; /* introducing redundancy */ } .featured { border: 1px solid #69c; /* introducing redundancy */ }
2.3 属性书写次序
[倡议] 统一 rule set 下的属性在书写时,应按功效停止分组,并以 Formatting Model(规划方式、位置) > Box Model(尺寸) > Typographic(文本相关) > Visual(视觉结果) 的次序书写,以提高代码的可读性。
说明:
Formatting Model 相关属性包罗:position / top / right / bottom / left / float / display / overflow 等
Box Model 相关属性包罗:border / margin / padding / width / height 等
Typographic 相关属性包罗:font / line-height / text-align / word-wrap 等
Visual 相关属性包罗:background / color / transition / list-style 等
Misc 相关属性包罗:opacity / text-decoration / white-space / word-wrap 等
别的,假如包括 content 属性,应放在最前面。
示例:
.sidebar { /* formatting model: positioning schemes / offsets / z-indexes / display / ... */ position: absolute; top: 50px; left: 0; overflow-x: hidden; display: block; /* box model: sizes / margins / paddings / borders / ... */ width: 200px; padding: 5px; border: 1px solid #ddd; /* typographic: font / aligns / text styles / ... */ font-size: 14px; line-height: 20px; /* visual: colors / shadows / gradients / ... */ background: #f5f5f5; color: #333; -webkit-transition: color 1s; -moz-transition: color 1s; transition: color 1s; /* Misc */ opacity: 1; text-decoration: none; white-space: nowrap; word-wrap: normal; cursor: pointer; }
2.4 清除浮动
[倡议] 当元素需要撑起高度以包括内部的浮动元素时,通过对伪类设定 clear 或触发 BFC 的方式停止 clearfix。尽量不使用增添空标签的方式。
说明:
触发 BFC 的方式许多,常见的有:
float 非 none position 非 static overflow 非 visible
如但愿使用更小副作用的清除浮动办法,拜见 A new micro clearfix hack 一文。
另需留意,对已经触发 BFC 的元素不需要再停止 clearfix。
示例:
<div class="fn-clear"> <div class="fn-left"></div> <div class="fn-left"></div> </div>
2.5 !important
[倡议] 尽量不使用 !important 声明。
[倡议] 当需要强迫指定样式且不同意任何场景覆盖时,通过标签内联和 !important 定义样式。
说明:
必需留意的是,仅在设计上 确实不同意任何其它场景覆盖样式 时,才使用内联的 !important 样式。平常在第三方环境的利用中使用这种方案。下面的 z-index 章节是其中一个非凡场景的典型样例。
2.6 z-index
[倡议] 将 z-index 停止分层,对文档流外绝对定位元素的视觉层级关系停止治理。
说明:
同层的多个元素,如多个由会员输入触发的 Dialog,在该层级内使用雷同的 z-index 或递增 z-index。
倡议每层包括100个 z-index 来容纳足够的元素,假如每层元素较多,可以调整这个数值。
[倡议] 在可控环境下,盼望显示在最上层的元素,z-index 指定为 9999。
说明:
可控环境分成两种,一种是本身产品线环境;还有一种是大概会被其他产品线援用,但是不会被外部第三方的产品援用。
不倡议取值为 2147483647。以便于本身产品线被其他产品线援用时,当碰到层级覆盖冲突的状况,留出向上调整的空间。
[倡议] 在第三方环境下,盼望显示在最上层的元素,通过标签内联和 !important,将 z-index 指定为 2147483647。
说明:
第三方环境关于开发者来说完全不成控。在第三方环境下的元素,为了包管元素不被其页面其他样式定义覆盖,需要采纳此做法。
3 值与单位
3.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: "”"; }
3.2 数值
[强迫] 当数值为 0 - 1 之间的小数时,省略整数部分的 0。
示例:
/* good */ panel { opacity: .8 } /* bad */ panel { opacity: 0.8 }
3.3 url()
[强迫] url() 函数中的途径不加引号。
示例:
/* good */ body { background: url(bg.png); } /* bad */ body { background: url("bg.png"); }
[强迫] url() 函数中的绝对途径可省略和谈名。
示例:
body { background: url(//w.zuzuche.com/img/bg.png) no-repeat 0 0; }
3.4 长度
[强迫] 长度为 0 时须省略单位。 (也只要长度单位可省)
示例:
/* good */ body { padding: 0 5px; } /* bad */ body { padding: 0px 5px; }
3.5 色彩
[强迫] RGB色彩值必需使用十六进制记号情势 #rrggbb。不同意使用 rgb()。
说明:
带有alpha的色彩信息可以使用 rgba()。使用 rgba() 时每个逗号后必需保存一个空格。
示例:
/* 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); }
[强迫] 色彩值可以缩写时,必需使用缩写情势。
示例:
/* good */ .success { background-color: #aca; } /* bad */ .success { background-color: #aaccaa; }
[强迫] 色彩值不同意使用命名色值。
示例:
/* good */ .success { color: #90ee90; } /* bad */ .success { color: lightgreen; }
[倡议] 色彩值中的英文字符采纳小写。如不消小写也需要包管统一项目内保持大小写一致。
示例:
/* good */ .success { background-color: #aca; color: #90ee90; } /* good */ .success { background-color: #ACA; color: #90EE90; } /* bad */ .success { background-color: #ACA; color: #90ee90; }
3.6 2D 位置
[强迫] 必需同时给出水安然平静垂直标的目的的位置。
说明:
2D 位置初始值为 0% 0%,但在只要一个标的目的的值时,另一个标的目的的值会被解析为 center。为幸免懂得上的困扰,应同时给出两个标的目的的值。background-position属性值的定义
示例:
/* good */ body { background-position: center top; /* 50% 0% */ } /* bad */ body { background-position: top; /* 50% 0% */ }
4 文本编排
4.1 字体族
[强迫] font-family 属性中的字体族名称应使用字体的英文 Family Name,其中如有空格,须放置在引号中。
说明:
所谓英文 Family Name,为字体文件的一个元数据,常见名称如下:
字体操纵系统Family Name 宋体 (中易宋体)WindowsSimSun 黑体 (中易黑体)WindowsSimHei 微软雅黑WindowsMicrosoft YaHei 微软正黑WindowsMicrosoft JhengHei 华文黑体Mac/iOSSTHeiti 冬青黑体Mac/iOSHiragino Sans GB 文泉驿正黑LinuxWenQuanYi Zen Hei 文泉驿微米黑LinuxWenQuanYi Micro Hei
示例:
h1 { font-family: "Microsoft YaHei"; }
[强迫] font-family 按「西文字体在前、中文字体在后」、「结果佳 (质量高/更能知足需求) 的字体在前、结果一样的字体在后」的次序编写,最后必需指定一个通用字体族( serif / sans-serif )。
说明:
更具体说明可参照 本文。
示例:
/* Display according to platform */ .article { font-family: Arial, sans-serif; } /* Specific for most platforms */ h1 { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", "WenQuanYi Micro Hei", "Microsoft YaHei", sans-serif; }
[强迫] font-family 不区分大小写,但在统一个项目中,一样的 Family Name 大小写必需统一。
示例:
/* good */ body { font-family: Arial, sans-serif; } h1 { font-family: Arial, "Microsoft YaHei", sans-serif; } /* bad */ body { font-family: arial, sans-serif; } h1 { font-family: Arial, "Microsoft YaHei", sans-serif; }
4.2 字号
[强迫] 需要在 Windows 平台显示的中文内容,其字号应不小于 12px。
说明:
由于 Windows 的字体渲染机制,小于 12px 的文字显示结果极差、难以识别。
4.3 字体风格
[倡议] 需要在 Windows 平台显示的中文内容,不要使用除 normal 外的 font-style。其他平台也应慎用。
说明:
由于中文字体没有 italic 风格的实现,所有阅读器下都会 fallback 到 obilique 实现 (主动拟合为歪体),小字号下 (特殊是 Windows 下会在小字号下使用点阵字体的状况下) 显示结果差,造成阅读艰难。
4.4 字重
[强迫] 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; }
4.5 行高
[倡议] line-height 在定义文本段落时,应使用数值。
说明:
将 line-height 设定为数值,阅读器会基于当前元素设定的 font-size 停止再次运算。在不一样字号的文本段落组合中,能到达较为舒服的行间间隔结果,幸免在每个设定了 font-size 都需要设定 line-height。
当 line-height 用于操纵垂直居中时,还是应当设定成与容器高度一致。
示例:
.container { line-height: 1.5; }
5 变换与动画
[强迫] 使用 transition 时应指定 transition-property。
示例:
/* good */ .box { transition: color 1s, border-color 1s; } /* bad */ .box { transition: all 1s; }
[倡议] 尽大概在阅读器能高效实现的属性上增加过渡和动画。
说明:
见本文,在大概的状况下应选中这样四种变换:
transform: translate(npx, npx); transform: scale(n); transform: rotate(ndeg); opacity: 0..1;
典型的,可以使用 translate 来代替 left 作为动画属性。
示例:
/* good */ .box { transition: transform 1s; } .box:hover { transform: translate(20px); /* move right for 20px */ } /* bad */ .box { left: 0; transition: left 1s; } .box:hover { left: 20px; /* move right for 20px */ }
6 响应式
[强迫] Media Query 不得独自编排,必需与相关的规则一起定义。
示例:
/* Good */ /* header styles */ @media (...) { /* header styles */ } /* main styles */ @media (...) { /* main styles */ } /* footer styles */ @media (...) { /* footer styles */ } /* Bad */ /* header styles */ /* main styles */ /* footer styles */ @media (...) { /* header styles */ /* main styles */ /* footer styles */ }
[强迫] Media Query 假如有多个逗号分隔的前提时,应将每个前提放在独自一行中。
示例:
@media (-webkit-min-device-pixel-ratio: 2), /* Webkit-based browsers */ (min--moz-device-pixel-ratio: 2), /* Older Firefox browsers (prior to Firefox 16) */ (min-resolution: 2dppx), /* The standard way */ (min-resolution: 192dpi) { /* dppx fallback */ /* Retina-specific stuff here */ }
[倡议] 尽大概给出在高辨论率设备 (Retina) 下结果更佳的样式。
7 兼容性
7.1 属性前缀
[倡议] 带私有前缀的属性由长到短摆列,按冒号位置对齐。
说明:
标准属性放在最后,按冒号对齐利便阅读,也便于在编纂器内停止多行编纂。
在 Sublime Text 2 中, 使用 Selection → Add Previous Line (??↑) 和 Selection → Add Next Line (??↓)。
示例:
.box { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }
7.2 Hack
[倡议] 需要增加 hack 时应尽大概思考可否可以采纳其他方式解决。
说明:
假如能通过合理的 HTML 构造或使用其他的 CSS 定义到达抱负的样式,则不该该使用 hack 手段解决问题。平常 hack 会致使保护成本的增添。
[倡议] 尽量使用 选中器 hack 处置兼容性,而非 属性 hack。
说明:
尽量使用相符 CSS 语法的 selector hack,可以幸免一些第三方库没法识别 hack 语法的问题。
示例:
/* IE 7 */ *:first-child + html #header { margin-top: 3px; padding: 5px; } /* IE 6 */ * html #header { margin-top: 5px; padding: 4px; }
[倡议] 尽量使用简便的 属性 hack。
示例:
.box { _display: inline; /* fix double margin */ float: left; margin-left: 20px; } .container { overflow: hidden; *zoom: 1; /* triggering hasLayout */ }
7.3 Expression
[强迫] 制止使用 Expression。
7.4 @import
[强迫] 制止使用 @import。
说明:与 比拟, @import 更慢,需要额外的页面恳求,并且大概激发其他的意想不到的问题。应当幸免使用他们,而选中其他的方案:
● 使用多个 <link> 元素
● 使用 CSS 预处置器例如 Sass 或 Less 将样式编译到一个文件中
以上就是CSS编码标准的具体内容,更多请关注百分百源码网其它相关文章!