Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why <br /> and not <br/>?

Tags:

xhtml

tags

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.

like image 919
Stefano Borini Avatar asked Nov 02 '09 02:11

Stefano Borini


People also ask

Does the BR tag need to have a </ br >?

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

What does br /> mean?

The <br> HTML element produces a line break in text (carriage-return).

What should the br /> tag be used for?

The <br> tag inserts a single line break. The <br> tag is useful for writing addresses or poems.

Why BR tag is not recommended?

The main reason for not using <br> is that it's not semantic. If you want two items in different visual blocks, you probably want them in different logical blocks. In most cases this means just using different elements, for example <p>Stuff</p><p>Other stuff</p> , and then using CSS to space the blocks out properly.


4 Answers

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.

like image 89
Matt Avatar answered Oct 18 '22 18:10

Matt


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.

like image 31
Matt Hamilton Avatar answered Oct 18 '22 19:10

Matt Hamilton


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.

like image 27
SLaks Avatar answered Oct 18 '22 19:10

SLaks


There is no right way in XHTML. They are formally identical in XML. Whitespace is not significant in that location.

like image 34
bmargulies Avatar answered Oct 18 '22 20:10

bmargulies