The right is br or br / or br /?

61

I see each using in a way, I even vario the form in a few moments and all work, but which one is right? Does it depend on the HTML version or the browser ?

    
asked by anonymous 09.01.2015 / 17:16

8 answers

45

HTML has always used <br> only and - more generally - <tag> and is ready whenever an element is considered "empty" (ie that it can not receive subelements). However, with the advent of XHTML (an attempt to merge HTML and XML into a unified dialect), some people were encouraged to always "close" the empty elements by using a forward slash - <br/> or <br /> , since white space is free within the tag. parsers started to accept this format to some extent, and HTML5 has standardized the behavior of void tags, so you can use one or the other and still be within standards.

(Clarifying: As long as the semantics of the empty / foreign elements are clear in your head, and you do not make the mistake of trying to close a non-empty tag in this way - eg <textarea/> - using a or other format becomes only a matter of style, subjective so to speak.)

However, a small difference between using an auto-close tag in HTML and XML (including XHTML): If you type <tag/> , and tag is considered a empty void tag) an HTML parser conforming to standards will interpret this as simply <tag> (ie it throws away the slash), while an parser of XML will treat it semantically equivalent to <tag></tag> . On the other hand, if tag is a foreign element (for example an SVG or MathML embedded in HTML) then both will interpret as <tag></tag> . If it is neither emptiness nor foreign, the behavior is undefined (and that is where the danger lives).

In the case of br this makes little difference, because at least I've never seen anyone trying to write <br></br> ... But in other tags it can do. Try saving the text below to a teste.html file:

<div>
    Teste
    <div/>
    Teste
</div>

... open it, and inspect its contents in the browser. You'll probably see something like this (eg Chrome):

<html>
    <head></head>
    <body>
        <div>
            Teste
            <div>
                Teste
            </div>
        </div>
    </body>
</html>

While you save this same file as teste.xhtml , the content displayed [on inspecting it] will be this:

<div>
    Teste
    <div><div/>
    Teste
</div>

What happened? In the case of HTML, as described, the bar was thrown away, so what the parser found was as follows (indented for better viewing):

<div>
    Teste
    <div>
        Teste
    </div>

And because of its "permissiveness" (ie robustness to malformed content - in this case, a% open tag but not closed [from the parser's point of view]), it added a div at the end. It should be noted that this code presented is not considered valid HTML, but the fact that the browser does not "complain" may lead someone to think that it is ok to use auto-close tags on any element - such as XML - and that's not true.

In the case of </div> , however, there is no practical difference, as it has already been said it is considered an empty element by specifying HTML, so it will never be interpreted as having content. Whether the final bar is present or not, the element ends right there, and that's it.

    
10.01.2015 / 21:41
21

<br/> and <br /> are essentially the same thing. The space is often used to facilitate the visual identification of the bar and is recommended, but not required, by the XHTML specification.

If you are using the XHTML specification this is the correct way to use tag . Any browser that I know accepts HTML or XHTML malformed regardless of determination to be using XHTML. In practice you use it if you want.

No one I know actually uses XHTML consciously. It's a dead spec. And some people even consider something that should not be used.

No problem using it, if you feel more comfortable to better identify the tag is self-strict, use. If you feel that for some reason whatever the code should be compatible with XHTML, always use.

I know some people who prefer this form for use with tools that only understand it.

HTML never required this and HTML5 explicitly leaves the use of the toolbars as an option for visual effect only. Of course it prevents use in tags that need a terminator.

Note that official HTML5 examples never use the toolbar. Strictly thinking about the original intent of HTML the bar should not be used.

As it is always important to maintain a pattern. Or never use, or use always. But do not worry about whether it's right or wrong, mix with legacy codes.

Obviously the same goes for other tags that are self-terminating.

<br> documentation . Documentation of void elements.

    
09.01.2015 / 17:16
17

This is a commentary on Maniero's response, but it was too big to fit into a comment. His answer is absolutely correct for today, but it does not explain why there is such confusion between forms. It says:

  

Any browser I know accepts HTML or malformed XHTML regardless of determination to be using XHTML.

That in general is true. It is true if the document is being served as text. However, for a while they said that all XML, including XHTML documents, should be served with the appropriate MIME-type - in the case of XHTML, it would be application/xhtml+xml . When served thus, the browser validates the document as XML, and complains if it finds any syntax error, displaying an error message instead of the page. At the time they started defending this idea, only Firefox did it right, if memory does not fail me. The fact is that this did not stick, XHTML died, and HTML5 came to simplify things. That is, try to detach yourself from the old habits, and use the doctype of HTML5, and simply <br> (although the variants with the bar at the end are also valid).

    
09.01.2015 / 18:04
11

(The original title of the question seemed to me a question between two formats - <br></br> and <br /> . The following answer takes this premise into account.)

<br></br> is an invalid string, since the <br> element is an empty element ( void element ).

An empty element is any element that can not, by definition, have any content between the start and end tags; the only way for an empty element to have data is via attributes .

Therefore, the only acceptable format between the two presented is <br /> .

Sources:

Void Element Definition , W3C

    
09.01.2015 / 18:01
3

In my Visual Studio when I type <br it automatically completes to <br /> , making us think that the right is <br /> . But it is known that in HTML <br> is appropriate, and in% XHTML co.de%.

You can change the Visual Studio configuration to stop completing <br /> , <br> , <img> and etc with <hr> by going to the

  

Tools - > Options - > Text Editor - > HTML - > Advanced- > XHTML coding style = False

Note: This in Visual Studio 2013

    
09.01.2015 / 18:41
2

From W3C's point of view,% w / w% should only be used. link

    
09.01.2015 / 17:39
-1

In files whose extension is .html you can use the three <br> , <br/> and <br /> in an interpolated way without causing any damage but when the extension is .xhml I remember that the presence of the / closure for all tag logo <br/> and <br /> were valid, this comparison can be seen in w3schools

    
22.04.2015 / 00:47
-1

For xhtml the right is <br/> , and for html the right is <br> .

Depends on the doctype you use.

    
05.04.2016 / 20:52