--- group: Coding Style order: 1 --- # HTML Coding Specification ## Boilerplate ```html My Site - There are a lot of fun!
``` ## DOCTYPE ### Must have doctype at the beginning Level: **Mandatory** ```html ``` ### Must use HTML5 lowercase doctype Level: **Mandatory** ```html ``` ## `` element ### Must have and only have one `` element at root Level: **Mandatory** ```html
...
...
... ... ``` ### `` element must have `lang` attribute Level: **Mandatory** `lang` attribute is important for search engines, screen readers and translation tools. The value is usually in `language-REGION` format, e.g. `en-US`, `zh-CN`. ```html ``` If a part of the document is written in another language, add `lang` attribute to the root element. ```html ...
...
...
``` ### `` must have and only have one `` and one `` as children Level: **Mandatory** ```html ... ... ... ... ... ``` ## `` elements ### `` element must be contained in `` element Level: **Mandatory** ```html ... ... ``` ### Must use UTF-8 charset Level: **Mandatory** ```html ``` ### Responsive web page should have viewport meta Level: **Recommended** The following settings match the actual device size. Users can scale the content with touch gestures. ```html ``` The following settings disable downscale, which is not useful and even troublesome. ```html ``` In case you have implemented scaleable UI in the web page, you may want to totally disable browser scale support. ```html ``` To fit iPhone's notch screen, you may need some extra configuration. [Learn more](https://webkit.org/blog/7929/designing-websites-for-iphone-x/) . ### 1.4 资源加载 - 1.4.1 `recommended` 引入 CSS 和 JavaScript 时无需指定 type。 根据 HTML5 规范,引入 CSS 和 JavaScript 时通常不需要指明 type,因为 [text/css](https://html.spec.whatwg.org/multipage/obsolete.html#attr-style-type) 和 [text/javascript](https://html.spec.whatwg.org/multipage/scripting.html#attr-script-type) 分别是他们的默认值。 ```html ``` - 1.4.2 `recommended` 在 head 标签内引入 CSS,在 body 结束标签前引入 JS。 在 `` 中指定外部样式表和嵌入式样式块可能会导致页面的重排和重绘,对页面的渲染造成影响。因此,一般情况下,CSS 应在 `` 标签里引入,[了解更多](https://developer.yahoo.com/performance/rules.html#css_top)。 > 在 HTTP2(Chrome 浏览器 69 版本之后,Firefox 和 Edge)中可以在 body 中使用 link 标签引入样式文件,但不推荐在 body 中使用 ` ... ``` - 1.4.3 `recommended` 外部资源的引用地址跟随页面协议,省略协议部分。 ```html ``` - 1.4.4 `recommended` 使用 preload 预加载关键资源,[了解更多](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Preloading_content)。 ```html ``` - 1.4.5 `recommended` 使用 dns-prefetch 和 preconnect 处理 DNS 解析延迟问题,提高网页加载性能,[了解更多](https://developer.mozilla.org/zh-CN/docs/Web/Performance/dns-prefetch)。 ```html ``` ### 1.5 页面标题 - 1.5.1 `mandatory` 页面需要指定 title 标签,有且仅有 1 个。 ```html 淘宝网 - 淘!我喜欢 ``` ## 2 编码 ### 2.1 缩进 - 2.1.1 `recommended` 统一使用 2 个空格缩进,不要使用 4 个空格或 tab 缩进。 ```html Page title Company

Hello, world!

``` ### 2.2 注释 - 2.2.1 `mandatory` 在 HTML 注释代码中,不允许出现任何敏感信息。 常见的敏感信息包括: - 业务相关敏感信息,例如业务规则 - 员工个人隐私信息,例如邮箱、手机、身份证号码 - AK(accessKey id, accesskey secret) - 证书、密码 - 内网 IP、URL - 其他公司、员工相关的内部信息、敏感信息 - 2.2.2 `recommended` 单行注释,需在注释内容和注释符之间需留有一个空格,以增强可读性。 ```html ``` - 2.2.3 `recommended` 多行注释,注释符单独占一行,注释内容 2 个空格缩进。 ```html ``` ### 2.3 标签 - 2.3.1 `mandatory` 标签名统一使用小写。 ```html

Hello, world!

Hello, world!

``` - 2.3.2 `recommended` 不要省略自闭合标签结尾处的斜线,且斜线前需留有一个空格。 虽然 [HTML5 规范](https://dev.w3.org/html5/spec-author-view/syntax.html#syntax-start-tag) 中指出结尾的斜线是可选的,但保留它们可以明确表达该标签已闭合的语义,更易于维护和理解。 同时,在 React 被广泛使用的今天,这与 [JSX 的规范](https://react-cn.github.io/react/tips/self-closing-tag.html) 相一致,JSX 中自闭合标签必须保留结尾的斜线。 ```html foo foo ``` ### 2.4 属性 - 2.4.1 `mandatory` 属性值使用双引号,不要使用单引号。 ```html ``` - 2.4.2 `recommended` 不要为 Boolean 属性添加取值。 XHTML 需要每个属性声明取值,但是 HTML5 并不需要。一个元素中 Boolean 属性存在即表示取值 `true`,不存在则表示取值 `false`,[了解更多](http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#boolean-attributes)。 ```html ``` - 2.4.3 `recommended` 自定义属性的命名:以 data- 为前缀。 建议自定义属性的命名都以 `data-` 为前缀,以便区分。 ```html Example link Example link ``` ### 2.5 语义化 - 2.5.1 `referenced` 尽量根据语义使用 HTML 标签。 HTML 标签(更严谨的叫法是 HTML 元素)都有其语义,例如 `p` 标签即“paragraphs”用于章节,`a` 标签即“anchors”用于锚点链接,[了解更多](https://www.w3.org/TR/2018/WD-html53-20181018/fullindex.html#index-elements)。 我们应优先选取符合当下所需语义的标签,这既有助于[可访问性(Accessibility)](https://developer.mozilla.org/zh-CN/docs/learn/Accessibility),也可以在 CSS 加载失败时获得较好的展示效果。 ```html
1
2
3
``` ### 2.6 可访问性 - 2.6.1 `referenced` 注意 HTML 的可访问性(Accessibility)。 网页可访问性使网页内容落实“无障碍”,让不同程度或需求的用户可以顺畅的获取网站上的信息。传统上我们认为这只与残疾人士有关,但提升网站的可访问性也可以让其他用户群体受益,比如使用移动设备的人群或低速网络的人群。 例如,为 img 标签设置 alt 属性: ```html Welcome to visit! ``` 了解更多 HTML 可访问性的知识,可以阅读[这篇 MDN 的文章](https://developer.mozilla.org/zh-CN/docs/learn/Accessibility)。 ## Template languages There are many template languages to generate HTML files, e.g. [Nunjucks], [EJS]. Writting template language syntax should also follow some common specification. This part uses [Nunjucks] as an example. But it should fit other template languages, too. ### Variables, filters and keywords MUST have spaces around Level: **Mandatory** ```jinja {# ❌ bad, no spaces #} {{username}} {{tags|join(',')}} {# ✅ good #} {{ username }} {{ tags | join(',') }} ``` ### Unfiltered user input MUST be HTML escaped Level: **Mandatory** ```jinja {# ❌ bad #}

{{ description }}

{# ✅ good #} {{ description | escaped }} ``` ## Credits > If you have contributed to this document but your name isn't listed, please [create an issue](https://github.com/alibaba/f2e-spec/issues/new) to let us know! - TODO ## References - [Code Guide by @mdo](http://codeguide.co) - [Google HTML/CSS Style Guide](https://google.github.io/styleguide/htmlcssguide.html) [EJS]: https://ejs.co/ [Nunjucks]: https://mozilla.github.io/nunjucks/