编写灵活、健壮、可维护性的 HTML 和 CSS 的标准。
在任何代码库中,无论有多少人参与及贡献,所有代码都应该如同一个人编写的一样。
这意味着应一直严格遵循这份指导。
不正确的例子:
<!DOCTYPE html>
<html>
<head>
<title>Page title</title>
</head>
<body>
<img src='images/company-logo.png' alt='Company' />
<h1 class='hello-world'>Hello, world!</h1>
</body>
</html>
正确的例子:
<!DOCTYPE html>
<html>
<head>
<title>Page title</title>
</head>
<body>
<img src="images/company-logo.png" alt="Company">
<h1 class="hello-world">Hello, world!</h1>
</body>
</html>
在每个 HTML 页面前面包含下面的 doctype 声明,尽可能地强制每个浏览器使用标准模式
<!DOCTYPE html>
努力保持 HTML 的标准和语义,但不要以牺牲实用为代价。使用尽可能少的标签和尽量避免复杂。
为了便于阅读代码,HTML 属性应该按照特定的顺序书写
因此你的代码看起来像:
<a class="" data-modal="" href="">Example link</a>
在 javascript 中生成标签让其难以查找、编辑和性能变差。因此,别这样做。
不正确的例子:
.selector, .selector-secondary, .selector[type=text] {
padding:15px;
margin:0px 0px 15px;
background-color:rgba(0, 0, 0, 0.5);
box-shadow:0 1px 2px #CCC,inset 0 1px 0 #FFFFFF
}
正确的例子
.selector,
.selector-secondary,
.selector[type="text"] {
padding: 15px;
margin: 0 0 15px;
background-color: rgba(0,0,0,.5);
box-shadow: 0 1px 2px #ccc, inset 0 1px 0 #fff;
}
将相关的属性组合在一起,并且将对结构来说比较重要的属性(如定位或者盒模型)放在前面,先于排版、背景及颜色等属性。
.declaration-order {
/* Positioning */
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 100;
/* Box-model */
display: block;
float: right;
width: 100px;
height: 100px;
/* Typography */
font: normal 13px "Helvetica Neue", sans-serif;
line-height: 1.5;
color: #333;
text-align: center;
/* Visual */
background-color: #f5f5f5;
border: 1px solid #e5e5e5;
border-radius: 3px;
/* Misc */
opacity: 1;
}
某些情况下,允许细微偏移原则。
使用带前缀的属性时,缩进每一个属性,让其值对齐,方便多行编辑。
.selector {
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
Vim 和 Sublime Text 等编辑器支持多行编辑。
对于大量仅包含单条声明的声明块,可以使用一种略微不同的单行格式。在这种情况下,在左大括号之后及右大括号之前都应该保留一个空格。
.span1 { width: 60px; }
.span2 { width: 140px; }
.span3 { width: 220px; }
.sprite {
display: inline-block;
width: 16px;
height: 15px;
background-image: url(../img/sprite.png);
}
.icon { background-position: 0 0; }
.icon-home { background-position: 0 -20px; }
.icon-account { background-position: 0 -40px; }
代码由人书写及维护,确保你的代码具有描述性、很好的注释和他人易于上手。
好的代码注释交代了上下文或者目的,而不应该只是注明了组件、类的名字。
不好的例子:
/* Modal header */
.modal-header {
...
}
好的例子:
/* Wrapping element for .modal-title and .modal-close */
.modal-header {
...
}
不好的例子:
.t { ... }
.red { ... }
.header { ... }
好的例子:
.tweet { ... }
.important { ... }
.tweet-header { ... }
不好的例子:
span { ... }
.page-container #stream .stream-item .tweet .tweet-header .username { ... }
.avatar { ... }
好的例子:
.avatar { ... }
.tweet-header .username { ... }
.tweet .avatar { ... }
千万不要用 CSS class 作为 JavaScript 钩子。如果你要把 JavaScript 和某些标记绑定起来的话,写一个 JavaScript 专用的 class,划定一个前缀.js-的命名空间,例如 .js-toggle , .js-drag-and-drop 。这意味着我们可以通过 class 同时绑定 JavaScript 和 CSS 而不会因为冲突而引发麻烦。
<th class="is-sortable js-is-sortable">
</th>
上面的这个标记有两个 class,你可以用其中一个来给这个可排序的表格栏添加样式,用另一个添加排序功能。

