结束斜杠前的空格?

我经常在 XML 和 HTML 标记中看到结束斜杠前面有一个空格。XHTML 换行符可能就是典型的例子:

<br />

而不是:

<br/>

这个空间似乎是多余的。事实上,我认为它是多余的。

写这个空间的原因是什么?

我读到这个空间解决了一些“向后兼容性问题”哪些向后兼容性问题?这些问题是否仍然相关,或者我们仍然为了 IE3兼容性而增加额外的空间?这个问题有没有一些明确的答案?

如果没有向后兼容性,那么这是一个可读性问题吗? 类似于大开卷曲支架的争论?

void it_goes_up_here() {


int no_you_fool_it_goes_down_there()
{

我当然可以尊重不同的风格意见,所以我会很高兴地了解到,写的空间只是一个品味的问题。

34549 次浏览

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.

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.

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.

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.

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.

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.

Are those issues still relevant or are we still adding extra spaces for the sake of, say, IE3 compatibility?

You were close - it is for Netscape 4.

It is interesting to see other rationalisations, but that's all it was meant for.

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

<img src=http://myunquotedurl.com/image.jpg />

versus

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

Netscape 4.80 showing different behaviour of <br/> and <br /> in HTML

Supporting bobince's answer with screenshot of Netscape 4.80 showing documents

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:

(Unable to reproduce there now, but supports Lee Kowalkowski's statement about multiple browsers affected by this.)