转载:原地址css
CSS做为网页样式的描述语言,在百度一直有着普遍的应用。本文档的目标是使CSS代码风格保持一致,容易被理解和被维护。html
虽然本文档是针对CSS设计的,可是在使用各类CSS的预编译器(如less、sass、stylus等)时,适用的部分也应尽可能遵循本文档的约定。html5
CSS
文件使用无 BOM
的 UTF-8
编码。解释:git
UTF-8 编码具备更普遍的适应性。BOM 在使用程序或工具处理文件时可能形成没必要要的干扰。github
4
个空格作为一个缩进层级,不容许使用 2
个空格 或 tab
字符。示例:web
css.selector { margin: 0; padding: 0; }
选择器
与 {
之间必须包含空格。示例:浏览器
css.selector { }
属性名
与以后的 :
之间不容许包含空格, :
与 属性值
之间必须包含空格。示例:sass
cssmargin: 0;
列表型属性值
书写在单行时,,
后必须跟一个空格。示例:less
cssfont-family: Arial, sans-serif;
120
个字符,除非单行不可分割。解释:编辑器
常见不可分割的场景为URL超长。
空格
处或 ,
后换行,建议按逻辑分组。示例:
css/* 不一样属性值按逻辑分组 */ 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)) );
示例:
css/* good */ .post, .page, .comment { line-height: 1.5; } /* bad */ .post, .page, .comment { line-height: 1.5; }
>
、+
、~
选择器的两边各保留一个空格。示例:
css/* 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; }
解释:
不容许使用单引号,不容许不使用引号。
示例:
css/* good */ article[character="juliet"] { voice-family: "Vivien Leigh", victoria, female } /* bad */ article[character='juliet'] { voice-family: "Vivien Leigh", victoria, female }
示例:
css/* good */ .selector { margin: 0; padding: 0; } /* bad */ .selector { margin: 0; padding: 0; }
示例:
css/* good */ .selector { margin: 0; } /* bad */ .selector { margin: 0 }
id
、class
选择器添加类型选择器进行限定。解释:
在性能和维护性上,都有必定的影响。
示例:
css/* good */ #error, .danger-message { font-color: #c00; } /* bad */ dialog#error, p.danger-message { font-color: #c00; }
示例:
css/* good */ #username input {} .comment .avatar {} /* bad */ .page .header .login #username input {} .comment div * {}
示例:
css/* 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 等缩写会同时设置多个属性的值,容易覆盖不须要覆盖的设定。如某些方向须要继承其余声明的值,则应该分开设置。
示例:
css/* 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 */ }
解释:
position
/ top
/ right
/ bottom
/ left
/ float
/ display
/ overflow
等border
/ margin
/ padding
/ width
/ height
等font
/ line-height
/ text-align
/ word-wrap
等background
/ color
/ transition
/ list-style
等另外,若是包含 content
属性,应放在最前面。
示例:
css.sidebar { /* formatting model: positioning schemes / offsets / z-indexes / display / ... */ position: absolute; top: 50px; left: 0; overflow-x: hidden; /* 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; }
clear
或触发 BFC
的方式进行 clearfix
。尽可能不使用增长空标签的方式。解释:
触发 BFC 的方式不少,常见的有:
如但愿使用更小反作用的清除浮动方法,参见 A new micro clearfix hack 一文。
另需注意,对已经触发 BFC 的元素不须要再进行 clearfix。
!important
声明。!important
定义样式。解释:
必须注意的是,仅在设计上 确实不容许任何其它场景覆盖样式
时,才使用内联的 !important
样式。一般在第三方环境的应用中使用这种方案。下面的 z-index 章节是其中一个特殊场景的典型样例。
z-index
进行分层,对文档流外绝对定位元素的视觉层级关系进行管理。解释:
同层的多个元素,如多个由用户输入触发的 Dialog,在该层级内使用相同的 z-index
或递增 z-index
。
建议每层包含100个 z-index
来容纳足够的元素,若是每层元素较多,能够调整这个数值。
z-index
指定为 999999
。解释:
可控环境分红两种,一种是自身产品线环境;还有一种是可能会被其余产品线引用,可是不会被外部第三方的产品引用。
不建议取值为 2147483647
。以便于自身产品线被其余产品线引用时,当遇到层级覆盖冲突的状况,留出向上调整的空间。
!important
,将 z-index
指定为 2147483647
。解释:
第三方环境对于开发者来讲彻底不可控。在第三方环境下的元素,为了保证元素不被其页面其余样式定义覆盖,须要采用此作法。
解释:
文本类型的内容可能在选择器、属性值等内容中。
示例:
css/* 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: "”"; }
0
。示例:
css/* good */ panel { opacity: .8 } /* bad */ panel { opacity: 0.8 }
url()
函数中的路径不加引号。示例:
cssbody { background: url(bg.png); }
url()
函数中的绝对路径可省去协议名。示例:
cssbody { background: url(//baidu.com/img/bg.png) no-repeat 0 0; }
0
时须省略单位。 (也只有长度单位可省)示例:
css/* good */ body { padding: 0 5px; } /* bad */ body { padding: 0px 5px; }
#rrggbb
。不容许使用 rgb()
。解释:
带有alpha的颜色信息可使用 rgba()
。使用 rgba()
时每一个逗号后必须保留一个空格。
示例:
css/* 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); }
示例:
css/* good */ .success { background-color: #aca; } /* bad */ .success { background-color: #aaccaa; }
示例:
css/* good */ .success { color: #90ee90; } /* bad */ .success { color: lightgreen; }
示例:
css/* good */ .success { background-color: #aca; color: #90ee90; } /* good */ .success { background-color: #ACA; color: #90EE90; } /* bad */ .success { background-color: #ACA; color: #90ee90; }
解释:
2D 位置初始值为 0% 0%
,但在只有一个方向的值时,另外一个方向的值会被解析为 center。为避免理解上的困扰,应同时给出两个方向的值。background-position属性值的定义
示例:
css/* good */ body { background-position: center top; /* 50% 0% */ } /* bad */ body { background-position: top; /* 50% 0% */ }
font-family
属性中的字体族名称应使用字体的英文 Family Name
,其中若有空格,须放置在引号中。解释:
所谓英文 Family Name,为字体文件的一个元数据,常见名称以下:
字体 | 操做系统 | Family Name |
---|---|---|
宋体 (中易宋体) | Windows | SimSun |
黑体 (中易黑体) | Windows | SimHei |
微软雅黑 | Windows | Microsoft YaHei |
微软正黑 | Windows | Microsoft JhengHei |
华文黑体 | Mac/iOS | STHeiti |
冬青黑体 | Mac/iOS | Hiragino Sans GB |
文泉驿正黑 | Linux | WenQuanYi Zen Hei |
文泉驿微米黑 | Linux | WenQuanYi Micro Hei |
示例:
cssh1 { font-family: "Microsoft YaHei"; }
font-family
按「西文字体在前、中文字体在后」、「效果佳 (质量高/更能知足需求) 的字体在前、效果通常的字体在后」的顺序编写,最后必须指定一个通用字体族( serif
/ sans-serif
)。解释:
更详细说明可参考本文。
示例:
css/* 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
大小写必须统一。示例:
css/* 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; }
12px
。解释:
因为 Windows 的字体渲染机制,小于 12px 的文字显示效果极差、难以辨认。
normal
外的 font-style
。其余平台也应慎用。解释:
因为中文字体没有 italic 风格的实现,全部浏览器下都会 fallback 到 obilique 实现 (自动拟合为斜体),小字号下 (特别是 Windows 下会在小字号下使用点阵字体的状况下) 显示效果差,形成阅读困难。
font-weight
属性必须使用数值方式描述。解释:
CSS 的字重分 100 – 900 共九档,但目前受字体自己质量和浏览器的限制,实际上支持 400 和 700 两档,分别等价于关键词 normal 和 bold。
浏览器自己使用一系列启发式规则来进行匹配,在 =700 时匹配 Bold 字重。
但已有浏览器开始支持 =600 时匹配 Semibold 字重 (见此表),故使用数值描述增长了灵活性,也更简短。
示例:
css/* good */ h1 { font-weight: 700; } /* bad */ h1 { font-weight: bold; }
line-height
在定义文本段落时,应使用数值。解释:
将 line-height 设置为数值,浏览器会基于当前元素设置的 font-size 进行再次计算。在不一样字号的文本段落组合中,能达到较为温馨的行间间隔效果,避免在每一个设置了 font-size 都须要设置 line-height。
当 line-height 用于控制垂直居中时,仍是应该设置成与容器高度一致。
示例:
css.container { line-height: 1.5; }
transition
时应指定 transition-property
。示例:
css/* 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 做为动画属性。
示例:
css/* 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 */ }
Media Query
不得单独编排,必须与相关的规则一块儿定义。示例:
css/* 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
若是有多个逗号分隔的条件时,应将每一个条件放在单独一行中。示例:
css@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 */ }
解释:
标准属性放在最后,按冒号对齐方便阅读,也便于在编辑器内进行多行编辑。
示例:
css.box { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }
hack
时应尽量考虑是否能够采用其余方式解决。解释:
若是能经过合理的 HTML 结构或使用其余的 CSS 定义达到理想的样式,则不该该使用 hack 手段解决问题。一般 hack 会致使维护成本的增长。
选择器 hack
处理兼容性,而非 属性 hack
。解释:
尽可能使用符合 CSS 语法的 selector hack,能够避免一些第三方库没法识别 hack 语法的问题。
示例:
css/* IE 7 */ *:first-child + html #header { margin-top: 3px; padding: 5px; } /* IE 6 */ * html #header { margin-top: 5px; padding: 4px; }
属性 hack
。示例:
css.box { _display: inline; /* fix double margin */ float: left; margin-left: 20px; } .container { overflow: hidden; *zoom: 1; /* triggering hasLayout */ }
Expression
。