0

Is there a way to force a part of a text to display on a new line with CSS?

So when the HTML looks like this:

<p>Hello, my name is John.</p>

The result will be:

Hello,

my name is John.

The HTML is generated dynamically, so I cannot change it.

Reza
  • 513
  • 3
  • 7
  • 20

5 Answers5

5

p::before {
  float: right;
  width: calc(100% - 3.6em);
  height: 1em;
  content: '';
}
<p>Hello, my name is John.</p>

Please note that this is rather fidgety: the 3.6em is somewhere in between the width of "Hello," and the width of "Hello, my". So this depends on the font as well as the word(s) you need it to break after. I had to experiment a bit with different fonts to find a good average size.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
1

The short answer is, you cannot split a text without writing code in between your texts.

Follow this link which provides a good solution.

How to give line-break from css, without using <br />?

Community
  • 1
  • 1
Mahedi Hassan
  • 93
  • 1
  • 10
0

But this does not even consider where to split the lines automatically. If you want something more automated, you need to use php or javascript.

Here is an example from another link: SPLIT HTML INTO different lines

Check and let me know if it helps you.

Thanks S

Community
  • 1
  • 1
Shei Man
  • 21
  • 1
  • 6
-1

There are many ways how to do it.

First is using the <br> element of HTML

<p>Hello,<br> my name is John.</p>

Second is to put both of it in <p> tag

<p>Hello,</p>
<p>my name is John.</p>

Third is by putting both of it in <p> tag with css attribute and value clear:both - this is to make sure that no element will be floated in left or right

Try the codes below and hope it helps you.

p {
  clear: both;
}
<p>Hello,</p>
<p>my name is John.</p>

All of it will output

Hello,

my name is John.

Grinex
  • 128
  • 1
  • 1
  • 10
-4
<p>Hello, <br>my name is John.</p> 

or just put another <p></p> tags

<p>Hello, </p>&nbsp;<p>my name is John.</p>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
jeanawhou
  • 171
  • 1
  • 11