HTML Coding Specification
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 This habit, please choose your IDE carefully and don't use a "text editor" anymore.
1 Code style
1.1 Indentation and line breaks
[Mandatory] Use 4 spaces as an indent Level, 2 spaces or tab characters are not allowed.
Example:
<ul> <li>first</li> <li>second</li> </ul>
[Recommendation] Maximum 120 characters per line.
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 must contain all lowercase letters, and words must be separated by -.
[Mandatory] class must represent the content or function of the corresponding module or component, and must not be named with style information.
Example:
<!-- good --> <div></div> <!-- bad --> <div></div>
[Mandatory] The element id must be unique on the page.
Explanation:
In the same page, different elements contain the same id, which does not conform to the attribute meaning of id. And using document.getElementById can lead to hard-to-trace problems.
[Suggestion] It is recommended that all words in id be lowercase, and words should be separated by -. The style must be consistent for the same project.
[Recommendation] The id and class names should be as short as possible while avoiding conflicts and describing clearly.
Example:
<!-- good --> <div id="nav"></div> <!-- bad --> <div id="navigation"></div> <!-- good --> <p></p> <!-- bad --> <p></p> <!-- good --> <span></span> <!-- bad --> <span></span>
[Mandatory] Avoid using the same name and id on the same page.
Explanation:
IE browser will confuse the id and name attributes of elements, and document.getElementById may obtain unexpected elements. Therefore, you need to be very careful when naming the id and name attributes of elements.
A good practice is to use different naming conventions for id and name.
Example:
<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 require self-closing include input, br, img, hr, etc.
Example:
<!-- good --> <input type="text" name="title"> <!-- bad --> <input type="text" name="title" />
[Mandatory] For closing tags that are allowed to be omitted in HTML5, omission of the closing tag is not allowed.
Example:
<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>
[Recommendation] The use of HTML tags should follow the semantics of the tags.
Explanation:
The following are common tag semantics
p - 段落 h1,h2,h3,h4,h5,h6 - 层级标题 strong,em - 强调 ins - 插入 del - 删除 abbr - 缩写 code - 代码标识 cite - 引述来源作品的标题 q - 引用 blockquote - 一段或长篇引用 ul - 无序列表 ol - 有序列表 dl,dt,dd - 定义列表
Example:
<!-- 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>building block</span> for some JavaScript language tools.</div>
[Recommendation] Tables must not be used when CSS can achieve the same requirement Make the layout.
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 src="image.png"> <!-- bad --> <span> <img src="image.png"> </span>
1.4 Properties
[Mandatory] Property names must use lowercase letters.
Example:
<!-- good --> <table cellspacing="0">...</table> <!-- bad --> <table cellSpacing="0">...</table>
[Mandatory] The attribute value must be surrounded by double quotes.
Explanation:
Single quotation marks are not allowed, and no quotation marks are allowed.
Example:
<!-- good --> <script src="esl.js"></script> <!-- bad --> <script src='esl.js'></script> <script src=esl.js></script>
[Suggestion] For Boolean type attributes, it is recommended not to add attribute values.
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">
[Recommendation] It is recommended that custom attributes be prefixed with xxx- and data- is recommended.
Explanation:
Using prefixes helps distinguish custom properties from standard-defined properties.
Example:
<ol data-ui-type="Select"></ol>
2 General
2.1 DOCTYPE
[Mandatory] Use HTML5 doctype To enable standards mode, it is recommended to use uppercase DOCTYPE.
Example:
<!DOCTYPE html>
[Recommendation] Enable IE Edge and Chrome Frame mode.
Example:
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
[Suggestion] Set the correct lang attribute on the html tag.
Explanation:
It helps to improve the accessibility of the page, such as letting the speech synthesis tool determine the pronunciation it should use, letting the translation tool determine the language of translation, etc.
Example:
<html>
[Recommendation] Turn on the webkit kernel of the dual-core browser for rendering.
Explanation:
See the browser kernel control Meta tag documentation article.
Example:
<meta name="renderer" content="webkit">
[Recommendation] Turn on the browser's DNS prefetching.
Explanation:
Reduce the number of DNS requests and pre-obtain DNS.
Example:
<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 Encoding
[Mandatory] The page must use the condensed form and explicitly specify the character encoding. The meta specifying the character encoding must be the first direct child of head.
Explanation:
See the article Can HTML5 Charset be used?
Example:
<html> ...... ......