why <br /> and not <br/>?

我只是一个虾纸丫 提交于 2019-12-18 11:50:56

问题


This is one of those things that you read once, say "aha!" and then forget. Exactly my case.

Why is the line-break tag in xhtml preferentially written with a space <br /> and not in the also ok format <br/> ? I remember the reason was interesting, and as you can imagine it's not easy to find with google.

For sure it's not an issue of xml well-formedness. From W3C

[44]    EmptyElemTag       ::=      '<' Name (S Attribute)* S? '/>' 

   Empty-element tags may be used for any element which has no content, whether
   or not it is declared using the keyword EMPTY. For interoperability, the 
   empty-element tag should be used, and should only be used, for elements which 
   are declared EMPTY.

Examples of empty elements:

<IMG align="left"  src="http://www.w3.org/Icons/WWW/w3c_home" /> 
<br></br> 
<br/>

So the space at the end is optional.


回答1:


If I recall correctly it's simply because some older browsers had problems with a self-closing tag without a space before the slash. I doubt it's an issue nowadays, but a lot of developers (myself included) got into the habit of including the space.

Edit: Ah, here we are:

http://www.w3.org/TR/xhtml1/#guidelines

Include a space before the trailing / and > of empty elements, e.g. <br />, <hr /> and <img src="karen.jpg" alt="Karen" />. Also, use the minimized tag syntax for empty elements, e.g. <br />, as the alternative syntax <br></br> allowed by XML gives uncertain results in many existing user agents.




回答2:


w3c specifies this as the grammar:

EmptyElemTag       ::=      '<' Name (S Attribute)* S? '/>'

That means open bracket, a name, a number of (space and attribute) tokens, an optional space, a slash, and an end tag. According to this, both are correct.




回答3:


Some older browsers didn't parse the element correctly without the space, so most web developers use <br />. I don't remember which browsers offhand, but I believe they're just about extinct.

EDIT: The browser was Netscape 4.




回答4:


There is no right way in XHTML. They are formally identical in XML. Whitespace is not significant in that location.




回答5:


For XHTML: both of them. For HTML4 and earlier: neither.




回答6:


A little background to add to Matt Hamilton's answer.

A least one problem browser was Netscape 4. A quick check shows that in that browser, <br/> (i.e. no space) doesn't cause a line break. In fact, it doesn't appear to do anything. <br /> (i.e. with space) does perform a line break.

When creating polyglot documents that can behave as XHTML or HTML (Note: "behave as" - not "valid") it's necessary to use either <br /> or <br></br>. However, in old browsers, and even in modern browsers when rendering a page in quirks mode, </br> behaves like <br>, so <br></br> produces two line breaks.




回答7:


<br /> is valid (old) HTML, while <br/> is not. If you are serving your XHTML as XML, it doesn't matter. If you are serving it as text/html, then it needs to be valid HTML in addition to being valid XHTML. (Why serve XHTML as HTML? Because IE doesn't understand XHTML as XML, and because no major browser will start rendering XHTML mid-way through downloading the text, but they will do that to HTML. My blog appears to load slowly not because the site is slow, but because the browser won't start rendering the page until everything has been fetched. I hate browsers.)




回答8:


Both <br/> and <br /> are correct. The reason that <br /> came about in the first place was to support older browsers that didn't understand the new <br/> syntax. It's really kind of a hack where the / is interpreted as an attribute with no value and ignored.




回答9:


Both are correct, and both will be accepted by web browsers. You may as well save yourself the extra character, and use <br/>




回答10:


Both are correct. But I would use <br /> just to keep my code consistent... because I would never write

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>

instead of

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

just to save a byte... and the second version is imho better readable. But that's just a matter of taste. Do it as you like, but do it consistent :-)




回答11:


Either will work just fine. Assuming you are asking for evangelical reasons, I prefer <br/>




回答12:


Both are correct.




回答13:


<br>. You aren't using XML anyway.



来源:https://stackoverflow.com/questions/1659208/why-br-and-not-br

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!