Home > Article > Web Front-end > HTML specifications you must know (organized and shared)
This article brings you knowledge about HTML specifications. HTML, as a hypertext markup language that describes the structure of web pages, has always been widely used. The goal of this document is to make the HTML code style consistent within internal development, making the project easier to understand and maintain. I hope everyone has to help.
HTML Specification
1 Preface
HTML, as a hypertext markup language that describes the structure of web pages, has always been widely used. The goal of this document is to make the HTML code style consistent within internal development, making the project easier to understand and maintain.
2 Code style
2.1 Indentation and line breaks
[Mandatory] Use 4 Spaces serve as an indentation level, and 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.
2.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.
[Mandatory] It is recommended that all words in id be lowercase and 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] It is forbidden to create classes without style information for hook scripts.
Explanation:
Class is not allowed. It is only used to let JavaScript select certain elements. Class should have clear semantics and style. Otherwise, it will easily lead to the proliferation of CSS classes.
Using id and attribute selection as hook is a better way.
[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>
2.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.
Explanation:
Exceptions can be made in scenarios with very strict requirements on code size. For example: the delivery system used by third-party pages.
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.
For detailed tag nesting rules, see the Elements definition section in HTML DTD.
[Recommendation] The use of HTML tags should follow the semantics of the tags.
Explanation:
The following are common tag semantics
p - paragraph
<!-- 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 should not be used for layout when 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 src="image.png"> <!-- bad --> <span> <img src="image.png"> </span>
2.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 (excluding component libraries such as iView and element). 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:
<input type="text" disabled> <input type="checkbox" value="1" 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>
3 Generic
##3.1 DOCTYPE[强制] 使用 HTML5 的 doctype 来启用标准模式,建议使用大写的 DOCTYPE。 示例: [建议] 启用 IE Edge 模式。 示例: [建议] 在 html 标签上设置正确的 lang 属性。 解释: 有助于提高页面的可访问性,如:让语音合成工具确定其所应该采用的发音,令翻译工具确定其翻译语言等。 示例: 3.2 编码 [强制] 页面必须使用精简形式,明确指定字符编码。指定字符编码的 meta 必须是 head 的第一个直接子元素。 解释: 见 HTML5 Charset能用吗 一文。 示例:<!DOCTYPE html>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<html>
<html>
......
......