Html 5: Is It ≪Br≫, ≪Br/≫, or ≪Br /≫

HTML 5: Is it br, br/, or br /?

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.

Very few people actually use XHTML, however. You need to serve your content 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 error you make will prevent your page from being displayed in browsers that do support XHTML. 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.

What is the main difference of br and br /

In practice, </br> does not exist. Just <br> or <br />.

However, the difference is the position, and is universal for all XML tags. <TAG_NAME> indicates the beginning of a tag, and </TAG_NAME> indicates the end of a tag. When a tag is used with nothing between it, then a self-closing, or null tag can be used, which combines the beginning and end. It looks like <TAG_NAME />.

In XML, any tag can be self closing, however, with HTML, only tags which are defined as such should be used that way. So you should never do <div />, instead you should use <div></div>, even if it's empty. Some self closing tags in HTML are, as already noted, <br />, also things like <param />, <input /> and <track />. You can view the full list here.

So, basically, elements in the link above are allowed to be self closing. They often have attributes to indicate their data, but no additional elements are allowed inside of them. Other elements, which can have additional elements inside of them require both <TAG> and </TAG> to be complete.

Note that under less strict rules, in HTML, self closing tags do not require the ending slash, so <br> is equivalent to <br />. However, the latter form is preferred, and much cleaner looking. Also, any tags that aren't self closing, that aren't closed property (i.e. has a </TAG>) will cause you a nightmare because elements will have a parent who should be a sibling.

Hope that helps.

HTML: What's the correct form of BR?

It depends on the doctype you're using. In the original versions of HTML (up to HTML4), only <br> is correct. In XHTML (which is based on XML), only <br /> is correct. In HTML5, both are allowed although <br> is preferred.

What to use instead of /br in HTML5

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

Very few people actually use XHTML, however. You need to serve your content as application/xhtml+xml for it to be interpreted as XHTML, and that will not work in IE (it will also mean that any small error you make will prevent your page from being displayed, in browsers that do support XHTML). 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.

why br / and not br/?

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.

Using br instead of br /

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.

why is br / different from br/br in XHTML?

Because the XHTML spec HTML Compatability Guidelines specify that br must should be self closing. Apparently Chrome and IE8 are not follwing the spec and closing the open one for you, thus creating a second line break.

Is it sometimes bad to use BR /?

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.

There are cases where <br> is semantically valid, i.e. cases where the line break is part of the data you're sending. This is really only limited to 2 use cases - poetry and mailing addresses.

Is it out of date to use br / tag nowadays? If so, what should I use instead?

<br /> is meant to be used as to represent a break in said content, a common example would be an address:

<p>
20 street name<br />
City Name<br />
etc.
</p>

As you can see in the above example, the address is all one group of content, the breaks are only for presentational purposes.

In short: <p> tags should be used to seperate content into blocks, and the <br /> tag should be used for any breaks needed in that content (for presentational purposes, as in the example above)

The <br /> tag is perfectly valid, it's in the current HTML5 spec too.



Related Topics



Leave a reply



Submit