|
| 1 | +# HTML and CSS code guide |
| 2 | +开发灵活,耐用,可持续的HTML和CSS标准。 |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +---------- |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## 目录 |
| 11 | + |
| 12 | +* [黄金法则](#golden-rule) |
| 13 | +* [HTML](#html) |
| 14 | + * [语法](#html-syntax) |
| 15 | + * [HTML5 doctype](#html5-doctype) |
| 16 | + * [实用而非语义](#pragmatism-over-semantics) |
| 17 | + * [属性顺序](#attribute-order) |
| 18 | + * [JavaScript generated markup](#javascript-generated markup) |
| 19 | +* [CSS](#css) |
| 20 | + * [CSS syntax](#css-syntax) |
| 21 | + * [Declaration order](#declaration-order) |
| 22 | + * [Formatting exceptions](#formatting-exceptions) |
| 23 | + * [Prefixed properties](#prefixed-properties) |
| 24 | + * [Rules with single declarations](#rules-with-single-declarations) |
| 25 | + * [Human readable](#human-readable) |
| 26 | + * [Comments](#comments) |
| 27 | + * [Classes](#classes) |
| 28 | + * [Selectors](#selectors) |
| 29 | + * [Organization](#organization) |
| 30 | +* [Writing copy](#copy) |
| 31 | + * [Sentence case](#sentence-case) |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +---------- |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +## 黄金法则 |
| 40 | + |
| 41 | +> 任何代码库中的所有代码应该看起来像是一个人书写的,不管有多少人贡献过代码。 |
| 42 | +
|
| 43 | +这意味着任何时候都要严格执行这些商定的准则。对于增加或减少代码的法则,请参看 [file an issue on GitHub](https://github.com/mdo/code-guide). |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +---------- |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +## HTML |
| 52 | + |
| 53 | + |
| 54 | +### HTML 语法 |
| 55 | + |
| 56 | +* 使用两个空格的 soft-tabs |
| 57 | +* 应每层嵌套元素缩进一次(2个空格) |
| 58 | +* 请务必实用双引号,而非单引号 |
| 59 | +* 不要自闭元素包括一个斜线 |
| 60 | + |
| 61 | +**错误示例:** |
| 62 | + |
| 63 | +````html |
| 64 | +<!DOCTYPE html> |
| 65 | +<html> |
| 66 | +<head> |
| 67 | +<title>Page title</title> |
| 68 | +</head> |
| 69 | +<body> |
| 70 | +<img src='images/company-logo.png' alt='Company' /> |
| 71 | +<h1 class='hello-world'>Hello, world!</h1> |
| 72 | +</body> |
| 73 | +</html> |
| 74 | +```` |
| 75 | + |
| 76 | +**正确示例:** |
| 77 | + |
| 78 | +````html |
| 79 | +<!DOCTYPE html> |
| 80 | +<html> |
| 81 | + <head> |
| 82 | + <title>Page title</title> |
| 83 | + </head> |
| 84 | + <body> |
| 85 | + <img src="images/company-logo.png" alt="Company"> |
| 86 | + <h1 class="hello-world">Hello, world!</h1> |
| 87 | + </body> |
| 88 | +</html> |
| 89 | +```` |
| 90 | + |
| 91 | + |
| 92 | +### HTML5 doctype |
| 93 | + |
| 94 | +在每个浏览器可以用这个简单的DOCTYPE,故在每个HTML页面开始强制执行的标准模式。 |
| 95 | + |
| 96 | +````html |
| 97 | +<!DOCTYPE html> |
| 98 | +```` |
| 99 | + |
| 100 | + |
| 101 | +### 实用大于语义 |
| 102 | + |
| 103 | +努力保持HTML的标准和语义,但不要牺牲实用性。用最少的复杂度尽可能少的标签实现需求。 |
| 104 | + |
| 105 | +### 属性顺序 |
| 106 | + |
| 107 | +HTML 属性应该遵循特定的顺序,以便能更易阅读代码。 |
| 108 | + |
| 109 | +* class |
| 110 | +* id |
| 111 | +* data-* |
| 112 | +* for|type|href |
| 113 | + |
| 114 | +比如你的代码看起来应该像这样: |
| 115 | + |
| 116 | +````html |
| 117 | +<a class="" id="" data-modal="" href="">链接示例</a> |
| 118 | +```` |
| 119 | + |
| 120 | +### JavaScript 生成标记 |
| 121 | + |
| 122 | +使用JavaScript生成标记,会使内容更难找到,更难编辑且性能更低。切记不要这样做。 |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +---------- |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +## CSS |
| 131 | + |
| 132 | +### CSS 语法 |
| 133 | + |
| 134 | +* 使用两个空格的 soft-tabs |
| 135 | +* 写组选择器时,保持选择器各占一行 |
| 136 | +* 在声明块的左 “{” 前应该有一个空格 |
| 137 | +* 关闭块的 “}” 要新行显示 |
| 138 | +* 在每个属性的 <code>:</code> 后包含一个空格 |
| 139 | +* 每个声明应该自己独占其行 |
| 140 | +* 每个属性以 “;” 结尾 |
| 141 | +* 分割选择器的 “,” 后应该包含一个空格 |
| 142 | +* Don't include spaces after commas in RGB or RGBa colors, and don't preface values with a leading zero |
| 143 | +* 小写所有16进制值, 例如, <code>#fff</code> 而非 <code>#FFF</code> |
| 144 | +* 使用简写16进制值, 例如, <code>#fff</code> 而非 <code>#ffffff</code> |
| 145 | +* 选择器中引用属性值, 例如, <code>input[type="text"]</code> |
| 146 | +* 避免0值设置单位, 例如, <code>margin: 0;</code> 而非 <code>margin: 0px;</code> |
| 147 | + |
| 148 | +**错误示例:** |
| 149 | + |
| 150 | +````css |
| 151 | +.selector, .selector-secondary, .selector[type=text] { |
| 152 | + padding:15px; |
| 153 | + margin:0px 0px 15px; |
| 154 | + background-color:rgba(0, 0, 0, 0.5); |
| 155 | + box-shadow:0 1px 2px #CCC,inset 0 1px 0 #FFFFFF |
| 156 | +} |
| 157 | +```` |
| 158 | + |
| 159 | +**正确示例:** |
| 160 | + |
| 161 | +````css |
| 162 | +.selector, |
| 163 | +.selector-secondary, |
| 164 | +.selector[type="text"] { |
| 165 | + padding: 15px; |
| 166 | + margin: 0 0 15px; |
| 167 | + background-color: rgba(0,0,0,.5); |
| 168 | + box-shadow: 0 1px 2px #ccc, inset 0 1px 0 #fff; |
| 169 | +} |
| 170 | +```` |
| 171 | + |
| 172 | +这里使用的术语有问题?参见 [syntax section of the Cascading Style Sheets article](http://en.wikipedia.org/wiki/Cascading_Style_Sheets#Syntax) on Wikipedia. |
| 173 | + |
| 174 | + |
| 175 | +### 属性顺序 |
| 176 | + |
| 177 | +相关属性应放在一起,将定位与盒模型属性写在最前面,其次是排版和视觉效果的属性。 |
| 178 | + |
| 179 | +````css |
| 180 | +.declaration-order { |
| 181 | + /* Positioning 定位 */ |
| 182 | + position: absolute; |
| 183 | + top: 0; |
| 184 | + right: 0; |
| 185 | + bottom: 0; |
| 186 | + left: 0; |
| 187 | + z-index: 100; |
| 188 | + |
| 189 | + /* Box-model 盒模型 */ |
| 190 | + display: block; |
| 191 | + float: right; |
| 192 | + width: 100px; |
| 193 | + height: 100px; |
| 194 | + |
| 195 | + /* Typography 排版 */ |
| 196 | + font: normal 13px "Helvetica Neue", sans-serif; |
| 197 | + line-height: 1.5 |
| 198 | + color: #333; |
| 199 | + text-align: center; |
| 200 | + |
| 201 | + /* Visual 视觉 */ |
| 202 | + background-color: #f5f5f5; |
| 203 | + border: 1px solid #e5e5e5; |
| 204 | + border-radius: 3px; |
| 205 | + |
| 206 | + /* Misc 其他 */ |
| 207 | + opacity: 1; |
| 208 | +} |
| 209 | +```` |
| 210 | + |
| 211 | +关于属性顺序的完整列表,请参考 [Recess](http://twitter.github.com/recess). |
| 212 | + |
| 213 | + |
| 214 | +### 格式化例外 |
| 215 | + |
| 216 | +某些情况下,这是有道理的,稍微偏离默认的 [语法](#css-syntax). |
| 217 | + |
| 218 | +#### 前缀属性 |
| 219 | + |
| 220 | +当使用供应商前缀的属性时,每个属性缩进到vlaue值垂直对齐的位置,方便多行编辑。 |
| 221 | + |
| 222 | +````css |
| 223 | +.selector { |
| 224 | + -webkit-border-radius: 3px; |
| 225 | + -moz-border-radius: 3px; |
| 226 | + border-radius: 3px; |
| 227 | +} |
| 228 | +```` |
| 229 | + |
| 230 | +在 Textmate、Sublime Text 2 以及 notepad++等工具中, 都支持多行编辑。 |
| 231 | + |
| 232 | +In Textmate, use **Text → Edit Each Line in Selection** (⌃⌘A). In Sublime Text 2, use **Selection → Add Previous Line** (⌃⇧↑) and **Selection → Add Next Line** (⌃⇧↓). |
| 233 | + |
| 234 | +#### 单一属性的书写规则 |
| 235 | + |
| 236 | +在有些情况下,每个样式只有一个属性,考虑到可读性及更快地编辑删除等,保持同一行书写。 |
| 237 | + |
| 238 | +````css |
| 239 | +.span1 { width: 60px; } |
| 240 | +.span2 { width: 140px; } |
| 241 | +.span3 { width: 220px; } |
| 242 | + |
| 243 | +.sprite { |
| 244 | + display: inline-block; |
| 245 | + width: 16px; |
| 246 | + height: 15px; |
| 247 | + background-image: url(../img/sprite.png); |
| 248 | +} |
| 249 | +.icon { background-position: 0 0; } |
| 250 | +.icon-home { background-position: 0 -20px; } |
| 251 | +.icon-account { background-position: 0 -40px; } |
| 252 | +```` |
| 253 | + |
| 254 | + |
| 255 | +### 可读性 |
| 256 | + |
| 257 | +代码是由人来书写和维护的。确保你的代码有很好的注释描述,以便他人使用。 |
| 258 | + |
| 259 | +#### 注释 |
| 260 | + |
| 261 | +好的代码都有一个良好的代码注释而不仅仅是一个类名 |
| 262 | + |
| 263 | +**Bad example:** |
| 264 | + |
| 265 | +````css |
| 266 | +/* Modal header */ |
| 267 | +.modal-header { |
| 268 | + ... |
| 269 | +} |
| 270 | +```` |
| 271 | + |
| 272 | +**Good example:** |
| 273 | + |
| 274 | +````css |
| 275 | +/* Wrapping element for .modal-title and .modal-close */ |
| 276 | +.modal-header { |
| 277 | + ... |
| 278 | +} |
| 279 | +```` |
| 280 | + |
| 281 | +#### 类名 |
| 282 | + |
| 283 | +* 保持类名使用小写字母或连接符 (不要使用下划线或驼峰命名法) |
| 284 | +* 避免使用随意的首字符命名法 |
| 285 | +* 保持命名尽可能短并简洁 |
| 286 | +* 使用有意义的命名;使用结构化或目的性的意义名称 |
| 287 | +* 根据最近的父组件基类作为命名前缀 |
| 288 | + |
| 289 | +**Bad example:** |
| 290 | + |
| 291 | +````css |
| 292 | +.t { ... } |
| 293 | +.red { ... } |
| 294 | +.header { ... } |
| 295 | +```` |
| 296 | + |
| 297 | +**Good example:** |
| 298 | + |
| 299 | +````css |
| 300 | +.tweet { ... } |
| 301 | +.important { ... } |
| 302 | +.tweet-header { ... } |
| 303 | +```` |
| 304 | + |
| 305 | +#### 选择器 |
| 306 | + |
| 307 | +* 在通用的元素标签中使用类 |
| 308 | +* 要保持尽量简短,并限制每个选择器最多三个class |
| 309 | +* 必要时使用最近的父类 (如,在不使用前缀类时) |
| 310 | + |
| 311 | +**Bad example:** |
| 312 | + |
| 313 | +````css |
| 314 | +span { ... } |
| 315 | +.page-container #stream .stream-item .tweet .tweet-header .username { ... } |
| 316 | +.avatar { ... } |
| 317 | +```` |
| 318 | + |
| 319 | +**Good example:** |
| 320 | + |
| 321 | +````css |
| 322 | +.avatar { ... } |
| 323 | +.tweet-header .username { ... } |
| 324 | +.tweet .avatar { ... } |
| 325 | +```` |
| 326 | + |
| 327 | +### 组织 |
| 328 | + |
| 329 | +* 组织代码段的组成部分 |
| 330 | +* 指定一个一致的注释层次结构 |
| 331 | +* 如果使用多个css文件,则按组件进行划分 |
| 332 | + |
| 333 | + |
| 334 | + |
| 335 | +---------- |
| 336 | + |
| 337 | + |
| 338 | + |
| 339 | +## 复制 |
| 340 | + |
| 341 | +### Sentence case |
| 342 | + |
| 343 | +务必写文案,包括标题和代码注释,in [sentence case](http://en.wikipedia.org/wiki/Letter_case#Usage). 换句话说,除了标题和专有名词,只有第一个字应予以资本化。 |
| 344 | + |
| 345 | + |
| 346 | + |
| 347 | +---------- |
| 348 | + |
| 349 | + |
| 350 | + |
| 351 | +### Thanks |
| 352 | + |
| 353 | +Heavily inspired by [Idiomatic CSS](https://github.com/necolas/idiomatic-css) and the [GitHub Styleguide](http://github.com/styleguide). Made with all the love in the world by [@mdo](http://twitter.com/mdo). |
0 commit comments