本文档的目的是使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编码范例的细致内容,更多请关注ki4网别的相干文章!