HTML 5: Is it <br>, <br/>, or <br />?
Asked Answered
C

17

2201

I've tried checking other answers, but I'm still confused — especially after seeing W3schools HTML 5 reference.

I thought HTML 4.01 was supposed to "allow" single-tags to just be <img> and <br>. Then XHTML came along with <img /> and <br /> (where someone said that the space is there for older browsers).

Now I'm wondering how I'm supposed to format my code when practicing HTML 5.

Is it <br>, <br/> or <br />?

Colbycolbye answered 22/12, 2009 at 13:39 Comment(0)
I
1843

Simply <br> is sufficient.

The other forms are there for compatibility with XHTML; to make it possible to write the same code as XHTML, and have it also work as HTML. Some systems that generate HTML may be based on XML generators, and thus do not have the ability to output just a bare <br> tag; if you're using such a system, it's fine to use <br/>, it's just not necessary if you don't need to do it.

XHTML is not commonly used. In such an event, the content would need to be served as application/xhtml+xml for it to be interpreted as XHTML, and that will not work in old versions of IE - it will also mean that any small validation error made would prevent the page from being displayed. So, most of what looks like XHTML on the web is actually being served, and interpreted, as HTML. See Serving XHTML as text/html Considered Harmful for some more information.

Ingratitude answered 22/12, 2009 at 13:39 Comment(2)
After seeing this self-closing tag article and checking code in console myself I came to know that in HTML5 even if you write <br/> or <br /> they will eventually be converted to <br> by the browser.Ganny
However, in HTML this phenomenon is allowed, but in SVG strictly;Anesthesia
S
325

I think this quote from the HTML5 Standard provides the answer:

3.2.2.2 Void Elements

The term void elements is used to designate elements that must be empty. These requirements only apply to the HTML syntax. In XHTML, all such elements are treated as normal elements, but must be marked up as empty elements.

These elements are forbidden from containing any content at all. In HTML, these elements have a start tag only. The self-closing tag syntax may be used. The end tag must be omitted because the element is automatically closed by the parser.

HTML Example:
A void element in the HTML syntax. This is not permitted in the XHTML syntax.

<hr>

Example:
A void element using the HTML- and XHTML-compatible self-closing tag syntax.

<hr/>

XHTML Example:
A void element using the XHTML-only syntax with an explicit end tag. This is not permitted for void elements in the HTML syntax.

<hr></hr>

In other words:

  • Invalid HTML5: <IMG></IMG>
  • Valid HTML5: <IMG>, <IMG/>

And while HTML forbids certain closing tags, xhtml requires them:

  • Invalid XHTML: <img>
  • Valid XHTML: <img></img> or <img/>

Other elements that are forbidden from having a closing tag in HTML:

Element Valid HTML Valid XHTML
AREA <AREA> <AREA></AREA>
BASE <BASE> <BASE></BASE>
BASEFONT <BASEFONT> <BASEFONT></BASEFONT>
BR <BR> <BR></BR>
COL <COL> <COL></COL>
FRAME <FRAME> <FRAME></FRAME>
HR <HR> <HR></HR>
IMG <IMG> <IMG></IMG>
INPUT <INPUT> <INPUT></INPUT>
ISINDEX <ISINDEX> <ISINDEX></ISINDEX>
LINK <LINK> <LINK></LINK>
META <META> <META></META>
PARAM <PARAM> <PARAM></PARAM>

The fact that HTML forbids certain closing tags, while XHTML requires them is XHTML's problem. If you're writing HTML, you follow the HTML rules.

At the same time, browers gave up trying to enforce the standards, because everyone gets it wrong. It's not obvious:

  • that </BR> is forbidden
  • that </P> is optional
  • and </SPAN> is required

And then xhtml came along, with its XML rule that every element must have a closing tag, and people just assumed that HTML was the same thing. So the standards gave up, and were later revised to throw up their hands to the reality.

Scopp answered 22/12, 2009 at 13:39 Comment(1)
Seems quite specific to me. <br> and <br /> are both legal HTML5.Hyman
F
160

XML doesn't allow leaving tags open, so it makes <br> a bit worse than the other two. The other two are roughly equivalent with the second (<br/>) preferred for compatibility with older browsers. Actually, space before / is preferred for compatibility sake, but I think it only makes sense for tags that have attributes. So I'd say either <br/> or <br />, whichever pleases your aesthetics.

To sum it up: all three are valid with the first one (<br>) being a bit less "portable".

Edit: Now that we're all crazy about specs, I think it worth pointing out that according to dev.w3.org:

Start tags consist of the following parts, in exactly the following order:

  1. A "<" character.
  2. The element’s tag name.
  3. Optionally, one or more attributes, each of which must be preceded by one or more space characters.
  4. Optionally, one or more space characters.
  5. Optionally, a "/" character, which may be present only if the element is a void element.
  6. A ">" character.
Fag answered 22/12, 2009 at 13:39 Comment(0)
B
134

In HTML (up to HTML 4): use <br>

In HTML 5: <br> is preferred, but <br/> and <br /> is also acceptable

In XHTML: <br /> is preferred. Can also use <br/> or <br></br>

Notes:

  • <br></br> is not valid in HTML 5, it will be thought of as two line breaks.
  • XHTML is case sensitive, HTML is not case sensitive.
  • For backward compatibility, some old browsers would parse XHTML as HTML and fail on <br/> but not <br />

Reference:

Bookkeeper answered 22/12, 2009 at 13:39 Comment(9)
checked the link, the preferred approach is <br>, but where does it say <br/> it is not acceptable?Bookkeeper
To clarify, for XML-compliant syntax, <br/> and <br /> (with a SPACE) are equal, with no preference for either. See the XML 1.0 spec. Whitespace (SPACE, tab, or LINE FEED) before the /> is optional, with no preference.Penneypenni
Can you cite a reference for the non-XML syntax being preferred in HTML5? That is news to me. Optional support for strict XML conformance was a founding goal of HTML5 as I recall. Perhaps I missed something on the HTML vs. XHTML document at whatwg.org, or the W3C Polyglot Markup: A robust profile of the HTML5 vocabulary at W3C.Penneypenni
@BasilBourque To re-iterate what I said elsewhere to you. Read the actual specification in HTML5 for the tags and elements and you will never, EVER find any recommendation or suggestion to require or suggest to use a closing slash for those tags. If you want to use XML or XHTML, then you aren't using HTML and that's a different story. Do NOT use a closing slash for those HTML tags. Elsewhere, the spec says you can put one there but it means nothing, does nothing and browsers are instructed to ignore it. So it is pointless and useless making any use of it just as pointless and useless.Diet
@Diet So Section 8.1.2.1. Start tags of Section 8. The HTML syntax of the document, HTML 5.2 W3C Recommendation, 14 December 2017, published by the W3C is not the actual specification of HTML? Please advise.Penneypenni
@BasilBourque Like most people, instead of looking at the exact specification for those tags as I instructed you to do, you instead you scrambled in vein to pull up the exceptions to make them the rule and ignored what I said earlier and also ignored the part in your link that the slash has no effect. Over 15 years I've pointed this out to people, even had W3C and WHATWG editors and authors like Martin Fowler and Ian Hixie back me up, but this is always an uphill battle for no known reason.Diet
@Rob, what do you mean by "looking at the exact specification for those tags"? As you correctly confirm, there is NO recommendation of either format for start tags in the HTML specs. As many others here, including WHATWG's HTML vs. XHTML linked above, I would rather recommend using <br /> for compatibility with XHTML / XML (and related tools). And that should be the end story, IMHO.Gizela
@PetrBodnár It's not a recommendation or a format. It's a standard and a syntax. You either follow it or be wrong. But the OP wants to use HTML, not XML. If one wants to be compatible with XML at the same time, you are opening a whole other can of worms. The server must specify it is serving XML and not HTML. One also needs to be sure to follow all the rules of XML in their HTML which many do not. But why would one want to be compatible with XML when they aren't serving XML? This makes no sense at all. It's a different technology.Diet
@Rob, IMO you are unnecessarily making this topic a rocket science. Yes, the OP asked just about HTML, but it is perfectly valid to take a broader context of related technologies like XML (or SVG) into account. Why? Just shortly: easier switching among the technologies, broader range of potential tools available. If this neither Basil's remarks aren't enough for you, then I really don't know what else would be.Gizela
M
62

According to the spec the expected form is <br> for HTML 5 but a closing slash is permitted.

Missal answered 22/12, 2009 at 13:39 Comment(0)
P
49

I would recommend using <br /> for the following reasons:

1) Text and XML editors that highlight XML syntax in different colours will highlight properly with <br /> but this is not always the case if you use <br>

2) <br /> is backwards-compatible with XHTML and well-formed HTML (ie: XHTML) is often easier to validate for errors and debug

3) Some old parsers and some coding specs require the space before the closing slash (ie: <br /> instead of <br/>) such as the WordPress Plugin Coding spec: http://make.wordpress.org/core/handbook/coding-standards/html/

I my experience, I have never come across a case where using <br /> is problematic, however, there are many cases where <br/> or especially <br> might be problematic in older browsers and tools.

Parbuckle answered 22/12, 2009 at 13:39 Comment(7)
Well formed HTML is not XHTML.Rook
XHTML is well-formed HTML by definition. XHTML follows the rules of XML, according to w3schools "XML is a markup language where documents must be marked up correctly (be "well-formed")... ...By combining the strengths of HTML and XML, XHTML was developed. XHTML is HTML redesigned as XML." (see w3schools.com/html/html_xhtml.asp)Parbuckle
HTML can be well formed, but not be valid XML. W3Schools isn't always the most authoritative reference.Rook
The professor of the university computer science course on web programming I took in 1997 also made the claim that xhtml is well-formed html. I am not sure where you are getting your information from, can you cite any official sources online to backup your claim?Parbuckle
<br> and <hr> are perfectly valid and well formed HTML. They are not valid XML tags. The HTML specs under HTML syntax says that void elements (like <br> or <hr>) may have a / character immediately preceding the final >. But it has no effect. It is ignored if it is there. If the / was a preferred piece of the syntax, the standard would say should rather than may.Rook
@jmarkmurphy, I think that maybe you are unfamiliar with the term "well-formed" being a technical jargon term to refer to the requirement to the standards of XML and XHTML that all tags must have closing tags and must be nested in the proper order. <hr> and <br> do not meet the requirements of XML and XHTML because they do not have closing tags, eg: <br /> or <br></br> are valid, <br> is not valid XHTML or XML. HTML, of course, does not have the well-formed requirement so <br> and <hr> are valid in HTML only.Parbuckle
"HTML5 can be well-formed" is an oxymoron, especially if well-formed does not follow any aspect of XML well-formed requirements. That's the general problem with HTML5 and it's "living" aspects. It isn't following SGML or any real markup specification but itself, which is decided by a hodge-podge of vendor-based organizations. That's why HTML5 has no standard, no well-formed nature, and ironically still allows one to fit older XML specs under and over it. By layering good XML practices into your HTML5 coding strategy, you allow it to still carry "aspects" of XML, which is good practice.Bareilly
T
25

XML requires all tags to have a corresponding closing tag. So there is a special short-hand syntax for tags without inner contents.

HTML5 is not XML, so it should not pose such a requirement. Neither is HTML 4.01.

For instance, in HTML5 specs, all examples with br tag use <br> syntax, not <br/>.

UPD Actually, <br/> is permitted in HTML5. 9.1.2.1, 7.

Teal answered 22/12, 2009 at 13:39 Comment(1)
"HTML5 is not XML, so it should not pose such a requirement." Whether that is correct or not depends on the interpretation of the word "HTML5". If we speak of HTML5 as language, then that statement is correct. If however we speak of HTML5 as a specification, then that statement is incorrect. The HTML5 specification defines "a vocabulary and associated APIs for HTML and XHTML". I know that's a bit nitpicking, I'm not saying this answer is wrong, just giving additional information for the reader.Marquettamarquette
P
18

If you're interested in comparability (not compatibility, but comparability) then I'd stick with <br />.

Otherwise, <br> is fine.

Pupil answered 22/12, 2009 at 13:39 Comment(0)
A
17
  1. If you are outputting HTML on a regular website you can use <br> or <br/>, both are valid anytime you are serving HTML5 as text/html.

  2. If you are serving HTML5 as XHTML (i.e. content type application/xhtml+xml, with an XML declaration) then you must use a self closing tag like so: <br/>.

    If you don't the some browsers may flat out refuse to render your page (Firefox in particular is very strict about rendering only valid xhtml+xml pages).

    As noted in 1. <br/> is also valid for HTML5 that happens to be generated as XML but served as a regular text/html without an XML declaration (such as from an XSL Transform that generates web pages, or something similar).

To clear up confusion: Putting a space before the slash isn't required in HTML5 and doesn't make any difference to how the page is rendered (if anyone can cite an example I'll retract this, but I don't believe it's true - but IE certainly does a lot of other odd things with all forms of <br> tags).

The excellent validator at http://validator.w3.org is really helpful for checking what's valid (although I'm not sure you can rely on it to also check content-type).

Anacardiaceous answered 22/12, 2009 at 13:39 Comment(4)
wait, text/html mimetype is strict XML?Margaretamargarete
You shouldn't serve it with an XML declaration if using text/html, but the content can be otherwise valid XML (e.g. generated from something that outputs XML, like XSLT output or an object that serializes to XML).Anacardiaceous
Still not understanding: if text/html should have a <br />, when should I use unclosed <br>?Margaretamargarete
I think it was Netscape Navigator 3 or 4 which didn't like <br/>, certainly nothing to be worried about in this day and age.Epistrophe
B
17

Both <br> and <br /> are acceptable in HTML5, but in the spirit of HTML, <br> should be used. HTML5 allows closing slashes in order to be more compatible with documents that were previously HTML 4.01 and XHTML 1.0, allowing easier migration to HTML5. Of course, <br/> is also acceptable, but to be compatible with some older browsers, there should be a space before the closing slash (/).

Bock answered 22/12, 2009 at 13:39 Comment(2)
@Knickerless-Noggins I'm not sure where you're reading that, but <br /> is perfectly acceptable, and W3Schools is not the spec for HTML. See the HTML5 spec, which clearly states that "Then, if the element is one of the void elements, or if the element is a foreign element, then there may be a single "/" (U+002F) character. [emphasis added]"Bock
This Answer, like others, incorrectly downplays the legitimacy of strict XML conformance in HTML5. Support for XML is not a temporary transition or migration feature. Optional support of XML conformance was a founding goal of HTML5. It is a vital feature to people using XML tooling for working with their HTML content.Penneypenni
M
11

<br> is sufficient but in XHTML <br /> is preferred according to the WHATWG and according to the W3C.

To quote Section 8.1.2.1 of HTML 5.2 W3C Recommendation, 14 December 2017

Start tags must have the following format:

  1. After the attributes, or after the tag name if there are no attributes, there may be one or more space characters. (Some attributes are required to be followed by a space. See §8.1.2.3 Attributes below.)

  2. Then, if the element is one of the void elements, or if the element is a foreign element, then there may be a single U+002F SOLIDUS character (/). This character has no effect on void elements, but on foreign elements it marks the start tag as self-closing.

If you use Dreamweaver CS6, then it will autocomplete as <br />.

To validate your HTML file on W3C see : http://validator.w3.org/

Markle answered 22/12, 2009 at 13:39 Comment(2)
@Julix Indeed, why the down-votes? Tthis is one of the few correct Answers on this page. HTML5 absolutely is XML conforming, optionally, and in XML syntax, a single tag must be closed with the slash. How can such simple facts be so misconstrued when written in black-and-white in the spec.Penneypenni
@BasilBourque, I think this can be given by that mainly the answer's 1st sentence is overly brief / misleading: "<br> is sufficient but in XHTML <br /> is preferred ..." => one could infer that <br> might be used in XHTML, which is not truth.Gizela
B
10

<br> and <br/> render differently. Some browsers interpret <br/> as <br></br> and insert two line breaks

Bearish answered 22/12, 2009 at 13:39 Comment(4)
What? Do you know which browsers?Colbycolbye
Found this when testing ie5 / ns4 era browsers. If I remember correctly it was ie in standards compliance mode. But it was a long time ago...Bearish
For strict HTML4 browsers (which is practically just HTML4 validator), <br /> means <br>&gt;.Inguinal
I know this is a very old answer, but by now browsers that misinterpret <br/> are virtually extinct. They were already quite rare by the time the answer was posted.Dulles
S
9

<br/> is the most appropriate one. This tag notation can also be used in Reactjs where a line break is required instead of <br>

Semipalatinsk answered 22/12, 2009 at 13:39 Comment(0)
P
7

Most of the cases in HTML, the tags are in pair. But for a line break you don't need a pair of tags. Therefore to indicate this, HTML uses <br/> format. <br/> is the right one. Use that format.

<br> tag has no end tag in HTML In XHTML, the <br> tag must be properly closed, like this: <br />

In XML every tag must be closed. XHTML is an extension of XML, hence all the rules of XML must be followed for valid XHTML. Hence even empty tags (nodes without child nodes) like
should be closed. XML has a short form called self closing tags for empty nodes. You can write <br></br> as <br />. Hence in XHTML <br /> is used.

HTML is very lenient in this regard, and there is no such rule. So in HTML empty nodes like <br> <hr> <meta> etc are written without the closing forward slash.

HTML

<br>
<hr>
<meta name="keywords" content="">
<link rel="canonical" href="http://www.google.com/">

XHTML

<br />
<hr />
<meta name="keywords" content="" />
<link rel="canonical" href="http://www.google.com/" />

Not all tags can be self closed. For example, a tag like <script src="jQuery.min.js" /> is not allowed by XHTML DTD.

Preglacial answered 22/12, 2009 at 13:39 Comment(0)
B
6

Ummm.....does anyone know a SINGLE vendor, user-agent, or browser maker that has ever followed the W3C Specifications 100%??? So if HTML5 says it supports all three break element versions, you can bet the vendors support the same and even more sloppier versions!

The ONLY thing that matters in this debate is to CONSISTENTLY use coding that also happens to follow XML specifications as well as HTML specifications when possible. That means you should use the correct XML version of the break tag and encourage all your team to do the same:

<br />

The same space-slash format should apply for the img, a, hr, and meta tags in your code. Why? Because:

  1. Its is backwards compatible with older XHTML user-agents / browsers
  2. The browser vendors support the XML version anyway so the HTML5 specification is moot.
  3. The sloppy implementations of most user-agents today, in the past, and in the future will accept it.
  4. It allows your markup to be comparable with XML standards should you need to go back to creating XHTML/XML documents from your markup.
  5. It's "good coding practice" for ALL WEB DEVELOPERS to keep using solid markup practices that follow XML, including coding in all lower case, quoted attributes, escaped XML characters, etc. etc. Why? In the future if you have to switch to XML data you automatically code and think in XML.
  6. We can only hope that in the future World Wide Web, we move away from private vendor-implemented standards and go back to solid, reliable, verified markup that parses faster, moves data over the wires faster, and make our future Internet a more standardized medium using XML.
  7. Old Netscape always needed the " /" space before the slash or it failed. Who cares about old browsers, right? But its one more case for my version I still like :)

Besides, in the robotic and machine world that's here, where robots don't have the same Human-interface coding problems HTML5 solves for us, they will gladly go back to XML data systems and parse such UI web pages much faster when converted to XML data.

Bareilly answered 22/12, 2009 at 13:39 Comment(2)
Who's going back to XML? Certainly not the robots! XHTML is dead, the world runs on HTML for content structure, CSS for pesky human layout needs, and JSON for data. :)Cut
@Micah Murray Why use something that has less compatibility with other standards? Why make life harder for yourself? If JSON was built into HTML markup like XHTML5/XML is, I would gladly choose it. And yes, I see a dystopian world coming where the web is automated by robots. Trust me....they will not be writing sloppy HTML :)Bareilly
A
5

Well all I know is that <br /> gives a break with a white line and <br> just gives a break in some cases. This happened to me when I was setting up an IPN-script (PHP) and sent mails and checked the inbox for it. Dont know why but I only got the message to look neat using both <br /> and <br>

Have a look at the mail here: http://snag.gy/cLxUa.jpg

The first two sections of text is seperated by <br />, hence the whitespace lines, the last three rows of text in the bottom and the last section is seperated by <br> and just gives new row.

Amanita answered 22/12, 2009 at 13:39 Comment(0)
H
-2

<br> and <br /> render differently in some browsers, so choosing either over the other isn't going to hurt your project, but do expect a bulk find..replace to affect the page render in some browsers, which may result in extra work for yourself or even embarrassment should the change affect nothing in your test browser, but break it in the preferred browser of your clients'.

I prefer <br> since it is what I have used since Erwise and Netscape Navigator (early web browsers), but there's no reason not to choose <br /> instead. It may be useful for some preprocessing, comparability, etc.

Even if your choice boils down to preferring the look of one over the other, or you (or your favourite HTML editor e.g. Dreamweaver) might like your code to be xml compliant. It's up to you.

A quick side note:

Not to be confused with br, but in addition you may also consider using wbr tags in your HTML: A word break opportunity tag, which specifies where in a text it would be ok to add a line-break.

For further reading, please have a read of the HTML5 spec.

Houdan answered 22/12, 2009 at 13:39 Comment(1)
See the HTML5 spec, which clearly states that "Then, if the element is one of the void elements, or if the element is a foreign element, then there may be a single "/" (U+002F) character. [emphasis added]" <br>, of course, is a void element, as you can see in the link in the quote.Bock

© 2022 - 2024 — McMap. All rights reserved.