The goal of this document is to make the HTML code style consistent and easy to understand and maintain. If you don't have this habit, please choose your IDE carefully and don't use "text" editor".
1.1 Indentation and line breaks
[Mandatory] Use 4
spaces are used as an indentation level, and 2
spaces or tab
characters are not allowed.
Example:
<ul> <li>first</li> <li>second</li> </ul>
[Recommendation] Each line should not exceed 120
characters.
Explanation:
Code that is too long is not easy to read and maintain. However, considering the particularity of HTML, there are no hard requirements. Sublime, phpstorm, wenstorm, etc. all have ruler functions.
1.2 Naming
[Mandatory] class
All words must be lowercase, with ## between words #- separated.
[Mandatory] class must represent the content or function of the corresponding module or component and must not be named with style information.
<!-- good --> <div class="sidebar"></div> <!-- bad --> <div class="left"></div>
[Mandatory] Element id must be unique on the page.
[Suggestion] id It is recommended that all words be lowercase and separated by
-. The style must be consistent for the same project.
[Recommendation] id,
class Names should be as short as possible while avoiding conflicts and describing clearly.
<!-- good --> <div id="nav"></div> <!-- bad --> <div id="navigation"></div> <!-- good --> <p class="comment"></p> <!-- bad --> <p class="com"></p> <!-- good --> <span class="author"></span> <!-- bad --> <span class="red"></span>
[Mandatory] Avoid using the same name and
id on the same page.
<input name="foo"> <div id="foo"></div> <script> // IE6 将显示 INPUT alert(document.getElementById('foo').tagName); </script>
1.3 Tag
[Mandatory] Tag names must use lowercase letters. Example:<!-- good --> <p>Hello StyleGuide!</p> <!-- bad --> <P>Hello StyleGuide!</P>
[Mandatory] Self-closing is not allowed for labels that do not require self-closing.
Explanation: Common tags that do not need to be self-closing include input, br, img, hr, etc. Example:<!-- good --> <input type="text" name="title"> <!-- bad --> <input type="text" name="title" />
[Mandatory] For the closing tag that is allowed to be omitted in HTML5, the closing tag is not allowed to be omitted.
<ul> <li>first</li> <li>second</li> </ul>
[Mandatory] Tag usage must comply with tag nesting rules.
Explanation: For example, div must not be placed in p, and tbody must be placed in table. Example:<!-- good --> <div><h1><span></span></h1></div> <a href=""><span></span></a> <!-- bad --> <span><div></div></span> <p><div></div></p> <h1><div></div></h1> <h6><div></div></h6> <a href="a.html"><a href="a.html"></a></a>
[Suggestion] HTML The use of tags should follow the semantics of the tags.
<!-- good --> <p>Esprima serves as an important <strong>building block</strong> for some JavaScript language tools.</p> <!-- bad --> <div> Esprima serves as an important <span class="strong">building block</span> for some JavaScript language tools. </div>
CSS can achieve the same requirement. Explanation:
Semantic correctness should be maintained as much as possible when compatibility allows. Exceptions are allowed for scenarios with strict requirements on grid alignment and stretchability, such as complex forms with multiple columns.
[Recommendation] The use of tags should be as concise as possible and reduce unnecessary tags.Example:
<!-- good --> <img class="avatar" src="image.png"> <!-- bad --> <span class="avatar"> <img src="image.png"> </span>
1.4 Attributes
[Mandatory] Attribute names must use lowercase letters .Example:
<!-- good --> <table cellspacing="0">...</table> <!-- bad --> <table cellSpacing="0">...</table>
Explanation:
Single quotes are not allowed, and no quotes are not allowed.
Example:
<!-- good --> <script src="esl.js"></script> <!-- bad --> <script src='esl.js'></script> <script src=esl.js></script>
Example:
<!-- good --> <input type="text" disabled> <input type="checkbox" value="1" checked> <!-- bad --> <input type="text" disabled="disabled"> <input type="checkbox" value="1" checked="checked">
xxx-, and data-
is recommended. . Explanation:
Using prefixes helps distinguish custom properties from standard-defined properties.
Example:
<ol data-ui-type="Select"></ol>
2 Generic
2.1 DOCTYPE
[Mandatory] UseHTML5's doctype
to enable standards mode, it is recommended to use uppercase DOCTYPE
. Example:
<!DOCTYPE html>
Example:
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
[建议] 在 html
标签上设置正确的 lang 属性。
解释:
有助于提高页面的可访问性,如:让语音合成工具确定其所应该采用的发音,令翻译工具确定其翻译语言等。
示例:
<html lang="zh-CN">
[建议] 开启双核浏览器的 webkit
内核进行渲染。
解释:
见浏览器内核控制Meta标签说明文档 一文。
示例:
<meta name="renderer" content="webkit">
[建议] 开启浏览器的DNS预获取。
解释:
减少DNS请求次数、对DNS进行预获取。
示例:
<link rel="dns-prefetch" href="//global.zuzuche.com/"> <link rel="dns-prefetch" href="//imgcdn1.zuzuche.com/"> <link rel="dns-prefetch" href="//qiniucdn.com/">
2.2 编码
[强制] 页面必须使用精简形式,明确指定字符编码。指定字符编码的 meta
必须是 head
的第一个直接子元素。
解释:
见 HTML5 Charset能用吗 一文。
示例:
<html lang="zh-CN"> ...... ......