無論有多少人共同參與同一項目,必定要確保每一行代碼都像是同一我的編寫的。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。
在文件結尾添加一個空白行。