Learn more about coding conventions in HTML

青灯夜游
Release: 2020-10-22 19:40:17
forward
2646 people have browsed it

Learn more about coding conventions in HTML

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 Code style

1.1 Indentation and line breaks

[Mandatory] Use4spaces are used as an indentation level, and2spaces ortabcharacters are not allowed.

Example:

  • first
  • second
Copy after login

[Recommendation] Each line should not exceed120characters.

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]classAll words must be lowercase, with ## between words #-separated.

[Mandatory]classmust represent the content or function of the corresponding module or component and must not be named with style information.

Example:

   
Copy after login

[Mandatory] Elementidmust 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]idIt is recommended that all words be lowercase and separated by-. The style must be consistent for the same project.

[Recommendation]id,classNames should be as short as possible while avoiding conflicts and describing clearly.

Example:

     

Copy after login

[Mandatory] Avoid using the samenameandidon 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 need to be self-closing include input, br, img, hr, etc.

Example:

   
Copy after login

[Mandatory] For the closing tag that is allowed to be omitted inHTML5, the closing tag is not allowed to be omitted.

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

[Suggestion]HTMLThe use of tags should follow the semantics of the tags.

Explanation:

The following are common tag semantics

  • p - Paragraph

  • ##h1 ,h2,h3,h4,h5,h6 - hierarchical title
  • strong, em - emphasis
  • ins - insert
  • del - delete
  • abbr - abbreviation
  • code - code identification
  • cite - the title of the work from which the citation is derived
  • q - the quotation
  • blockquote - a paragraph or long quotation
  • ul - unordered list
  • ol - ordered list
  • dl,dt,dd - definition list
  • 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 should not be used for layout when

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

     
Copy after login

1.4 Attributes

[Mandatory] Attribute names must use lowercase letters .

Example:

 ...
...
Copy after login

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

Explanation:

Single quotes are not allowed, and no quotes are not allowed.

Example:

    
Copy after login

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

Example:

     
Copy after login

[Recommendation] Custom attributes are recommended to be prefixed with

xxx-, anddata-is recommended. .Explanation:

Using prefixes helps distinguish custom properties from standard-defined properties.

Example:

    Copy after login

    2 Generic

    2.1 DOCTYPE

    [Mandatory] Use

    HTML5'sdoctypeto enable standards mode, it is recommended to use uppercaseDOCTYPE.Example:

    
            
    Copy after login

    [Recommended] Enable IE Edge and Chrome Frame mode.

    Example:

    Copy after login

    [建议] 在html标签上设置正确的 lang 属性。

    解释:

    有助于提高页面的可访问性,如:让语音合成工具确定其所应该采用的发音,令翻译工具确定其翻译语言等。

    示例:

    
            
    Copy after login

    [建议] 开启双核浏览器的webkit内核进行渲染。

    解释:

    浏览器内核控制Meta标签说明文档一文。

    示例:

    Copy after login

    [建议] 开启浏览器的DNS预获取。

    解释:

    减少DNS请求次数、对DNS进行预获取。

    示例:

      
    Copy after login

    2.2 编码

    [强制] 页面必须使用精简形式,明确指定字符编码。指定字符编码的meta必须是head的第一个直接子元素。

    解释:

    HTML5 Charset能用吗一文。

    示例:

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

    [建议]HTML文件使用无BOMUTF-8编码。

    解释:

    UTF-8 编码具有更广泛的适应性。BOM 在使用程序或工具处理文件时可能造成不必要的干扰。

    2.3 CSS和JavaScript引入

    [强制] 引入CSS时必须指明rel="stylesheet"

    示例:

    Copy after login

    [建议] 引入CSSJavaScript时无须指明type属性。

    解释:

    text/csstext/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 图片

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

    解释:

    src 取值为空,会导致部分浏览器重新加载一次当前页面,参考:https://developer.yahoo.com/performance/rules.html#emptysrc

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

    解释:

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

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

    解释:

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

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

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

    解释:

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

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

    5 表单

    5.1 控件标题

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

    解释:

    有两种方式:

    1. 将控件置于 label 内。

    2. 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 Learn more about coding conventions in HTML. 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!