藏井阁

" Scientists ask why, engineers ask why not? "

HTML(5) 不要求标签自闭合

XHTML教导我们,标签都是要闭合的,不能包含子元素的标签需要自闭合。但HTML5是没有这个要求的,所有HTML中都没这个要求,是XHTML将这门松散的语言变得严格起来。XHTML诸多严格,包括标签都要闭合,包括tagName和属性都要小写等。

HTML 第一版

追溯HTML的元祖第一版,就是不要求自闭合的,如下的一段用法就表明了其的松散随意

<ADDRESS>
Newsletter editor<p>
J.R. Brown<p>
JimquickPost News, Jumquick, CT 01234<p>
Tel (123) 456 7890
</ADDRESS>

对于<img>这类标签,本来就是没有闭合的,原始定义为:

The IMG element is empty: it has no closing tag.

现行的 HTML5

HTML5中,<br> <img> <input> <meta>等空标签可以不用自闭合了。

官方草案中描述:

Then, if the element is one of the void elements, or if the element is a foreign element, then there may be a single U+002F SOLIDUS character (/). This character has no effect on void elements, but on foreign elements it marks the start tag as self-closing.

即自关闭的斜线(/)对空标签无效。

Void Elements 包含以下标签:

area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr

官方答疑中,对被问及此问题的答复如下:

Void elements in HTML (e.g. the br, img and input elements) do not require a trailing slash. e.g. Instead of writing, you only need to write. This is the same as in HTML4. However, due to the widespread attempts to use XHTML1, there are a significant number of pages using the trailing slash. Because of this, the trailing slash syntax has been permitted on void elements in HTML in order to ease migration from XHTML1 back to HTML.

言即不再需要自闭合(/)这个小尾巴了,如果要写上也是可以的。

除了MathML标签遵循XML语法需要自闭合外,普通HTML标签不要自闭合才是标准的写法。

类别: Web开发

Tags: html5,self-closing,自闭合

damon pang

大魔

Damo

i#impng.com

专注Web开发,爱电影,爱One Piece.