19

Is it out of date to use
tag nowadays? If so, what should I use instead?
For new line, I coding like this. This is not supposed to do these days?

<br />
<br />
<br />
<br />
Foo
  • 800
  • 1
  • 7
  • 17

4 Answers4

23

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

Sean
  • 6,389
  • 9
  • 45
  • 69
  • 6
    At the risk of sounding like a code-nazi, ideally you should NEVER use html to control presentation. Its up to the individual developer or team conventions, but personally, I frown anytime I see
    , , , , , etc... These tags can and should be replaced by CSS.
    – Joshua Barker Mar 12 '15 at 16:42
  • 8
    Well what would you suggest doing? Wrapping the address lines in span tags and setting them display block? Not really a great solution ... and what about the strong tag? Would you suggest using a span for that too? I think you're taking separation of concerns a little too far. – Sean Mar 13 '15 at 10:58
  • 6
    Granted I have seen the
    tag misused quite a lot, but there are valid uses for it (like the address example above) where the presentation is important semantically. This is also true of the many other elements (heading elements, blockquotes, etc.)
    – Sean Mar 13 '15 at 11:00
  • Dunwood - That's exactly what I would recommend. Developers are lazy and are constantly trying find excuses for not doing things the right way. What is the point of the span, if not to wrap a snippet of text specifically so that its presentation / layout can be controlled? Additionally, its a slippery slope that I've seen all to often come back to bite me. You open the door even a crack to these types of things, and sooner or later, developers are taking all kinds of "short-cuts" for the sake of expedience (like inline styles). Bad habits are bad habits, and should not be encouraged. – Joshua Barker Mar 14 '15 at 06:49
  • 2
    Additionally, wrapping text that needs to be formatted inside a span gives you infinitely more flexibility moving forward to do whatever you need to, not just make it bold or display on its own line. Why prematurely limit yourself by hard-coding against a tag that can only one purpose? Semantically. each part of the address is its own meaningful grouping of text, why not wrap them in a span to clarify that distinction? – Joshua Barker Mar 14 '15 at 06:59
  • 4
    @JoshuaBarker You don't seem to be considering the semantic value of `
    `. Quoting from https://html.spec.whatwg.org/multipage/semantics.html#the-br-element: "The `br` element represents a line break. […] `br` elements must be used only for line breaks that are actually part of the content, as in poems or addresses. `br` elements must not be used for separating thematic groups in a paragraph."
    – Toothbrush Mar 15 '16 at 16:00
  • 1
    @JoshuaBarker Also, it may very well be a good idea to wrap in `` tags as well, but not at the expense of semantic HTML. You can always hide the `
    ` tags in CSS, and then use `::after` on the `span`s to add a comma and space, or something like that.
    – Toothbrush Mar 15 '16 at 16:27
11

HTML 5 only requires <br> however <br/> is still fine.

If you were using something like XHTML you would want that to be <br /> anyway..

This question will probably tell you everything you need to know: HTML 5: Is it <br>, <br/>, or <br />?

Community
  • 1
  • 1
cowls
  • 24,013
  • 8
  • 48
  • 78
8

(From the original question, before it was edited): The "line break end tag" should only ever have been used in XHTML served with an XML content-type (i.e. almost never) and even then you could still use <br />.


Dealing with the edited question alone:

If you want a line break then use <br> (or <br /> in XHTML). HTML 5 also permits <br /> but this is just syntactic sugar for the XML addicted and bad syntax highlighters.

The line break element isn't out of date, but like any other element, it should only be used in appropriate places. There aren't many places when a line break is appropriate, the most common ones that prince to mind are postal addresses and poetry. In most cases a <p> is more suitable. In places where multiple sequential line breaks appear, a stylesheet that adds padding or margins is often the right choice.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

I'm going to answer the question differently:

You can use <br /> but to answer the question the way I think you are asking... is that you can have the <p> or <div> control the spacing using padding element. so instead of adding <br /> just make padding on the p or div element. But as people mention <p> is the way to go :)

Riskbreaker
  • 4,621
  • 1
  • 23
  • 31