3

When I started learning HTML from w3schools, they were using this <br />. As I started learning ASP.NET, on their forum (ASP.NET Forums) I was told to use <br>. So I started using the second one.

My Question: What is the difference between <br> and <br />? Is there something like browser support?

cdhowie
  • 158,093
  • 24
  • 286
  • 300
Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
  • You can use whatever you are fine with, `
    ` the xhtml thing where you need to self close the tags, it's strict
    – Mr. Alien Aug 22 '13 at 14:45
  • See this link: http://www.sitepoint.com/forums/showthread.php?435290-difference-between-lt-br-gt-and-lt-br-gt – user2520969 Aug 22 '13 at 14:45
  • The link by @user2520969 is more preferable, as it has almost the answer I was hoping for. The link that was provided as a duplicate might look alike, but I am not sure it is a duplicate for that. However, might be a duplicate. And the answer of cdhowie helped me out by providing that example of XML. – Afzaal Ahmad Zeeshan Aug 22 '13 at 14:53

3 Answers3

5

A lone <br> is invalid in XHTML, since XML documents must close each tag they open. <br /> is semantically the same as <br></br> in XML documents, and is referred to as a self-closing tag, so <br /> is used when writing XHTML, or HTML documents that will be read by an XML parser.

This applies to all other tags that do not have a closing tag in HTML, such as <hr /> and <meta />.

Both are valid HTML, so there is no reason not to use <br />, unless you are writing for a broken HTML parser.


Note that in XML, <br/> is valid. However, older HTML parsers that don't know about self-closing tags have been known to choke on this. If a space is inserted before the tag name and the self-closing tag token (/) then these parsers see / as an attribute, or as noise that is discarded. Therefore, one should always make sure to put a space between the element name and the self-closing tag token for compatibility with these broken parsers.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • So that means, `
    ` usage is better? But what if I am not using XHTML, and neither I am using XML, is the usage of `
    ` legal?
    – Afzaal Ahmad Zeeshan Aug 22 '13 at 14:47
  • @AfzaalAhmadZeeshan A proper HTML parser will consider both `
    ` and `
    ` as legal, yes.
    – cdhowie Aug 22 '13 at 14:47
  • @ironicaldiction There is no difference in HTML. `
    ` is preferred not because of some notion of style, but because it allows the document to be read using an XML parser.
    – cdhowie Aug 22 '13 at 14:48
  • So that means, `
    ` is not allowed in XML files, as they require a closing tag too. Nice anwer.
    – Afzaal Ahmad Zeeshan Aug 22 '13 at 14:49
  • @AfzaalAhmadZeeshan Yes. You could write `
    ` but this would confuse older HTML parsers. The `
    ` syntax (including the space) is XML-compatible while also working well with older HTML parsers that don't understand self-closing tags.
    – cdhowie Aug 22 '13 at 14:53
  • @ironicaldiction, I just visited the page you are mentioning, and after that! I was forced to come to this site to ask what's the right way to use this tag. Nothing else. As in start they post the `
    ` then they use `
    `.
    – Afzaal Ahmad Zeeshan Aug 22 '13 at 14:55
  • However, I never meant to give a closing tag for `
    `. Its just a line break. I was saying the XML needs to be closed, sorry you mis understood!
    – Afzaal Ahmad Zeeshan Aug 22 '13 at 14:56
  • Afzaal, My earlier comment was irrelevant. Didn't mean to confuse you. – ironicaldiction Aug 22 '13 at 14:59
  • @cdhowie, please correct all your claims of
    being fine and only confusing old parsers. It is simply not allowed to have closing tags for a selvclosing tag.
    and
    are the only valid ones.
    – Brunis Jul 26 '14 at 10:53
  • @Brunis No, that's [not true in XHTML 1.0](http://www.w3.org/TR/xhtml1/#C_2), though it can (and probably will) confuse user agents that can parse HTML, but not XHTML. If an XHTML parser does not understand `
    ` then it is a broken XHTML parser.
    – cdhowie Jul 28 '14 at 00:33
  • I read the grey box with the duplicate answer and saw HTML5, i realize this question was not standards specific. You are right. – Brunis Jul 28 '14 at 09:01
1

<br> is the valid way to do this in HTML, and <br/> is the way to do it in XHTML.

Arun C
  • 9,035
  • 2
  • 28
  • 42
1

This is a duplicate, but anyways, there is no difference other than preference, at least in HTML. In XML, <br /> is more correct.

Community
  • 1
  • 1
Mr. Shtuffs
  • 127
  • 2
  • 11