Html 关闭斜线之前的空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/462741/
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
Space Before Closing Slash?
提问by Greg Mattes
I've frequently seen a space preceding the closing slash in XML and HTML tags. The XHTML line break is probably the canonical example:
我经常看到 XML 和 HTML 标签中的斜线前面有一个空格。XHTML 换行符可能是典型的例子:
<br />
instead of:
代替:
<br/>
The space seems superfluous. In fact, I think that it is superfluous.
空间似乎是多余的。事实上,我认为这是多余的。
What is the reason for writing this space?
写这个空间的原因是什么?
I've read that the space solves some "backwards compatibility issues." Which backwards compatibility issues? Are those issues still relevant, or are we still adding extra spaces for the sake of, say, IE3 compatibility? Does there exist some spec with the definitive answer on this?
我读过该空间解决了一些“向后兼容性问题”。哪些向后兼容问题?这些问题是否仍然相关,或者我们是否仍然为了 IE3 兼容性而添加额外的空格?是否存在一些对此有明确答案的规范?
If not backwards compatibility, then is it a readability issue? Similar to the Great Open Curly Brace debate?
如果不是向后兼容性,那么它是可读性问题吗?类似于 Great Open Curly Brace 辩论?
void it_goes_up_here() {
int no_you_fool_it_goes_down_there()
{
I can certainly respect differing stylistic opinions, so I'll be happy to learn that writing the space is simply a matter of taste.
我当然可以尊重不同的文体意见,所以我会很高兴地了解到写这个空间只是一个品味问题。
采纳答案by Lee Kowalkowski
The answer is people wish to adhere to Appendix C of the XHTML1.0 specification. Which you only need to do if you are serving XHTML as text/html. Which most people do, because XHTML's real MIME type (application/html+xml) does not work in Internet Explorer.
答案是人们希望坚持的XHTML1.0规范的附录C。如果您将XHTML 作为 text/html 提供服务,您只需要这样做。大多数人会这样做,因为 XHTML 的真实 MIME 类型(application/html+xml)在 Internet Explorer 中不起作用。
No current browser cares for the space. Browsers are very tolerant of these things.
当前没有浏览器关心空间。浏览器对这些事情非常宽容。
The space used to be required to ensure HTML parsers treated the trailing slash as an unrecognised attribute.
过去需要空间来确保 HTML 解析器将尾部斜杠视为无法识别的属性。
回答by myf
Supporting bobince's answerwith screenshot of Netscape 4.80showing documents
通过显示文档的Netscape 4.80屏幕截图支持bobince 的回答
data:text/html,<title>space</title>foo<br />bar
(top left, linebreak rendered) and
(左上角,呈现换行符)和
data:text/html,<title>no space</title>foo<br/>bar
(bottom left, linebreak ignored).
(左下角,忽略换行符)。
Posting as answer to show the picture
发布作为答案以显示图片
Tangentially related: in fact I had a lengthy answer identifying the cause of such misbehaviour of ancient browsers (and resulting recommendation to include space) in misunderstood SGML specs, namely SGML Null End Tag (NET)(where 1<tag/2/3
equals 1<tag>2</tag>3
so 1<tag/>2
would actually mean 1<tag>>2
), but not only I was unable to find good proof and concrete version of standard, I wasn't even able to grasp proper standard-complying behaviour. So few raw links for reference:
切向相关:事实上,我有一个冗长的答案,确定了在被误解的 SGML 规范中,即SGML Null End Tag ( NET)(其中1<tag/2/3
等于1<tag>2</tag>3
所以1<tag/>2
实际上意味着1<tag>>2
)中古代浏览器的这种不当行为(以及由此产生的包含空格的建议)的原因,但是我不仅无法找到标准的良好证明和具体版本,我什至无法掌握正确的符合标准的行为。很少有原始链接可供参考:
- w3c validator notice mentioning problematic closing slashand pointing to
- Empty elements in SGML, HTML, XML, and XHTML @ www.cs.tut.fi/~jkorpela/
- Beware of XHTML: Null End Tags (NET)stating, that
However, there are still some smaller user agents that properly support Null End Tags. One of the more well-known user agents that support it is the W3C validator.
- w3c 验证器通知提到有问题的关闭斜线并指向
- SGML、HTML、XML 和 XHTML 中的空元素 @ www.cs.tut.fi/~jkorpela/
- 当心 XHTML: Null End Tags (NET)声明,
但是,仍然有一些较小的用户代理可以正确支持 Null End Tags。支持它的最著名的用户代理之一是 W3C 验证器。
(Unable to reproduce there now, but supports Lee Kowalkowski's statement about multiple browsers affected by this.)
(现在无法在那里重现,但支持 Lee Kowalkowski 关于受此影响的多个浏览器的声明。)
- XML W3C Working Draft 07-Aug-97- latest specs draft that includes reference of Null End Tag in DTD snippet:
NET "/>"
- XML W3C 工作草案 07-Aug-97- 最新的规范草案,包括在 DTD 片段中引用 Null End Tag:
NET "/>"
回答by bobince
Are those issues still relevant or are we still adding extra spaces for the sake of, say, IE3 compatibility?
这些问题是否仍然相关,或者我们是否仍然为了 IE3 兼容性而添加额外的空格?
You were close - it is for Netscape 4.
你很接近 - 它适用于 Netscape 4。
It is interesting to see other rationalisations, but that's all it was meant for.
看到其他合理化很有趣,但这就是它的全部意义。
回答by Andrew Hare
No, the space is not required but it is necessary for some older browsers to render those tags correctly. The proper way to do it is without the extra space as this is something XHTML inherited from XML.
不,空间不是必需的,但某些较旧的浏览器需要正确呈现这些标签。正确的做法是没有多余的空间,因为这是 XHTML 从 XML 继承的东西。
回答by Pesto
In XHTML, br tags must be closed, but the space is not necessary. It's a stylistic thing. In HTML, br tags cannot be closed, so both are wrong.
在 XHTML 中,br 标签必须关闭,但空格不是必需的。这是一个风格的东西。在 HTML 中,br 标签不能关闭,所以两者都是错误的。
回答by Jim Petkus
The space just makes the tags more readable. I am a big proponent of formatting for more readable code. Little things like that go a long way. Without the space the closing tag blends in with the opening tag. It takes just an instant longer for me to process it as I am quickly reading the code.
空间只是使标签更具可读性。我是格式化更具可读性的代码的大力支持者。诸如此类的小事大有裨益。没有空间,结束标签与开始标签融合在一起。因为我正在快速阅读代码,所以我处理它只需要一瞬间的时间。
回答by Jim True
What if there was a very lazy html writer out there or maybe he had a fear of quotation marks. Consider the following if you were his robot page crawler...
如果那里有一个非常懒惰的 html 编写者或者他害怕引号怎么办。如果您是他的机器人页面爬虫,请考虑以下内容...
<img src=http://myunquotedurl.com/image.jpg />
<img src=http://myunquotedurl.com/image.jpg />
versus
相对
<img src=http://myunquotedurl.com/image.jpg/>
<img src=http://myunquotedurl.com/image.jpg/>
This might seem small but look what it can do if the space isn't there. The robot won't know if the slash is part of the url or part of the closing tag.
这可能看起来很小,但看看如果没有空间它可以做什么。机器人不会知道斜杠是 url 的一部分还是结束标记的一部分。
回答by nicruo
I think that the white space is a way to reinforce the idea that this tag is empty and it closes itself.
我认为空白是一种强化这种标签是空的并且它自己关闭的想法的方式。
Today i don't use the white space anymore because i never had a problem with no white space.
今天我不再使用空格了,因为我从来没有遇到没有空格的问题。