tags:

views:

1196

answers:

15

I continuously find places where I need to use the <br /> tag because CSS can't do what I need. Isn't the <br /> considered part of the "design" and not part of document structure? What is the acceptable usage for it? Should the same rules also apply to the <hr />?

Here is an example of where I feel forced to use the <br /> tag:

I want to display this:

<address>1234 south east Main St. Somewhere, Id 54555</address>

like this:

1234 south east main st.
Somewhere, Id 54555
+1  A: 

Interesting question. I have always used <br/> as a carriage return (and hence as part of content, really). Not sure if that is the right way of going about it, but its served me well.

<hr/> on the other hand...

Vaibhav
exactly what a line break is. Horizontal rule also has it's place in content, but not so commonly used.
Steve Perks
+6  A: 

<hr /> and <br />, much like everything else, can be abused to do design when they shouldn't be. <hr /> is meant to be used to visually divide sections of text, but in a localized sense. <br /> is meant to do the same thing, but without the horizontal line.

It would be a design flaw to use <hr /> across a site as a design, but in this post, for instance, it would be correct to use both <br /> and <hr />, since this section of text would still have to be a section of text, even if the site layout changed.

George Stocker
+1  A: 

I'd not say at all costs but if you want to be purist, these tags have nothing to do with structure and everything to layout but HTML is supposed to separate content from presentation. <hr /> can be done through CSS and <br/> through proper use of otehr tags like <p>.

If you do not want to be a purist, use them :)

Keltia
HTML does provide layout in the semantic meaning, but not in style. A paragraph, table, or list will produce layout just like an HR and BR tag, it's just that you're supposed to use them for what they are and don't use a hr because it works to provide a pretty horizontal line above a title.
Steve Perks
+5  A: 

Personally, I think there are much worse things you could do than use those two tags. Worrying excessively about them is a waste of time.

alex
+1  A: 

I think you should seldom need the BR tag in your templates. But at times it can be called for in the content, user generated and system generated. Like if you want to keep a piece of text in the paragraph but need a newline before it.

What are the occasions where you feel you are forced to use BR tags?

PEZ
+3  A: 

<hr/> and <br/> are presentational elements that have no semantic value to the document, so from a purist perspective yes, they ought to be avoided.

Think about HTML not as a presentational tool but rather as a document that needs to be self-describing. <hr/> and <br/> add no semantic value - rather they represent a very specific presentation in the browser.

That all being said, be pragmatic in your approach. Try to avoid them at all cost but if you find yourself coding up the walls and across the ceiling to avoid them then its better to just go ahead and use them. Semantics are important but fringe cases like this are not where they matter the most.

Andrew Hare
both br and hr do have semantic purpose for separating content. If you deal with content, then a line break or horizontal rule are justified elements, but they have very little to no use in design and layout of a site.
Steve Perks
+2  A: 

No. Why? They're useful constructs.


Adding this addendum (with accompanying HR), in case my brief answer is construed as lacking appropriate consideration. ;)

It can be, and often is, an incredible waste of time -- time someone else is usually paying for -- trying to come up with cross-browser CSS-limited solutions to UI problems that BR and HR tags, and their likes, can solve in two seconds flat. Why some UI folks waste so much time trying to come up with "pure" ways of getting around using tried-and-true HTML constructs like line breaks and horizontal rules is a complete mystery to me; both tags, among many others, are totally legitimate and are indeed there for you to use. "Pure," in that sense, is nonsense.

One designer I worked with simply could not bring himself to do it; he'd waste hours, sometimes days, trying to "code" around it, and still come up with something that didn't work in Opera. I found that totally baffling. Dude, add BR, done. Totally legal, works like a charm, and everyone's happy.

I'm all for abstracting presentation, don't get me wrong; we're all out do to the best work we can. But be sensible. If you've spent five hours trying to figure out some way to achieve, in script, something that BR gives you right now, and the gods won't rain fire down on you for doing it, then do it, and move on. Chances are if it's that problematic, there might be something wrong with your solution, anyway.

Christian Nunciato
A: 

make sure you include a DTD that specifies XHTML.

If your DTD is missing or specifies HTML4, dead meat as a correct parse of HTML4 or below would litter your document with angle brackets.

Joshua
+1  A: 

<br> is the HTML way of expressing a line break, as there is no other way of doing it.

Physical line breaks in the source code are rightfully ignored (more correctly: treated as a single white space), so you need a markup way to express them.

Not every line break is the beginning of a new paragraph, and packing text into <div>s (for example) just to avoid <br>s seems overly paranoid to me. Why do they bother you?

Tomalak
You are correct to say that <br/> is the only way to express a line break in HTML but this is something that can be better expressed in CSS where it belongs.
Andrew Hare
"can be better expressed": How?
Tomalak
Apart from that: "where it belongs" - I think *that* assumption is wrong. Line breaks are not part of the presentation. They are part of the data. Call the <br> tag the necessary compromise, if you like.
Tomalak
I can see what you mean but proper layout with CSS negates the need for presentational elements like <br/> and if you really need a line break you can clear the left side of your element.
Andrew Hare
Let's agree on the fact that a line break is meant to be a line break. I don't think it should be used as a spacer, which indeed would be "presentational use". Ripping consecutive text apart, wrapping both parts in <span>'s just so you can "clear:left" on one of them is the wrong way to do it, IMO.
Tomalak
A: 

That's bad usage if you're going Strict.

<br/> and <hr/> are not part of the content. For instance the <hr/> is commonly used to separate blocks of text. But isn't possible to that with border-bottom? And <br/> is seen in many cases as a way to limit text to a certain form, which couldn't be accomplished with css?

Of course you aren't going Strict don't worry to much.

nicruo
+5  A: 

I believe absolutely avoiding usage of a commonly accepted solution (even it is outdated) is the same thing as designing a table with <div> tags instead of <table> tags, just so you can use CSS.

When designing your website, you probably won't require the use of <br /> tags, but I can still imagine them being useful where user input is needed, for example.

I don't see anything wrong with using <hr /> but have not come across many situation where I required using them. In most cases, there probably are more elegant (and prettier) solutions than using <hr /> tags if this is what you need for vertically seperating content.

Aron Rotteveel
+10  A: 

Just so long as you don't use <br/> to form paragraphs, you're probably alright in my book ;) I hate seeing:

<p>
  ...lengthy first paragraph...
  <br/>
  <br/>
  ...lengthy second paragraph...
  <br/>
  <br/>
  ...lengthy third paragraph...
</p>

As for an address, I would do it like this:

<address class="address">
  <span class="street">1100 N. Wullabee Lane</span><br/>
  <span class="city">Pensacola</span>, <span class="state">Florida</span> 
  <span class="zip">32503</span>
</address>

But that's likely because I love jQuery and would like access to any of those parts at any given moment :)

Jonathan Sampson
why not simply use an <address> tag?<address>1100 N. Wullabee Lane<br/>Pensacola, FL<br/>32503</address>
Birk
You're right. I've never used that tag before, but it does appear to be the most proper element to use. Thanks.
Jonathan Sampson
This is getting pretty tangential, but you might want to look at the hcard microformat. It lets the (few) people that have browser support automatically import addresses into their address book, do map lookups, etc.
Matthew Crumley
+28  A: 

There is nothing wrong with using <br /> or <hr />. Neither of them are deprecated tags, even in the new HTML 5 draft spec (relevant spec info). In fact, it's hard to state correct usage of the <br /> tag better than the W3C itself:

The following example is correct usage of the br element:

<p>P. Sherman<br>
42 Wallaby Way<br>
Sydney</p>

br elements must not be used for separating thematic groups in a paragraph.

The following examples are non-conforming, as they abuse the br element:

<p><a ...>34 comments.</a><br>
<a ...>Add a comment.<a></p>

<p>Name: <input name="name"><br>
Address: <input name="address"></p>

Here are alternatives to the above, which are correct:

<p><a ...>34 comments.</a></p>
<p><a ...>Add a comment.<a></p>

<p>Name: <input name="name"></p>
<p>Address: <input name="address"></p>

<hr /> can very well be part of the content as well, and not just a display element. Use good judgement when it comes to what is content and what is not, and you'll know when to use these elements. They're both valid, useful elements in the current W3C specs. But with great power comes great responsibility, so use them correctly.

Edit 1:

Another thought I had after I first hit "post" - there's been a lot of anti-<table> sentiment among web developers in recent years, and with good reason. People were abusing the <table> tag, using it for site layout and formatting. That's not what it's for, so you shouldn't use it that way. But does that mean you should never use the <table> tag? What if you actually have an honest-to-goodness table in your code, for example, if you were writing a science article and you wanted to include the periodic table of the elements? In that case, the use of <table> is totally justified, it becomes semantic markup instead of formatting. It's the same situation with <br />. When it's part of your content (ie, text that should break at those points in order to be correct English), use it! When you're just doing it for formatting reasons, it's best to try another way.

Joshua Carmody
+2  A: 

I put in a <hr style="display:none"> between sections. For example, between columns in a multi-column layout. In browsers with no support for CSS the separation will still be clear.

oohh, I'm not sure about this one, it's an interesting idea though. Do you know of anywhere that discusses this idea?
Steve Perks
I don't know any places that discuss this, but I do this as well (minus the inline style), and for the same reason: separating distinct sections for those without CSS.
mercator
+1  A: 

BR is fine, since a hard line-break could be part of the content, for example in code blocks (even though you'd probably use the PRE-element for that) or lyrics.

HR on the other hand is purely presentational: a horizontal rule, a horizontal line. Use border-top/bottom for neighbouring elements instead.

Arve Systad