Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New lines in text within a div

Tags:

html

textarea

People also ask

Can text be inside div?

Using CSS, you can center text in a div in multiple ways. The most common way is to use the text-align property to center text horizontally. Another way is to use the line-height and vertical-align properties. The last way exclusively applies to flex items and requires the justify-content and align-items properties.

Does div create line breaks?

By default <DIV> does cause a line break. You probably want to use a <SPAN> . Show activity on this post. You could make them float, but for menu items, it is far more common to use lists to create these.

What is \n line break?

Newline (frequently called line ending, end of line (EOL), next line (NEL) or line break) is a control character or sequence of control characters in a character encoding specification (e.g., ASCII, EBCDIC) that is used to signify the end of a line of text and the start of a new one.

How do you add a new line in text?

To add spacing between lines or paragraphs of text in a cell, use a keyboard shortcut to add a new line. Click the location where you want to break the line. Press ALT+ENTER to insert the line break.


I think a better way to achieve this is to add

white-space: pre

or

white-space: pre-wrap

style to your div. see HTML - Newline char in DIV content editable?


Add this to the CSS:

white-space: pre-line

Newline characters don't do any thing to html rendering. Change this in your js:

$('.message').html($('textarea[name="mensaje"]').val());

...to this:

$('.message').html($('textarea[name="mensaje"]').val().replace(/\n/g, "<br />"));

...and it will replace your newlines with a proper html line break