Home > Web Front-end > HTML Tutorial > Learn more about coding conventions in HTML

Learn more about coding conventions in HTML

青灯夜游
Release: 2020-10-22 19:40:17
forward
2764 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] Use 4 spaces are used as an indentation level, and 2 spaces or tab characters are not allowed.

Example:

<ul>
    <li>first</li>
    <li>second</li>
</ul>
Copy after login

[Recommendation] Each line should not exceed 120 characters.

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

[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 class="sidebar"></div>

<!-- bad -->
<div class="left"></div>
Copy after login

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

[Recommendation] id, 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 class="comment"></p>
<!-- bad -->
<p class="com"></p>

<!-- good -->
<span class="author"></span>
<!-- bad -->
<span class="red"></span>
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:

<input name="foo">
<div id="foo"></div>
<script>
// IE6 将显示 INPUT
alert(document.getElementById(&#39;foo&#39;).tagName);
</script>
Copy after login

1.3 Tag

[Mandatory] Tag names must use lowercase letters.

Example:

<!-- good -->
<p>Hello StyleGuide!</p>

<!-- bad -->
<P>Hello StyleGuide!</P>
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:

<!-- good -->
<input type="text" name="title">

<!-- bad -->
<input type="text" name="title" />
Copy after login

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

Example:


<ul>
    <li>first</li>
    <li>second</li>
</ul>


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

<!-- 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>
Copy after login

[Suggestion] HTML The 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:
<!-- 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 class="strong">building block</span> for some JavaScript language tools.
</div>
Copy after login

[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 class="avatar" src="image.png">

<!-- bad -->
<span class="avatar">
    <img src="image.png">
</span>
Copy after login

1.4 Attributes

[Mandatory] Attribute names must use lowercase letters .

Example:

<!-- good -->
<table cellspacing="0">...</table>

<!-- bad -->
<table cellSpacing="0">...</table>
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:

<!-- good -->
<script src="esl.js"></script>

<!-- bad -->
<script src=&#39;esl.js&#39;></script>
<script src=esl.js></script>
Copy after login

[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">
Copy after login

[Recommendation] Custom attributes are recommended to 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>
Copy after login

2 Generic

2.1 DOCTYPE

[Mandatory] Use

HTML5's doctype to enable standards mode, it is recommended to use uppercase DOCTYPE. Example:

<!DOCTYPE html>
Copy after login

[Recommended] Enable IE Edge and Chrome Frame mode.

Example:

<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
Copy after login

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

解释:

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

示例:

<html lang="zh-CN">
Copy after login

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

解释:

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

示例:

<meta name="renderer" content="webkit">
Copy after login

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

解释:

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

示例:

<link rel="dns-prefetch" href="//global.zuzuche.com/">
<link rel="dns-prefetch" href="//imgcdn1.zuzuche.com/">
<link rel="dns-prefetch" href="//qiniucdn.com/">
Copy after login

2.2 编码

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

解释:

HTML5 Charset能用吗 一文。

示例:

<html lang="zh-CN">
    
        
        ......
    
    
        ......
    
Copy after login

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

解释:

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

2.3 CSS和JavaScript引入

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

示例:

<link rel="stylesheet" src="page.css">
Copy after login

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

解释:

text/csstext/javascript 是 type 的默认值。

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

解释:

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

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

解释:

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

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

解释:

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

示例:

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

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

示例:

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

3 Head

3.1 title

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

[强制] title 必须作为 head 的直接子元素,并紧随 <link rel="dns-prefetch"> 声明之后。

解释:

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>
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 结构不允许直接嵌套,则应使用第二种。

示例:

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

5.2 按钮

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

解释:

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

示例:

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

[建议] 尽量不要使用按钮类元素的 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>
Copy after login

[建议] 当使用 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>
Copy after login

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

解释:

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

示例:

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

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 -->
Copy after login

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

示例:

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

<!-- bad -->
<li <!-- IF selected --><!-- ENDIF selected -->>{type_name}</li>
Copy after login

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

示例:

<!-- 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 -->
Copy after login

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

示例:

<!-- 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>
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template