why
and not
?

2019-02-03 02:39发布

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.

标签: xhtml tags
13条回答
beautiful°
2楼-- · 2019-02-03 03:20

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 :-)

查看更多
孤傲高冷的网名
3楼-- · 2019-02-03 03:21

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.

查看更多
聊天终结者
4楼-- · 2019-02-03 03:22

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

查看更多
Rolldiameter
5楼-- · 2019-02-03 03:22

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

查看更多
6楼-- · 2019-02-03 03:25

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.

查看更多
冷血范
7楼-- · 2019-02-03 03:29

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

查看更多
登录 后发表回答