Html 在 HTML5 中我们还需要结束斜杠吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7366344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do we still need end slashes in HTML5?
提问by CaptSaltyHyman
In HTML5, do we still need the end slash like in XHTML?
在 HTML5 中,我们还需要像 XHTML 中那样的结尾斜杠吗?
<img src="some_image.png" />
validator.w3.org didn't complain if I dropped it, not even a warning. But some online documents seem to indicate the end slash is still required for tags such as img, link, meta, br, etc.
如果我放弃它,validator.w3.org 没有抱怨,甚至没有警告。但是有些网上的文档似乎表明img、link、meta、br等标签仍然需要结尾斜杠。
采纳答案by Brian R. Bondy
img
tags are Void Elements so they do not need an end tag.
img
标签是空元素,所以它们不需要结束标签。
Void elements area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr
空元素 area、base、br、col、command、embed、hr、img、input、keygen、link、meta、param、source、track、wbr
...
...
Void elements only have a start tag; end tags must not be specified for void elements.
void 元素只有一个开始标签;不得为空元素指定结束标记。
That being said it's not strict parsing in HTML5 so it won't do any major harm.
话虽如此,它在 HTML5 中的解析并不严格,因此不会造成任何重大损害。
回答by Free Radical
In HTML 5, the closing slash is optionalon void elements such img
(I am adding this because the currently accepted answer only says: "end tags must not be specified for void elements", and does not address closing slashes in void elements).
在 HTML 5 中,结束斜线在 void 元素上是可选的img
(我添加这个是因为当前接受的答案只说:“不得为 void 元素指定结束标记”,并且没有解决 void 元素中的结束斜线问题)。
Citing from http://www.w3.org/TR/html5/syntax.html#start-tags(number 6):
引用自http://www.w3.org/TR/html5/syntax.html#start-tags(6号):
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) character. This character has no effect on void elements, but on foreign elements it marks the start tag as self-closing.
然后,如果该元素是空元素之一,或者如果该元素是外来元素,则可能存在单个“/”(U+002F) 字符。这个字符对空元素没有影响,但对外部元素它标记开始标签为自关闭。
回答by Josh Lee
According to Start Tagsthey are optional.
根据开始标签,它们是可选的。
回答by Ry-
Nope. HTML has never required it, even before HTML5. If you plan to use XHTML with HTML features, then yes, it is necessary.
不。HTML 从未要求它,甚至在 HTML5 之前。如果您打算使用具有 HTML 功能的 XHTML,那么是的,这是必要的。
回答by Kai Noack
The end slash for void elements is optional.
void 元素的结束斜杠是可选的。
Both are valid HTML5:
两者都是有效的 HTML5:
<img src="some_image.png" />
and
和
<img src="some_image.png">
Void Elements are:
虚空元素是:
area
base
br
col
embed
hr
img
input
link
meta
param
source
track
wbr
area
base
br
col
embed
hr
img
input
link
meta
param
source
track
wbr
Besides interesting:
除了有趣:
Void elements can't have any contents (since there's no end tag, no content can be put between the start tag and the end tag).
void 元素不能有任何内容(因为没有结束标签,所以开始标签和结束标签之间不能放置任何内容)。
More details at: https://html.spec.whatwg.org/multipage/syntax.html#elements-2
更多详细信息,请访问:https: //html.spec.whatwg.org/multipage/syntax.html#elements-2
See also the SO-question here:
Self-closing tags (void elements) in HTML5
另请参阅此处的 SO 问题:
HTML5 中的自闭合标签(空元素)
回答by Fabio Nolasco
FROM W3C:
来自 W3C:
Void elements: area, base, br, col, embed, hr, img, input, keygen, link, meta, param, source, track, wbr
空元素:area、base、br、col、embed、hr、img、input、keygen、link、meta、param、source、track、wbr
"Void elements only have a start tag; end tags must not be specified for void elements."
“空元素只有一个开始标签;不能为空元素指定结束标签。”