首页 > 是否应该闭合html5文档中的空标签

是否应该闭合html5文档中的空标签

处女贴,问个很浅薄也很困扰的问题吧
发现官方草案及答疑中说明即自关闭的斜线(/)对空标签无效,普通HTML标签不要自闭合才是标准的写法,那么我们是否还应该闭合html5中的空标签?
看了下SF的源码,也并不是所有的单标签都闭合了:)

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


XHTML的设计者认为,应当让HTML页面严格符合XML,使得HTML真正成为XML的一个子集,从而提倡了和XML一致的自闭合标签。

不得不说XHTML在促进程序员规范语义上,起到了巨大的历史作用。但在结果上,实际写出的XHTML页面没有几个真的送进XML解析器不报错的。所以到了现在HTML5时代,对XML的执着终究被废弃了,XHTML当初的设计目标被Web标准的设计者自己挖个坑给埋了。

现在的原则是:只要标签没有二义性,爱咋整咋整。对于标准中所说的void elements,爱闭合不闭合,不必执着怎么做更“标准”——这在当前的思想下,是个不必要的负担。


这个网站有的闭合,有的没闭合,可能不是一个人写的,不然不会产生两种风格。

关于空标签闭合,我偏向Google Style Guide中建议的不闭合,在这方面我有点偷懒,觉得Google的标准就是业界今后的标准。

有点完美主义,以前力求闭合,现在力求不闭合。


html5其中一个新特性就是:强大的容错机制。所以,不要在意细节

【热门文章】
【热门文章】