Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct way to display multi line text?

Tags:

html

text

I have a HTML document with some distinct rows of text, is there some decided correct way to display them?

Example:

Here are
some lines
of text

Should I use the <p> tag for each row, or is there some other/better way to do it?

Examples:

<p>Here are</p>
<p>some lines</p>
<p>of text</p>

or

<p>
  Here are <br>
  some lines <br>
  of text <br>
</p>

Or something completely different?

The CSS & other things isn't really relevant at the moment, I'm just wondering which is the "most correct" way to use.

like image 911
Sebastian Norr Avatar asked Apr 08 '18 15:04

Sebastian Norr


4 Answers

According to this, the <br> element is used to insert a line break without starting a new paragraph. Hence you should prefer the second solution over the first.

w3schools comes with a marvelous article about style guides and coding conventions.

like image 24
Daniele Cappuccio Avatar answered Oct 22 '22 07:10

Daniele Cappuccio


if you have a string with new lines that you want to display for example in a div, you can use white-space: pre-wrap css style:

.multiline {
  white-space: pre-wrap;
}

<div class="multiline">
    A multiline text
    for demo purpose
</div>
like image 67
vir us Avatar answered Oct 22 '22 07:10

vir us


Or you can try this without tag wrapping each line:

<div style="white-space:pre">
Here are
some lines
of text
</div>
like image 27
Lerner Zhang Avatar answered Oct 22 '22 05:10

Lerner Zhang


The correct way to do things is using things made for the things you need. If you want a line break (enter), use <br>; If you want to define a paragraph, use <p>.

like image 38
D. Pardal Avatar answered Oct 22 '22 07:10

D. Pardal