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

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

HTML 第一版

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

1
2
3
4
5
6
< 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标签不要自闭合才是标准的写法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值