22

Nowadays I see many websites having few or no <br/> tags. Has CSS obviated the need for <br/>, or is <br/> still useful?

I am asking this so that I know whether or not to use <br/> in the future.

3
  • 1
    Is there a practical application for this question?
    – cimmanon
    Commented Jul 5, 2013 at 17:29
  • 4
    I think the question relates more to the semantical value of the <br />tag instead of the practical uses. One could ask the same question about the <hr /> tag. To me, it seems a valid question.
    – chocolata
    Commented Jul 5, 2013 at 17:37
  • In some cases can be very useful but depends on the specific situation, avoid the overuse though ;)
    – JFK
    Commented Jul 6, 2013 at 23:49

4 Answers 4

28

As D.A. said, sometimes <br> is appropriate and sometimes it isn’t – it depends on what you’re using it for. Here are some examples of what CSS to use instead of <br>, in the cases where <br> is not appropriate.

Vertical margin or padding

If you’re using <br> to add vertical space just because it looks nicer (not because the line breaks are part of the content), you should use vertical margin or padding. Instead of this:

<div class="foo">some content</div>
<br><br>
<div class="bar">more content</div>

You might want this:

<div class="foo">some content</div>
<div class="bar">more content</div>
.foo { margin-bottom: 2em; }

Depending on the situation, you might want to change the distance 2em, or using padding instead of margin, or put the space at the top of .bar instead of the bottom of .foo.

display: block

If you’re using a single <br> to break a line into two solely for visual effect, it might be better to use display: block. If you have this HTML in a form:

<label for="first-name">First name:</label>
<br>
<input type="text" id="first-name" name="first-name" />

then you should do something like this instead:

<label for="first-name">First name:</label>
<input type="text" id="first-name" name="first-name" />
form label { display: block; }

<label>s are display: inline by default, meaning they can sit on a line with other text. When you make an element display: block and don’t set its width to a fixed value like 100px, the element expands to fill the whole line, forcing the element after it to the next line.

1
14

<br> is a valid tag. But it's just a line break. You typically don't NEED line breaks as there are typically much more semanticly meaningful tags to use to format your content. But it all depends on context.

It'd be better to ask us if a specific use of the <br> tag makes sense. Post some examples of how you use it.

3
  • 6
    +1 A good use of a <br> tag is for cases where the line break is part of the data you're sending. eg. poetry, mailing addresses, etc. Commented Jul 5, 2013 at 17:28
  • 1
    CSS can be used to enforce line breaks if they exist in the markup (white-space: pre).
    – cimmanon
    Commented Jul 5, 2013 at 17:32
  • 1
    Potentially, CSS can enforce line breaks even if they don't exist in the markup, using the approach described here: lea.verou.me/2012/02/… Commented Jul 5, 2013 at 17:38
10

<br> tags are fine to use, just make sure you use them appropriately. If you find yourself using more than one to get extra spacing then you should be using css instead. A common use of the <br> tag is when inserting something like an address.

<p>Company Name<br>
Address Line 1<br>
Address Line 2<br>
City, State, Zip</p>

Try not to use them for presentation, if you need more than a single line break then use margin or padding in css.

Do not do this

<p>some paragraph text</p>
<br><br><br>
<p>more content</p>
0
6

Use <br> whenever the content contains a line break. There are a few cases:

  • In a forum, if the poster signs his/her post:

    <p>See you,<br>Jon Doe<br>
    
  • For postal addresses, if it would be an overkill to use dedicated <div>s each with its own class for the name, the street, the state etc.

    <p>Jon Doe<br>Sunrise Avenue 123<br>Saint Nowhere</p>
    
  • More generally, if users can enter formatted text, and someone enters a single line break.

2
  • My voted are finished for today else I would have been upvoted it! Anyway, thanks for this awesome answer! :) Commented Jul 5, 2013 at 20:44
  • All the websites I found name just two examples, “poems or addresses”, as examples of places where <br>s are appropriate. Good job thinking of more examples of acceptable places – signatures and reproductions of multiline user input. Commented Jul 5, 2013 at 21:24

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