Home  >  Article  >  Web Front-end  >  HTML coding standards

HTML coding standards

藏色散人
藏色散人forward
2019-10-29 11:55:443278browse

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:

<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(&#39;foo&#39;).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>

  • first
  • second

[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=&#39;esl.js&#39;></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>
    
        
        ......
    
......

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

<link rel="stylesheet" src="page.css">

[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 放在页面中间将阻断页面的渲染。出于性能方面的考虑,如非必要,请遵守此条建议。

示例:

<body>
    <!-- a lot of elements -->
    <script src="init-behavior.js"></script>
</body>

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

示例:

<script src="//global.zuzuche.com/assets/js/gallery/jquery/1.11.2/jquery.js"></script>

3 Head

3.1 title

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

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

解释:

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

示例:

<head>
    <meta charset="UTF-8">
    <link rel="dns-prefetch" href="//global.zuzuche.com/">
    <link rel="dns-prefetch" href="//imgcdn1.zuzuche.com/">
    <link rel="dns-prefetch" href="//qiniucdn.com/">
    <title>页面标题</title>
</head>

4 图片

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

解释:

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

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

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

解释:

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

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

解释:

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

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

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

解释:

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

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

5 表单

5.1 控件标题

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

解释:

有两种方式:

将控件置于 label 内。

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

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

示例:

<label><input type="checkbox" name="confirm" value="on"> 我已确认上述条款</label>
<label for="username">用户名:</label> <input type="textbox" name="username" id="username">

5.2 按钮

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

解释:

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

示例:

<button type="submit">提交</button>
<button type="button">取消</button>

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

解释:

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

5.3 可访问性 (A11Y)

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

解释:

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

示例:

<!-- good -->
<style>
.buttons .button-group {
    float: right;
}
</style>
<div>
    <div>
        <button type="submit">提交</button>
        <button type="button">取消</button>
    </div>
</div>
<!-- bad -->
<style>
.buttons button {
    float: right;
}
</style>
<div>
    <button type="button">取消</button>
    <button type="submit">提交</button>
</div>

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

解释:

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

示例:

<form action="/login" method="post">
    <p><input name="username" type="text" placeholder="用户名"></p>
    <p><input name="password" type="password" placeholder="密码"></p>
</form>

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

解释:

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

示例:

<input type="date">
<input type="tel">
<input type="number">
<input type="number" pattern="\d*">

6 模板中的 HTML

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

示例:

<!-- good -->
<!-- IF is_display -->
<div>
    <ul>
        <!-- BEGIN item_list -->
        <li>{name}<li>
        <!-- END item_list -->
    </ul>
</div>
<!-- ENDIF item_list -->
<!-- bad -->
<!-- IF is_display -->
    <div>
        <ul>
    <!-- BEGIN item_list -->
        <li>{$item.name}<li>
    <!-- END item_list -->
        </ul>
    </div>
<!-- ENDIF item_list -->

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

示例:

<!-- good -->
<li class="<!-- IF selected --> selected<!-- ENDIF selected -->">{type_name}</li>
<!-- bad -->
<li <!-- IF selected --><!-- ENDIF selected -->>{type_name}</li>

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

示例:

<!-- good -->
<!-- IF is_display -->
<div>
    <!-- BEGIN item_list -->
    <ul>
        <!-- BEGIN package_list -->
        <li>
            <span>{name}:</span><span>¥{unit_price}</span>
        <li>
        <!-- END package_list -->
    </ul>
    <!-- END item_list -->
</div>
<!-- ENDIF is_display -->
<!-- bad -->
<!-- IF is_display -->
<div>
    <!-- BEGIN item_list -->
    <ul>
        <!-- BEGIN package_list -->
        <li>
            <span>{name}:</span><span>¥{unit_price}</span>
        <li>
        <!-- END -->
    </ul>
    <!-- END -->
</div>
<!-- ENDIF -->

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

示例:

<!-- good -->
<table>
    <!-- BEGIN item_list -->
    <tr>
        <!-- BEGIN package_list -->
        <td>
            <span>{name}:</span><span>¥{unit_price}</span>
        </td>
        <!-- END package_list -->
    <tr>
    <!-- END item_list -->
</table>
<!-- bad -->
<table>
<tr>
    <!-- BEGIN item_list -->
    <td>
        <span>{name}:</span><span>¥{unit_price}</span>
    </td>
        <!-- IF id="5" -->
    </tr>
    <tr>
        <!-- ENDIF id -->
    <!-- END item_list -->
</tr>
</table>
<!-- good -->
<table>
    <!-- BEGIN item_list -->
    <tr>
        <!-- BEGIN package_list -->
        <td>
            <span>{name}:</span><span>¥{price}</span>
        </td>
        <!-- END package_list -->
    <tr>
    <!-- END item_list -->
</table>
<!-- bad -->
<table>
<!-- BEGIN item_list -->
<tr>
    <td>
        <span>{name}:</span>
        <!-- IF type="unit" -->
        <span>¥{unit_price}</span>
        <!-- ELSEIF type="total" -->
        <span>¥{total_price}</span>
        <!-- ENDIF type -->
    </td>
</tr>
<!-- END item_list -->
</tr>
</table>

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

Statement:
This article is reproduced at:zuzuche.com. If there is any infringement, please contact admin@php.cn delete
Previous article:HTML coding conventionsNext article:HTML coding conventions