无论有多少人共同参与同一项目,必定要确保每一行代码都像是同一我的编写的。javascript
<!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>
</head>
</html>
<html lang="en-us">
<!-- ... -->
</html>
<meta http-equiv="X-UA-Compatible"
content="IE=Edge">
<head>
<meta charset="UTF-8">
</head>
<!-- External CSS -->
<link rel="stylesheet" href="code-guide.css">
<!-- In-document CSS -->
<style>
/* ... */
</style>
<!-- JavaScript -->
<script src="code-guide.js"></script>
<a class="..." id="..." data-toggle="modal" href="#">
Example link
</a>
<input class="form-control" type="text">
<img src="..." alt="...">
元素的布尔型属性若是有值,就是 true,若是没有值,就是 false。css
若是必定要为其赋值的话,请参考 WhatWG 规范:html
若是属性存在,其值必须是空字符串或 […] 属性的规范名称,而且不要在首尾添加空白符。html5
简单来讲,就是不用赋值。java
<input type="text" disabled>
<input type="checkbox" value="1" checked>
<select>
<option value="1" selected>1</option>
</select>
<!-- Not so great -->
<span class="avatar">
<img src="...">
</span>
<!-- Better -->
<img class="avatar" src="...">
对于这里用到的术语有疑问吗?请参考 Wikipedia 上的
syntax section of the Cascading Style Sheets article。git
/* Bad CSS */
.selector, .selector-secondary, .selector[type=text] {
padding:15px;
margin:0px 0px 15px;
background-color:rgba(0, 0, 0, 0.5);
box-shadow:0px 1px 2px #CCC,inset 0 1px 0 #FFFFFF
}
/* Good CSS */
.selector,
.selector-secondary,
.selector[type="text"] {
padding: 15px;
margin-bottom: 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;
}
使用多个 <link> 元素
经过 Sass 或 Less 相似的 CSS 预处理器将多个 CSS 文件编译为一个文件
经过 Rails、Jekyll 或其余系统中提供过 CSS 文件合并功能
<!-- Use link elements -->
<link rel="stylesheet" href="core.css">
<!-- Avoid @imports -->
<style>
@import url("more.css");
</style>
.element { ... }
.element-avatar { ... }
.element-selected { ... }
@media (min-width: 480px) {
.element { ...}
.element-avatar { ... }
.element-selected { ... }
}
当使用特定厂商的带有前缀的属性时,经过缩进的方式,让每一个属性的值在垂直方向对齐,这样便于多行编辑。github
在 Textmate 中,使用 Text → Edit Each Line in Selection (⌃⌘A)。在 Sublime Text 2 中,使用 Selection → Add Previous Line (⌃⇧↑) 和 Selection → Add Next Line (⌃⇧↓)。web
/* Prefixed properties */
.selector {
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);
box-shadow: 0 1px 2px rgba(0,0,0,.15);
}
对于只包含一条声明的样式,为了易读性和便于快速编辑,建议将语句放在同一行。对于带有多条声明的样式,仍是应当将声明分为多行。bootstrap
这样作的关键因素是为了错误检测 – 例如,CSS 校验器指出在 183 行有语法错误。若是是单行单条声明,你就不会忽略这个错误;若是是单行多条声明的话,你就要仔细分析避免漏掉错误了。浏览器
/* Single declarations on one line */
.span1 { width: 60px; }
.span2 { width: 140px; }
.span3 { width: 220px; }
/* Multiple declarations, one per line */
.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; }
在须要显示地设置全部值的状况下,应当尽可能限制使用简写形式的属性声明。常见的滥用简写属性声明的状况以下:
padding
margin
font
background
border
border-radius
大部分状况下,咱们不须要为简写形式的属性声明指定全部值。例如,HTML 的 heading 元素只须要设置上、下边距(margin)的值,所以,在必要的时候,只需覆盖这两个值就能够。过分使用简写形式的属性声明会致使代码混乱,而且会对属性值带来没必要要的覆盖从而引发意外的反作用。
在 MDN(Mozilla Developer Network)上一篇很是好的关于shorthand properties 的文章,对于不太熟悉简写属性声明及其行为的用户颇有用。
/* Bad example */
.element {
margin: 0 0 10px;
background: red;
background: url("image.jpg");
border-radius: 3px 3px 0 0;
}
/* Good example */
.element {
margin-bottom: 10px;
background-color: red;
background-image: url("image.jpg");
border-top-left-radius: 3px;
border-top-right-radius: 3px;
}
// Without nesting
.table > thead > tr > th { … }
.table > thead > tr > td { … }
// With nesting
.table > thead > tr {
> th { … }
> td { … }
}
// Bad example
.element {
margin: 10px 0 @variable*2 10px;
}
// Good example
.element {
margin: 10px 0 (@variable * 2) 10px;
}
代码是由人编写并维护的。请确保你的代码可以自描述、注释良好而且易于他人理解。好的代码注释可以传达上下文关系和代码目的。不要简单地重申组件或 class 名称。
对于较长的注释,务必书写完整的句子;对于通常性注解,能够书写简洁的短语。
/* Bad example */
/* Modal header */
.modal-header {
...
}
/* Good example */
/* Wrapping element for .modal-title and .modal-close */
.modal-header {
...
}
/* Bad example */
.t { ... }
.red { ... }
.header { ... }
/* Good example */
.tweet { ... }
.important { ... }
.tweet-header { ... }
/* Bad example */
span { ... }
.page-container #stream .stream-item .tweet .tweet-header .username { ... }
.avatar { ... }
/* Good example */
.avatar { ... }
.tweet-header .username { ... }
.tweet .avatar { ... }
/*
* Component section heading
*/
.element { ... }
/*
* Component section heading
*
* Sometimes you need to include optional context for the entire component. Do that up here if it's important enough.
*/
.element { ... }
/* Contextual sub-component or modifer */
.element-heading { ... }
将你的编辑器按照下面的配置进行设置,以免常见的代码不一致和差别:
用两个空格代替制表符(soft-tab 即用空格表明 tab 符)。
保存文件时,删除尾部的空白符。
设置文件编码为 UTF-8。
在文件结尾添加一个空白行。