523

How to achieve same output without <br>?

<p>hello <br> How are you </p>

output:

hello
How are you
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852

28 Answers28

495

You can use white-space: pre; to make elements act like <pre>, which preserves newlines. Example:

p {
  white-space: pre;
}
<p>hello 
How are you</p>
TylerH
  • 20,799
  • 66
  • 75
  • 101
Joey Adams
  • 41,996
  • 18
  • 86
  • 115
469

Impossible with the same HTML structure, you must have something to distinguish between Hello and How are you.

I suggest using spans that you will then display as blocks (just like a <div> actually).

p span {
  display: block;
}
<p><span>hello</span><span>How are you</span></p>
johannchopin
  • 13,720
  • 10
  • 55
  • 101
Vincent Robert
  • 35,564
  • 14
  • 82
  • 119
  • 45
    note also how much additional mark-up there is - the `
    ` element exists for a very good reason. If you want the line break because they are separate paragraphs, then simply mark them up as separate paragraphs.
    – Rowland Shaw Apr 24 '10 at 08:11
  • 8
    You might need structured lines without actually using paragraphs. To markup a poem, a song or an address for example – Vincent Robert Apr 27 '10 at 10:35
  • 2
    @VincentRobert Right, but a poem is the canonical example for when `
    ` is the correct markup. Spans for a poem would be “wrong.”
    – Alan H. Jun 24 '13 at 21:18
  • 10
    Note that assigning display: block to an element will force a line break before and after, and so is not at all the same as having one line break. – jerseyboy Jul 12 '13 at 00:15
  • 18
    Definitely use the

    elements. A element should NOT be made into display:block, the whole point of is that it's inline. I would do it this way:

    hello

    How are you

    . No wonked out CSS required.

    –  Oct 31 '13 at 17:42
  • 1
    "No wonked out CSS required" You may still need to check line-height – ObjectMatrix Sep 19 '16 at 16:51
  • I was trying to figure out how to add a second line subtext to BS4 and this is solution; except use `` instead of `

    ` and set the second span to a smaller font size. Thx so much!

    – RyanNerd Oct 14 '21 at 11:42
  • or just

    helloHow are your

    – magento4u_com Mar 06 '23 at 08:37
153

Use <br/> as normal, but hide it with display: none when you don't want it.

I would expect most people finding this question want to use css / responsive design to decide whether or not a line-break appears in a specific place. (and don't have anything personal against <br/>)

While not immediately obvious, you can actually apply display:none to a <br/> tag to hide it, which enables the use of media queries in tandem with semantic BR tags.

<div>
  The quick brown fox<br />
  jumps over the lazy dog
</div>
@media screen and (min-width: 20em) {
  br {
    display: none; /* hide the BR tag for wider screens (i.e. disable the line break) */
  }
}

This is useful in responsive design where you need to force text into two lines at an exact break.

jsfiddle example

Simon Fels
  • 21
  • 5
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • 9
    Simon, you are spot on — the example you name is the exact reason I was researching this question and the `display: none` solution is by far the most appropriate and useful. – fullstackplus Jun 24 '14 at 05:53
  • 11
    Note that for cases where it would cause words to run together, you could use something like `display: inline-block; width: 1em;` instead of `none`. – Beejor Aug 11 '16 at 01:20
  • 3
    You could even apply a class to the `
    ` if you needed more control but don't go too crazy!
    – Simon_Weaver Nov 27 '16 at 06:36
  • 3
    Another "why didn't I think of this?!" answer. `
    ` is great at what it does; no need to reinvent the wheel. Thanks!
    – rinogo Oct 24 '17 at 05:30
  • Similar concept can be applied to flex-box layout : https://stackoverflow.com/questions/29732575/line-break-in-multi-line-flexbox/50978270#50978270 – Simon_Weaver Jun 21 '18 at 22:40
  • Although an interesting answer, the questioner was looking to do it without
    i.e. respecting line breaks in the HTML, which is tackled by some other answers with pre-line and pre-wrap CSS.
    – amh15 Jan 10 '19 at 19:36
  • 2
    @amh15 he doesn’t say WHY he wants to do that. Or rather what is wrong with BR. Is it preformatted text from a weather report web service, or is it a responsive design question. At time of answering all the pre/word wrap answers were already there. You’re right that the details matter but my answer has helped a lot of people that found this question based on keywords so I don’t see why you have to be so nit picky. I’d love to know what he really wanted to do. But I also don’t care. – Simon_Weaver Jan 10 '19 at 20:13
145

There are several options for defining the handling of white spaces and line breaks. If one can put the content in e.g. a <p> tag it is pretty easy to get whatever one wants.

For preserving line breaks but not white spaces use pre-line (not pre) like in:

<style>
 p {
     white-space: pre-line; /* collapse WS, preserve LB */
   }
</style>

<p>hello
How are you</p>

If another behavior is wanted choose among one of these (WS=WhiteSpace, LB=LineBreak):

white-space: normal;   /* collapse WS, wrap as necessary, collapse LB */
white-space: nowrap;   /* collapse WS, no wrapping,       collapse LB */
white-space: pre;      /* preserve WS, no wrapping,       preserve LB */
white-space: pre-wrap; /* preserve WS, wrap as necessary, preserve LB */
white-space: inherit;  /* all as parent element */

SOURCE: W3 Schools

bdkopen
  • 494
  • 1
  • 6
  • 16
petermeissner
  • 12,234
  • 5
  • 63
  • 63
70

The "\a" command in CSS generates a carriage return. This is CSS, not HTML, so it shall be closer to what you want: no extra markup.

In a blockquote, the example below displays both the title and the source link and separate the two with a carriage return ("\a"):

blockquote[title][cite]:after {
  content:attr(title)"\a"attr(cite)
}
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
David Latapie
  • 701
  • 5
  • 3
  • Fancy, but totally unneeded for what the question was. – YePhIcK Feb 06 '14 at 07:29
  • 18
    +1 because its CSS only, and doesn’t recommend use of pre, br tags nor changing the display mode to block (which adds different behavior, might break if the parent is in `display:flex` and therefore is a hack in this context). Its not fancy, really, just a modern technique. If you want the exact same markup, but to actually react differently that’s the way to go. – renoirb Jul 02 '14 at 01:29
  • Brilliant idea. Note the `"` – do not use simple quotes `'` because you want to allow the `\a` to get parsed as a special character. – Hafenkranich Aug 11 '16 at 17:17
  • 2
    i want to give +1 but it not work for me on chrome 55 – pery mimon Jan 09 '17 at 23:10
  • 5
    I would have up-voted this if there was some HTML and maybe a Fiddle to help visualize what you're doing. – John Jul 19 '17 at 23:50
41

In the CSS use the code

p {
  white-space: pre-line;
}

With this CSS every enter inside the P tag will be a break-line at the HTML.

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
burunoh
  • 744
  • 7
  • 11
  • 2
    This is exactly what I was looking for! Works perfectly for element content generated from JS (e.g. JSON result from AJAX query, angular-schema-form, etc.) that gets passed through escaping/sanitization (e.g. normal AngularJS escaping behavior when not using ngBindHtml) – Stefan Dragnev Jul 18 '16 at 14:10
31

Building on what has been said before, this is a pure CSS solution that works.

<style>
  span {
    display: inline;
  }
  span:before {
    content: "\a ";
    white-space: pre;
  }
</style>
<p>
  First line of text. <span>Next line.</span>
</p>
cruzanmo
  • 747
  • 8
  • 8
  • I just found a variation on this approach to be helpful for multi-line `input type='text', wrapping the input, and then laying the text over it with a wrapper `div`. That also requires `pointer-events:none;` on the `:before` in order to still be able to click the button below. – Eric Lease Feb 01 '16 at 06:16
11

To make an element have a line break afterwards, assign it:

display:block;

Non-floated elements after a block level element will appear on the next line. Many elements, such as <p> and <div> are already block level elements so you can just use those.

But while this is good to know, this really depends more on the context of your content. In your example, you would not want to use CSS to force a line break. The <br /> is appropriate because semantically the p tag is the the most appropriate for the text you are displaying. More markup just to hang CSS off it is unnecessary. Technically it's not exactly a paragraph, but there is no <greeting> tag, so use what you have. Describing your content well with HTMl is way more important - after you have that then figure out how to make it look pretty.

Syntax Error
  • 4,475
  • 2
  • 22
  • 33
  • 2
    But this makes it the full width of the container, which might be an unwanted side effect (especially if the item is an anchor/link). – NickG Aug 20 '15 at 10:11
  • Yes, by default block level elements take up the full width unless you set a width. Read my paragraph about context - thinking in terms of semantic context rather than choosing your html based on your design generally helps prevent you from running into issues like that. – Syntax Error Aug 20 '15 at 13:39
  • 1
    Often the reason
    tags need to be avoided is more technical than semantic.
    tags are standalone and you can't necessarily use them as you don't know if the elements in front will exist at the time you render the page, so you might not want blank lines. Consider a list of links on a vertical menu where you want them all on their own line, but can't use
    as you don't know which links will get hidden due to server-side rules. Hiding the links would cause blank lines if
    were used.
    – NickG Aug 21 '15 at 09:28
  • 1
    ..For that reason, people often use a
      list and then hide the bullets, but that's pretty hacky. It would be better if there was a css rule which just said "always render on own line".
    – NickG Aug 21 '15 at 09:29
  • CSS does have a rule like that. It's called "display:block". BTW
  • are block level elements that's why they show on their own line. There is also the option of floating and clearing but personally I think that's messy if it's overused. The reason people use li and hide the bullets with css is actually a semantic one - if they are showing a list of items you should use a list item tag. Things get way less "hacky" when you put semantics first even if it seems technically harder - you actually end up with much easier code to work with.
  • – Syntax Error Aug 21 '15 at 15:37
  • 1
    But as I said in my first comment - that has the undesirable side effect of the elements being full width :) I just need items on a new line, without them being full width. If the item is a link on the far left of the page, it means even clicks on the far right of the screen follow the link. – NickG Aug 21 '15 at 19:34