</li>
or </body>
). </li>
<code><!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> </code>
Add a standard mode declaration to the first line of each HTML page to ensure consistent display in each browser.
<code><!DOCTYPE html> <html> <head> </head> </html> </code>
According to HTML5 specification:
It is highly recommended to specify the lang attribute for the html root element to set the correct language for the document. This will help speech synthesis tools determine the pronunciation they should use, help translation tools determine the rules they should follow when translating, and so on.
More knowledge about lang
attributes can be learned from this specification.
The language code table is listed here.
<code><html lang="zh-CN"> <!-- ... --> </html> </code>
IE supports specific <meta>
tags to determine the IE version that should be used to draw the current page. Unless there are strong special needs, it is best to set it to edge mode to notify IE to adopt the latest mode it supports.
Read this article on stack overflow for more useful information.
<code><meta http-equiv="X-UA-Compatible" content="IE=Edge"> </code>
By explicitly declaring the character encoding, you can ensure that the browser quickly and easily determines how the page content is rendered. The advantage of this is that you can avoid using character entities in HTML, so that everything is consistent with the document encoding (usually UTF-8 encoding).
<code><head> <meta charset="UTF-8"> </head> </code>
According to the HTML5 specification, there is generally no need to specify the type
attribute when introducing CSS and JavaScript files, because text/css
and text/javascript
are their default values respectively.
<code><!-- External CSS --> <link rel="stylesheet" href="code-guide.css"> <!-- In-document CSS --> <style> /* ... */ </style> <!-- JavaScript --> <script src="code-guide.js"></script> </code>
Try to follow HTML standards and semantics, but don’t sacrifice practicality. Try to use the fewest tags and keep complexity to a minimum at all times.
HTML attributes should be arranged in the order given below to ensure the readability of the code.
class
</li>
id
, name
</li>
data-*
</li>
src
, for
, type
, href
</li>
title
, alt
</li>
aria-*
, role
</li>
class is used to identify highly reusable components, so it should be listed first. The id is used to identify a specific component and should be used with caution (for example, bookmarks within a page), so it comes second.
<code><a class="..." id="..." data-modal="toggle" href="#"> Example link </a> <input class="form-control" type="text"> <img src="..." alt="..."> </code>
Boolean properties can be declared without assigning a value. The XHTML specification requires it to be assigned a value, but the HTML5 specification does not.
For more information, please refer to WhatWG section on boolean attributes:
If the Boolean attribute of the element has a value, it is true; if it has no value, it is false.
If must be assigned a value, please refer to the WhatWG specification:
If the attribute exists, its value must be an empty string or [...] The canonical name of the attribute, and do not add trailing whitespace characters.
To put it simply, there is no need to assign a value.
<code><input type="text" disabled> <input type="checkbox" value="1" checked> <select> <option value="1" selected>1</option> </select> </code>
When writing HTML code, try to avoid redundant parent elements. Many times, this requires iteration and refactoring to achieve. Please see the case below:
<code><!-- Not so great --> <span class="avatar"> <img src="..."> </span> <!-- Better --> <img class="avatar" src="..."> </code>
Tags generated through JavaScript make content difficult to find, edit, and reduce performance. Avoid it when you can.
:
后应该插入一个空格。
</li>
box-shadow
)。
</li>
rgb()
、rgba()
、hsl()
、hsla()
或 rect()
值的内部的逗号后面插入空格。这样利于从多个属性值(既加逗号也加空格)中区分多个颜色值(只加逗号,不加空格)。
</li>
.5
代替 0.5
;-.5px
代替 -0.5px
)。
</li>
#fff
。在扫描文档时,小写字符易于分辨,因为他们的形式更易于区分。
</li>
#fff
代替#ffffff
。
</li>
input[type="text"]
。只有在某些情况下是可选的,但是,为了代码的一致性,建议都加上双引号。
</li>
margin: 0;
代替margin: 0px;
。</li>
对于这里用到的术语有疑问吗?请参考 Wikipedia 上的 syntax section of the Cascading Style Sheets article。
<code>/* 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; } </code>
相关的属性声明应当归为一组,并按照下面的顺序排列:
由于定位(positioning)可以从正常的文档流中移除元素,并且还能覆盖盒模型(box model)相关的样式,因此排在首位。盒模型排在第二位,因为它决定了组件的尺寸和位置。
其他属性只是影响组件的内部(inside)或者是不影响前两组属性,因此排在后面。
完整的属性列表及其排列顺序请参考 Recess。
<code>.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; } </code>
@import
与 <link>
标签相比,@import
指令要慢很多,不光增加了额外的请求次数,还会导致不可预料的问题。替代办法有以下几种:
<link>
元素
</li>
请参考 Steve Souders 的文章了解更多知识。
<code><!-- Use link elements --> <link rel="stylesheet" href="core.css"> <!-- Avoid @imports --> <style> @import url("more.css"); </style> </code>
将媒体查询放在尽可能相关规则的附近。不要将他们打包放在一个单一样式文件中或者放在文档底部。如果你把他们分开了,将来只会被大家遗忘。下面给出一个典型的实例。
<code>.element { ... } .element-avatar { ... } .element-selected { ... } @media (min-width: 480px) { .element { ...} .element-avatar { ... } .element-selected { ... } } </code>
当使用特定厂商的带有前缀的属性时,通过缩进的方式,让每个属性的值在垂直方向对齐,这样便于多行编辑。
在 Textmate 中,使用 Text → Edit Each Line in Selection(⌃⌘A)。在 Sublime Text 2 中,使用 Selection → Add Previous Line (⌃⇧↑) 和 Selection → Add Next Line (⌃⇧↓)。
<code>/* Prefixed properties */ .selector { -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15); box-shadow: 0 1px 2px rgba(0,0,0,.15); } </code>
对于只包含一条声明的样式,为了易读性和便于快速编辑,建议将语句放在同一行。对于带有多条声明的样式,还是应当将声明分为多行。
这样做的关键因素是为了错误检测 -- 例如,CSS 校验器指出在 183 行有语法错误。如果是单行单条声明,你就不会忽略这个错误;如果是单行多条声明的话,你就要仔细分析避免漏掉错误了。
<code>/* 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; } </code>
在需要显示地设置所有值的情况下,应当尽量限制使用简写形式的属性声明。常见的滥用简写属性声明的情况如下:
padding
</li>
margin
</li>
font
</li>
background
</li>
border
</li>
border-radius
</li>
大部分情况下,我们不需要为简写形式的属性声明指定所有值。例如,HTML 的 heading 元素只需要设置上、下边距(margin)的值,因此,在必要的时候,只需覆盖这两个值就可以。过度使用简写形式的属性声明会导致代码混乱,并且会对属性值带来不必要的覆盖从而引起意外的副作用。
MDN(Mozilla Developer Network)上一片非常好的关于shorthand properties 的文章,对于不太熟悉简写属性声明及其行为的用户很有用。
<code>/* 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; } </code>
避免非必要的嵌套。这是因为虽然你可以使用嵌套,但是并不意味着应该使用嵌套。只有在必须将样式限制在父元素内(也就是后代选择器),并且存在多个需要嵌套的元素时才使用嵌套。
<code>// Without nesting .table > thead > tr > th { … } .table > thead > tr > td { … } // With nesting .table > thead > tr { > th { … } > td { … } } </code>
代码是由人编写并维护的。请确保你的代码能够自描述、注释良好并且易于他人理解。好的代码注释能够传达上下文关系和代码目的。不要简单地重申组件或 class 名称。
对于较长的注释,务必书写完整的句子;对于一般性注解,可以书写简洁的短语。
<code>/* Bad example */ /* Modal header */ .modal-header { ... } /* Good example */ /* Wrapping element for .modal-title and .modal-close */ .modal-header { ... } </code>
.btn
和 .btn-danger
)。
</li>
.btn
代表 button,但是 .s
不能表达任何意思。
</li>
.js-*
class 来标识行为(与样式相对),并且不要将这些 class 包含到 CSS 文件中。</li>
在为 Sass 和 Less 变量命名是也可以参考上面列出的各项规范。
<code>/* Bad example */ .t { ... } .red { ... } .header { ... } /* Good example */ .tweet { ... } .important { ... } .tweet-header { ... } </code>
[class^="..."]
)。浏览器的性能会受到这些因素的影响。
</li>
扩展阅读:
<code>/* Bad example */ span { ... } .page-container #stream .stream-item .tweet .tweet-header .username { ... } .avatar { ... } /* Good example */ .avatar { ... } .tweet-header .username { ... } .tweet .avatar { ... } </code>
<code>/* * 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 { ... } </code>
将你的编辑器按照下面的配置进行设置,以避免常见的代码不一致和差异: