CSS 作为网页样式的描述语言,在百度一直有着广泛的应用。本文档的目标是使 CSS 代码风格保持一致,容易被理解和被维护。
虽然本文档是针对 CSS 设计的,但是在使用各种 CSS 的预编译器(如 less、sass、stylus 等)时,适用的部分也应尽量遵循本文档的约定。
解释:
UTF-8 编码具有更广泛的适应性。BOM 在使用程序或工具处理文件时可能造成不必要的干扰。
示例:
.selector { margin: 0; padding: 0; }
示例:
.selector { }
示例:
margin: 0;
示例:
font-family: Arial, sans-serif;
解释:
常见不可分割的场景为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)) );
示例:
/* 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; }
示例:
/* good */ .selector { margin: 0; padding: 0; } /* bad */ .selector { margin: 0; padding: 0; }
示例:
/* good */ .selector { margin: 0; } /* bad */ .selector { margin: 0 }
解释:
在性能和维护性上,都有一定的影响。
示例:
/* 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 * {}
示例:
/* 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等缩写会同时设置多个属性的值,容易覆盖不需要覆盖的设定。如某些方向需要继承其他声明的值,则应该分开设置。
示例:
/* centering
解释:
另外,如果包含content属性,应放在最前面。
示例:
.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; }
解释:
触发 BFC 的方式很多,常见的有:
如希望使用更小副作用的清除浮动方法,参见A new micro clearfix hack一文。
另需注意,对已经触发 BFC 的元素不需要再进行 clearfix。
解释:
必须注意的是,仅在设计上确实不允许任何其它场景覆盖样式时,才使用内联的!important样式。通常在第三方环境的应用中使用这种方案。下面的z-index章节是其中一个特殊场景的典型样例。
解释:
同层的多个元素,如多个由用户输入触发的 Dialog,在该层级内使用相同的z-index或递增z-index。
建议每层包含100个z-index来容纳足够的元素,如果每层元素较多,可以调整这个数值。
解释:
可控环境分成两种,一种是自身产品线环境;还有一种是可能会被其他产品线引用,但是不会被外部第三方的产品引用。
不建议取值为2147483647。以便于自身产品线被其他产品线引用时,当遇到层级覆盖冲突的情况,留出向上调整的空间。
解释:
第三方环境对于开发者来说完全不可控。在第三方环境下的元素,为了保证元素不被其页面其他样式定义覆盖,需要采用此做法。
解释:
文本类型的内容可能在选择器、属性值等内容中。
示例:
/* 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: "”"; }
示例:
/* good */ panel { opacity: .8; } /* bad */ panel { opacity: 0.8; }
示例:
body { background: url(bg.png); }
示例:
body { background: url(../../img/bg.png) no-repeat 0 0; }
示例:
/* good */ body { padding: 0 5px; } /* bad */ body { padding: 0px 5px; }
解释:
带有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; }
解释:
2D 位置初始值为0% 0%,但在只有一个方向的值时,另一个方向的值会被解析为 center。为避免理解上的困扰,应同时给出两个方向的值。background-position属性值的定义
示例:
/* good */ body { background-position: center top; /* 50% 0% */ } /* bad */ body { background-position: top; /* 50% 0% */ }
解释:
所谓英文 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 |
示例:
h1 { font-family: "Microsoft YaHei"; }
解释:
更详细说明可参考本文。
示例:
/* 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; }
示例:
/* 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; }
解释:
由于 Windows 的字体渲染机制,小于12px的文字显示效果极差、难以辨认。
解释:
由于中文字体没有italic风格的实现,所有浏览器下都会 fallback 到obilique实现 (自动拟合为斜体),小字号下 (特别是 Windows 下会在小字号下使用点阵字体的情况下) 显示效果差,造成阅读困难。
解释:
CSS 的字重分 100 – 900 共九档,但目前受字体本身质量和浏览器的限制,实际上支持400和700两档,分别等价于关键词normal和bold。
浏览器本身使用一系列启发式规则来进行匹配,在<700时一般匹配字体的 Regular 字重,>=700时匹配 Bold 字重。
但已有浏览器开始支持=600时匹配 Semibold 字重 (见此表),故使用数值描述增加了灵活性,也更简短。
示例:
/* good */ h1 { font-weight: 700; } /* bad */ h1 { font-weight: bold; }
解释:
将line-height设置为数值,浏览器会基于当前元素设置的font-size进行再次计算。在不同字号的文本段落组合中,能达到较为舒适的行间间隔效果,避免在每个设置了font-size都需要设置line-height。
当line-height用于控制垂直居中时,还是应该设置成与容器高度一致。
示例:
.container { line-height: 1.5; }
示例:
/* good */ .box { transition: color 1s, border-color 1s; } /* bad */ .box { transition: all 1s; }
解释:
见本文,在可能的情况下应选择这样四种变换:
典型的,可以使用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 */ }
示例:
/* 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 (-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 */ }
解释:
标准属性放在最后,按冒号对齐方便阅读,也便于在编辑器内进行多行编辑。
示例:
.box { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }
解释:
如果能通过合理的 HTML 结构或使用其他的 CSS 定义达到理想的样式,则不应该使用 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; }
示例:
.box { _display: inline; /* fix double margin */ float: left; margin-left: 20px; } .container { overflow: hidden; *zoom: 1; /* triggering hasLayout */ }