Home  >  Article  >  Web Front-end  >  HTML specifications you must know (organized and shared)

HTML specifications you must know (organized and shared)

WBOY
WBOYforward
2022-01-13 18:06:361872browse

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 specifications you must know (organized and shared)

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

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

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

  • ##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 - citation

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

示例:

<!DOCTYPE html>

[建议] 启用 IE Edge 模式。

示例:

<meta http-equiv="X-UA-Compatible" content="IE=Edge">

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

解释:

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

示例:

<html>

3.2 编码

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

解释:

见 HTML5 Charset能用吗 一文。

示例:

<html>
    
        
        ......
    
    
        ......
    

[建议] HTML 文件使用无 BOM 的 UTF-8 编码。

解释:

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

3.3 CSS和JavaScript引入

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

示例:

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

[建议] 引入 CSS 和 JavaScript 时无须指明 type 属性。

解释:

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

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

解释:

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

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

解释:

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

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

解释:

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

示例:

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

[建议] 移动环境或只针对现代浏览器设计的 Web 应用,如果引用外部资源的 URL 协议部分与页面相同,建议省略协议前缀。

解释:

使用 protocol-relative URL 引入 CSS,在 IE7/8 下,会发两次请求。是否使用 protocol-relative URL 应充分考虑页面针对的环境。

示例:

<script src="//s1.bdstatic.com/cache/static/jquery-1.10.2.min_f2fb5194.js"></script>

4 head

4.1 title

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

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

解释:

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

示例:

<head>
    <meta charset="UTF-8">
    <title>页面标题</title>
</head>

4.2 favicon

[强制] 保证 favicon 可访问。

解释:

在未指定 favicon 时,大多数浏览器会请求 Web Server 根目录下的 favicon.ico 。为了保证favicon可访问,避免404,必须遵循以下两种方法之一:

  • 在 Web Server 根目录放置 favicon.ico 文件。

  • 使用 link 指定 favicon。

示例:

<link rel="shortcut icon" href="path/to/favicon.ico">

4.3 viewport

[建议] 若页面欲对移动设备友好,需指定页面的 viewport。

解释:

viewport meta tag可以设置可视区域的宽度和初始缩放大小,避免在移动设备上出现页面展示不正常。

比如,在页面宽度小于 980px 时,若需 iOS 设备友好,应当设置 viewport 的 width 值来适应你的页面宽度。同时因为不同移动设备分辨率不同,在设置时,应当使用 device-width 和 device-height 变量。

另外,为了使 viewport 正常工作,在页面内容样式布局设计上也要做相应调整,如避免绝对定位等。关于 viewport 的更多介绍,可以参见 Safari Web Content Guide的介绍

5 图片

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

解释:

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

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

解释:

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

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

解释:

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

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

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

解释:

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

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

6 表单

6.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">

6.2 按钮

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

解释:

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

示例:

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

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

解释:

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

6.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">

7 多媒体

[建议] 当在现代浏览器中使用 audio 以及 video 标签来播放音频、视频时,应当注意格式。

解释:

音频应尽可能覆盖到如下格式:

  • MP3

  • WAV

  • Ogg

视频应尽可能覆盖到如下格式:

  • MP4

  • WebM

  • Ogg

[建议] 在支持 HTML5 的浏览器中优先使用 audio 和 video 标签来定义音视频元素。

[建议] 使用退化到插件的方式来对多浏览器进行支持。

示例:

<audio controls>
    <source src="audio.mp3" type="audio/mpeg">
    <source src="audio.ogg" type="audio/ogg">
    <object width="100" height="50" data="audio.mp3">
        <embed width="100" height="50" src="audio.swf">
    </object>
</audio>
<video width="100" height="50" controls>
    <source src="video.mp4" type="video/mp4">
    <source src="video.ogg" type="video/ogg">
    <object width="100" height="50" data="video.mp4">
        <embed width="100" height="50" src="video.swf">
    </object>
</video>

[建议] 只在必要的时候开启音视频的自动播放。

[建议] 在 object 标签内部提供指示浏览器不支持该标签的说明。

示例:

<object width="100" height="50" data="something.swf">DO NOT SUPPORT THIS TAG</object>

8 模板中的 HTML

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

示例:

<!-- good -->
{if $display == true}
<div>
    <ul>
        {foreach $item_list as $item}
        <li>{$item.name}<li>
        {/foreach}
    </ul>
</div>
{/if}
<!-- bad -->
{if $display == true}
    <div>
        <ul>
    {foreach $item_list as $item}
        <li>{$item.name}<li>
    {/foreach}
        </ul>
    </div>
{/if}

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

示例:

<!-- good -->
<li class="{if $item.type_id == $current_type}focus{/if}">{ $item.type_name }</li>
<!-- bad -->
<li {if $item.type_id == $current_type}{/if}>{ $item.type_name }</li>

[建议] 在循环处理模板数据构造表格时,若要求每行输出固定的个数,建议先将数据分组,之后再循环输出。

示例:

<!-- good -->
<table>
    {foreach $item_list as $item_group}
    <tr>
        {foreach $item_group as $item}
        <td>{ $item.name }</td>
        {/foreach}
    <tr>
    {/foreach}
</table>
<!-- bad -->
<table>
<tr>
    {foreach $item_list as $item}
    <td>{ $item.name }</td>
        {if $item@iteration is div by 5}
    </tr>
    <tr>
        {/if}
    {/foreach}
</tr>
</table>

推荐教程:《html视频教程

The above is the detailed content of HTML specifications you must know (organized and shared). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete