Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Why indeed? Wouldn't something like &br; be more appropriate?

share|improve this question
8  
I have to say that, with exception of really rare cases (poetry comes to mind) the br tag should never be used. – Yi Jiang Aug 15 '10 at 16:27
4  
How exactly does the posi-trac rear end on a Plymouth work!? It just does. – Robert Aug 15 '10 at 16:28
4  
Probably because line breaks are ignored by HTML. (except in pre) – tcooc Aug 15 '10 at 16:31
8  
@Yi Jiang, I have to say that good English, and good use of many other languages, distinguishes a line-break from ending a block of text (paragraph, heading, etc.) and so it most certainly should be used when appropriate. – Jon Hanna Aug 15 '10 at 17:07
3  
Where you want a break stronger than the space between words, but don't want to start a new paragraph. – Jon Hanna Aug 15 '10 at 22:32
up vote 32 down vote accepted

An HTML entity reference is, depending on HTML version either an SGML entity or an XML entity (HTML inherits entities from the underlying technology). Entities are a way of inserting chunks of content defined elsewhere into the document.

All HTML entities are single-character entities, and are hence basically the same as character references (technically they are different to character references, but as there are no multi-character entities defined, the distinction has no impact on HTML).

When an HTML processor sees, for example — it replaces it with the content of that entity reference with the appropriate entity, based on the section in the DTD that says:

<!ENTITY mdash   CDATA "&#8212;" -- em dash, U+2014 ISOpub -->

So it replaces the entity reference with the entity &#8212; which is in turn a character reference that gets replaced by the character (U+2014). In reality unless you are doing this with a general-purpose XML or SGML processor that doesn't understand HTML directly, this will really be done in one step.

Now, what would we replace your hypothetical &br; with to cause a line-break to happen? We can't do so with a newline character, or even the lesser known U+2028 LINE SEPARATOR (which semantically in plain text has the same meaning as <br/> in HTML), because they are whitespace characters which are not significant in most HTML code, which is something that you should be grateful for as writing HTML would be much harder if we couldn't format for readability within the source code.

What we need is not an entity, but a way to indicate semantically that the rendered content contains a line-break at this point. We also need to not indicate anything else (we can already indicate a line-break by beginning or ending a block element, but that's not what we want). The only reasonable way to do so is to have an element that means exactly that, and so we have the <br/> element, with its related tag being put into the source code.

share|improve this answer
1  
HTML entities may be multi-character entities; the standard just doesn't define any by default. But you're right to say that <br> is an indication of a semantic line break. (Now, if you could just rail on a bit at idiot people who think that <br><br> is a replacement for <p>, my day would be complete… ;-)) – Donal Fellows Aug 15 '10 at 17:40
3  
@Donal That's precisely what I meant when I said they were technically different, but as there are no multi-s defined the distinction has no impact. As for people thinking double line-breaks is the same as a paragraph, there are too many different ways in which such thinking is wrong to be able to fit complaining about that into the allowed comment space. – Jon Hanna Aug 15 '10 at 17:57
    
+1 - Now I get it :D – Richard JP Le Guen Aug 15 '10 at 19:38

A tag and a character entity reference exist for different reasons - character entities are stand-ins for certain characters (sometimes required as escape sequences - for example &amp; for an ampersand &), tags are there for structure.

The reason the <br> tag exists is that HTML collapses whitespace. There needs to be a way to specify a hard line break - a place that has to have a line break. This is the function of the <br> tag.

There is no single character that has this meaning, though U+2028 LINE SEPARATOR has similar meaning, and even if it were to be used it would not help as it is considered to be whitespace and HTML would collapse it.

See the answers from @John Kugelman and @John Hanna for more detail on this aspect.


Not entirely related, there is another reason why a &br; character entity reference does not exist: a line break is defined in such a way that it could have more than one character, see the HTML 4 spec:

A line break is defined to be a carriage return (&#x000D;), a line feed (&#x000A;), or a carriage return/line feed pair.

Character entities are single character escapes, so cannot represent this, again in the HTML 4 spec:

A character entity reference is an SGML construct that references a character of the document character set.

You will see that all the defined character entities map to a single character. A line break/new line cannot be cleanly mapped this way, thus an entity is required instead of a character entity reference.

This is why a line break cannot be represented by a character entity reference.

Regardless, it not not needed as simply using the Enter key inserts a line break.

share|improve this answer
8  
But &br; is an entity reference and not just a character reference. It sure can represent more than just a single character. – Gumbo Aug 15 '10 at 16:41
3  
-1 I don't see how this is relevant at all. The reason a hard line break indicator is needed is because whitespace in HTML is collapsed and newlines are ignored. It doesn't have anything to do with Windows using \r\n for line endings. – John Kugelman Aug 15 '10 at 16:41
5  
That really has nothing to do with it. Different line ending encoding standards is a total red herring. The problem isn't that there's no way to represent a line ending in one character, it's that HTML doesn't differentiate between spaces, tabs, and newlines: they're all whitespace, and newlines don't get special treatment. – John Kugelman Aug 15 '10 at 16:59
6  
Indeed, in cases where whitespace is significant (in <pre> element) the different line-endings are all normalised and not an issue at all. This answer is completely misleading. – Jon Hanna Aug 15 '10 at 17:06
4  
I don’t understand why this answer is still getting up votes. From your initial wrong answer on you just seem to copy parts of the other answers to keep floating on top. But, apart from being inaccurate, your answer still does not answer the question why it’s not an entity reference that is used to mark HTML line breaks. – Gumbo Aug 16 '10 at 6:23

Entities are stand-ins for other characters or bits of text. In HTML they are used to represent characters that are hard to type (e.g. &mdash; for "—") or for characters that need to be escaped (&amp; for "&"). What would a hypothetical &br; entity stand for?

It couldn't be \r or \n or \r\n as these are already easy enough to type (just press enter). The issue you're trying to workaround is that HTML collapses whitespace in most contexts and treats newlines as spaces. That is, \n is not a line break character, it is just whitespace like tabs and spaces.

An entity &br; would have to be replaced by some other text. What character do you use to represent the concept of "hard line break"? The standard line break character \n is exactly the right character, but unfortunately it's unsuitable since it's thrown in the generic "whitespace" bucket. You'd have to either overload some other control character to represent "hard line break", or use some extended Unicode character. When HTML was designed Unicode was only a nascent, still-developing standard, so that wasn't an option.

A <br> element was the simple, straightforward way to add the concept of "hard line break" to a document since no character could represent that concept.

share|improve this answer
6  
&nbsp; was invented because spaces were ignored but people still needed to force spaces into their texts in html (without using pre). So I think it's more than a valid question why hasn't the same happened for newline. Now there is a special 0u00A0 unicode character for &nbsp; , and I think it wouldn't be a bad idea to have a similar one for newline so something like &br; could be implemented. For the exact same reason we have &nbsp; – manixrock Aug 15 '10 at 17:34
3  
@manixrock, you have the details of &nbsp; entirely backwards. &nbsp; is an entity reference and as such takes something defined elsewhere and inserts it into the source before it is processed at a higher level. If the non-breaking space character didn't already exist, then this would never have been possible. &nbsp; is useful because many people do not have a quick binding on their keyboards for non-breaking space, and because it's indistinguishable in source from space. The reason we don't have &br; is the question of what that entity should be replaced with. – Jon Hanna Aug 15 '10 at 17:48
2  
@manixrock ... indeed it has never been defined in any standard that &nbsp; can't be collapsed into a single space (that would be a valid rendering behaviour), only that it can't be treated as a word-break when deciding where to wrap text. That &nbsp; forces extra space is valid, and the choice made by all browsers, but not required. You can't say a standard did something to allow X when it doesn't even promise that X will happen. – Jon Hanna Aug 15 '10 at 17:52
    
@John Why wasn't an ASCII control character chosen as the reference for &br; ? – Pacerier Jul 13 '12 at 0:48

In HTML all line breaks are treated as white space:

A line break is defined to be a carriage return (&#x000D;), a line feed (&#x000A;), or a carriage return/line feed pair. All line breaks constitute white space.

And white space does only separate words and sequences of white space is collapsed:

For all HTML elements except PRE, sequences of white space separate "words" (we use the term "word" here to mean "sequences of non-white space characters"). […]

[…]

Note that a sequence of white spaces between words in the source document may result in an entirely different rendered inter-word spacing (except in the case of the PRE element). In particular, user agents should collapse input white space sequences when producing output inter-word space. […]

This means that line breaks cannot be expressed by plain characters. And although there are certain special characters in Unicode to unambiguously separate lines and paragraphs, they are not specified to do this in HTML too:

Note that although &#x2028; and &#x2029; are defined in [ISO10646] to unambiguously separate lines and paragraphs, respectively, these do not constitute line breaks in HTML […]

That means there is no plain character or sequence of plain characters that is to mark a line break in HTML. And that’s why there is the BR element.

Now if you want to use &br; instead of <br>, you just need to declare the entity br to represent the value <br>:

<!ENTITY br "<br>">

Having this additional entity named br declared, a general-purpose XML or SGML processor will replace every occurrence of the entity reference &br; with the value it represents (<br>). An example document:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd" [
   <!ENTITY br "<br>">
]>
<HTML>
   <HEAD>
      <TITLE>My first HTML document</TITLE>
   </HEAD>
   <BODY>
      <P>Hello &br;world!
   </BODY>
</HTML>
share|improve this answer
2  
They want to stop using <br> entirely, so they'd have to define it as <pre>&#a;</pre> – Jon Hanna Aug 15 '10 at 18:22

Entities are content, tags are structure or layout (very roughly speaking). It seems whoever made the <br> a tag decided that breaking a line has more to do with structure and layout than with content. Not being able to actually "see" a <br> I'd tend to agree. Oh and I'm making this up as I go so feel free to disagree ;)

share|improve this answer

HTML is a mark-up language - it represents the structure of a document, not how that document should appear visually. Take the <EM> tag as an example - it tells user-agents that they should give emphasis to any text that is placed between the opening and closing <EM> tags. However, it does not state how that emphasis should be represented. Yes, most visual web-browsers will place the text in italics, but this is only convention. Other browsers, such as monochrome text-only browsers may display the text in inverse. A screen reader might read the text in a louder voice, or change the pronunciation. A search-engine spider might decide the text is more important than other elements.

The same goes for the <BR> tag - it isn't just another character entity, it actually represents a break in the document structure. A <BR> is not just a replacement for a newline character, but is a "semantic" part of the document and how it is structured. This is similar to the way an <H1> is not just a way of making text bigger and bolder, but is an integral part of the way the document is structured.

share|improve this answer
    
So what kind of break was the <br> tag intended to signify? – Pacerier Jul 13 '12 at 0:54

br elements can be styled, though. How would you style an HTML entity? Because they're elements it makes them more flexible.

share|improve this answer
7  
I disagree; Styling a <br /> element is a hack; the system isn't built to accommodate hacks, hacks are built to go around the system. – Richard JP Le Guen Aug 15 '10 at 16:36
    
... I would even say that this would have been a reason in favor of it being an entity as opposed to an element. Who was at that meeting saying "But what if they need a red border around the new line?" :P – Richard JP Le Guen Aug 15 '10 at 16:46
    
Good point actually. – Gregory Baker Aug 15 '10 at 16:47
3  
@Richard Actually the main (and almost only) use of style in the br is <br style="clear: both" />. It isn't really a hack. – HoLyVieR Aug 15 '10 at 17:26
    
@Gregory Baker: To my mind, the fact that BR tags can take styles like "clear:both" is the most compelling reason for a hard line break to be represented using tag rather than an entity. Specifying that it must be mapped to some character which an implementation should render as a newline <i>after</i> white space was eliminated would also work, though special handling would be required to deal with leading blanks on new lines (if I had my druthers, the only swallowing of white space would be of newlines which are preceded or followed by whitespace (others would become blanks)) – supercat Aug 15 '10 at 19:10

Yes. An HTML entity would be more appropriate, as a break tag cannot contain text and behaves much like a newline.

That's just not the way things are, though. Too late. I can't tell you the number of non-XML-compatible HTML documents I've had to deal with because of unclosed break tags...

share|improve this answer
    
At least that's an easy one to deal with; unlike unclosed nested lists and tables. – Rex M Aug 15 '10 at 16:29
6  
A break tag does not behave like a newline, as it does not get ignored in the rendering. A break tag indicates a line break in the rendered document, which would be impossible to indicate with an entity. – Jon Hanna Aug 15 '10 at 17:09
    
It would be nice. We can add extra white space with &nbsp;, why not add page break or a newline that respected in a similar manner. It would definitely be more consistent. – Shanimal Jan 12 '13 at 3:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.