HTML coding standards

藏色散人
Release: 2019-10-29 11:55:44
forward
3192 people have browsed it

HTML coding standards

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:

  • first
  • second
Copy after login

[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:

 
Copy after login

[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:

     

Copy after login

[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:

 
Copy after login

1.3 Tag

[Mandatory] Tag names must use lowercase letters.

Example:

 

Hello StyleGuide!

Hello StyleGuide!

Copy after login

[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:

   
Copy after login

[Mandatory] For closing tags that are allowed to be omitted in HTML5, omission of the closing tag is not allowed.

Example:

         
  • first
  • second
  • first
  • second
Copy after login

[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:

 

Copy after login

[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 - 定义列表
Copy after login

Example:

 

Esprima serves as an important building block for some JavaScript language tools.

Esprima serves as an important building block for some JavaScript language tools.
Copy after login

[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:

     
Copy after login

1.4 Properties

[Mandatory] Property names must use lowercase letters.

Example:

 ...
...
Copy after login

[Mandatory] The attribute value must be surrounded by double quotes.

Explanation:

Single quotation marks are not allowed, and no quotation marks are allowed.

Example:

    
Copy after login

[Suggestion] For Boolean type attributes, it is recommended not to add attribute values.

Example:

     
Copy after login

[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:

    Copy after login

    2 General

    2.1 DOCTYPE

    [Mandatory] Use HTML5 doctype To enable standards mode, it is recommended to use uppercase DOCTYPE.

    Example:

    
            
    Copy after login

    [Recommendation] Enable IE Edge and Chrome Frame mode.

    Example:

    Copy after login

    [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:

    
            
    Copy after login

    [Recommendation] Turn on the webkit kernel of the dual-core browser for rendering.

    Explanation:

    See the browser kernel control Meta tag documentation article.

    Example:

    Copy after login

    [Recommendation] Turn on the browser's DNS prefetching.

    Explanation:

    Reduce the number of DNS requests and pre-obtain DNS.

    Example:

      
    Copy after login

    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:

    
             ......
             
    ......
    Copy after login

    [Recommendation] Use UTF-8 encoding without BOM for HTML files.

    Explanation:

    UTF-8 encoding has wider adaptability. BOMs can cause unnecessary interference when using programs or tools to process files.

    2.3 CSS and JavaScript introduction

    [Mandatory] When introducing CSS, you must specify rel="stylesheet".

    Example:

    Copy after login

    [Suggestion] There is no need to specify the type attribute when introducing CSS and JavaScript.

    解释:

    text/css 和 text/javascript 是 type 的默认值。

    [建议] 展现定义放置于外部 CSS 中,行为定义放置于外部 JavaScript 中。

    解释:

    结构-样式-行为的代码分离,对于提高代码的可阅读性和维护性都有好处。

    [建议] 在 head 中引入页面需要的所有 CSS 资源。

    解释:

    在页面渲染的过程中,新的CSS可能导致元素的样式重新计算和绘制,页面闪烁。

    [建议] JavaScript 应当放在页面末尾,或采用异步加载。

    解释:

    将 script 放在页面中间将阻断页面的渲染。出于性能方面的考虑,如非必要,请遵守此条建议。

    示例:

       
    Copy after login

    [强制] 引用静态资源的 URL 协议部分与页面相同,建议省略协议前缀。

    示例:

    Copy after login

    3 Head

    3.1 title

    [强制] 页面必须包含 title 标签声明标题。

    [强制] title 必须作为 head 的直接子元素,并紧随声明之后。

    解释:

    title 中如果包含 ascii 之外的字符,浏览器需要知道字符编码类型才能进行解码,否则可能导致乱码。

    示例:

         页面标题 
    
    Copy after login

    4 图片

    [强制] 禁止 img 的 src 取值为空。延迟加载的图片也要增加默认的 src。

    解释:

    src 取值为空,会导致部分浏览器重新加载一次当前页面,参考:

    https://developer.yahoo.com/performance/rules.html#emptysrc
    Copy after login

    [建议] 避免为 img 添加不必要的 title 属性。

    解释:

    多余的 title 影响看图体验,并且增加了页面尺寸。

    [建议] 为重要图片添加 alt 属性。

    解释:

    可以提高图片加载失败时的用户体验。

    [建议] 添加 width 和 height 属性,以避免页面抖动。

    [建议] 有下载需求的图片采用 img 标签实现,无下载需求的图片采用 CSS 背景图实现。

    解释:

    产品 logo、用户头像、用户产生的图片等有潜在下载需求的图片,以 img 形式实现,能方便用户下载。

    无下载需求的图片,比如:icon、背景、代码使用的图片等,尽可能采用 css 背景图实现。

    5 表单

    5.1 控件标题

    [强制] 有文本标题的控件必须使用 label 标签将其与其标题相关联。

    解释:

    有两种方式:

    将控件置于 label 内。

    label 的 for 属性指向控件的 id。

    推荐使用第一种,减少不必要的 id。如果 DOM 结构不允许直接嵌套,则应使用第二种。

    示例:

      
    Copy after login

    5.2 按钮

    [强制] 使用 button 元素时必须指明 type 属性值。

    解释:

    button 元素的默认 type 为 submit,如果被置于 form 元素中,点击后将导致表单提交。为显示区分其作用方便理解,必须给出 type 属性。

    示例:

     
    Copy after login

    [建议] 尽量不要使用按钮类元素的 name 属性。

    解释:

    由于浏览器兼容性问题,使用按钮的 name 属性会带来许多难以发现的问题。具体情况可参考此文。

    5.3 可访问性 (A11Y)

    [建议] 负责主要功能的按钮在 DOM 中的顺序应靠前。

    解释:

    负责主要功能的按钮应相对靠前,以提高可访问性。如果在 CSS 中指定了 float: right 则可能导致视觉上主按钮在前,而 DOM 中主按钮靠后的情况。

    示例:

      
    Copy after login

    [建议] 当使用 JavaScript 进行表单提交时,如果条件允许,应使原生提交功能正常工作。

    解释:

    当浏览器 JS 运行错误或关闭 JS 时,提交功能将无法工作。如果正确指定了 form 元素的 action 属性和表单控件的 name 属性时,提交仍可继续进行。

    示例:

    Copy after login

    [建议] 在针对移动设备开发的页面时,根据内容类型指定输入框的 type 属性。

    解释:

    根据内容类型指定输入框类型,能获得能友好的输入体验。

    示例:

       
    Copy after login

    6 模板中的 HTML

    [建议] 模板代码的缩进优先保证 HTML 代码的缩进规则。

    示例:

      
    • {name}
    • {$item.name}
    Copy after login

    [建议] 模板代码应以保证 HTML 单个标签语法的正确性为基本原则。

    示例:

     
  1. {type_name}
  2. >{type_name}
  3. Copy after login

    [建议] 模板代码应以保证结束符的闭合名

    示例:

      
    • {name}:¥{unit_price}
    • {name}:¥{unit_price}
    Copy after login

    [建议] 在循环处理模板数据构造表格时,若要求每行输出固定的个数,建议先将数据分组,之后再循环输出,模板只是做数据展示,别加插太多业务逻辑(其他数据构造同理)。

    示例:

     
    {name}:¥{unit_price}
    {name}:¥{unit_price}
    {name}:¥{price}
    {name}: ¥{unit_price} ¥{total_price}
    Copy after login

    The above is the detailed content of HTML coding standards. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:zuzuche.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!