Joe Cartano
Joe Cartano

Reputation: 3037

good style: <br> element in html

Is it considered bad style to use <br>? I have a text element that I would like to break up into two lines, is there a more recommended way to do this or a different element I should use?

Upvotes: 2

Views: 503

Answers (2)

user164226
user164226

Reputation:

Semantically, the break tag is intended to convey a natural break in the flow of information - it's a little vague, but the idea is to use it when you need to indicate that there is a logical separation between elements but that they are still part of the same general context.

For instance I might have fieldsets styled so that they layout inline, racking up horizontally. In that scenario if I wanted to separate two fieldsets in the same form, I'd likely use a break tag. It indicates both visually and semantically that the fieldsets are still part of the same context but that there is some concept of seperation between them.

Another, probably better example: the address element.

<address>
    <p>
        123 Main Street<br/>
        Townsville, USA 12345
    </p>
</address>

The spec equates it to a carriage return. http://www.w3.org/TR/html401/struct/text.html

They're only 'bad' when you're using them for layout - i.e.: multiple breaks to emulate spacing that should really be handled in a cosmetic layer (like CSS).

Upvotes: 4

Ry-
Ry-

Reputation: 224886

The question you need to ask is, why do you need to break up the line?

  • If it's to make it fit in a space, you need to use CSS to set word wrapping and width values.

  • If it's because they are distinct paragraphs, use the <p> tag.

  • If it's because you need to show a visually distinct sample of something, set display: block with CSS.

  • Otherwise, it might be okay. What's the reason for the break?

You want to be as semantically correct as possible, and you want to separate presentation from content.

Upvotes: 6

Related Questions