why
and not
?

前端 未结 13 962
天涯浪人
天涯浪人 2020-12-29 05:29

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

相关标签:
13条回答
  • 2020-12-29 05:48

    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, when parsed as HTML, </br> is treated as if it was <br>, so <br></br> produces two line breaks.

    0 讨论(0)
  • 2020-12-29 05:51

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

    0 讨论(0)
  • 2020-12-29 05:56

    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.

    0 讨论(0)
  • 2020-12-29 05:59

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

    0 讨论(0)
  • 2020-12-29 06:02

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

    0 讨论(0)
  • 2020-12-29 06:07

    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.

    0 讨论(0)
提交回复
热议问题