Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a label having text with line breaks?

I put text in a <textarea> like this:

First day 1-one apple. 2-one orange. 3-two cups of milk. 

But it shows in a <label> like this:

First day 1- one apple. 2-one orange. 3- two cups of milks. 

How do I make it look the same as in a <textarea>?

like image 945
my_ubuntu Avatar asked Apr 11 '16 20:04

my_ubuntu


People also ask

How do you put a line break in label text?

Press Shift+Enter as you type the label text to add a line break. Line breaks are only possible when you use HTML labels, and with word wrapping enabled on shapes. Both are enabled by default.

How do you break a line in a label in HTML?

The <br> HTML element produces a line break in text (carriage-return).

How do I create a multiline label in HTML?

Besides the white-space property combined with a new line character (e.g. \n ), the HTML way to break line is using <br> , add here is a small sample how they work and differ. Show activity on this post.

How do I force a line break in a text box?

Of course, there are situations in which there is need to add line breaks. You can do so by simply hitting Shift + Return or Shift + Enter , respectively.


1 Answers

Give the label white-space: pre-wrap and it will break line as the text area does

textarea {    height: 70px;  }  label {    white-space: pre-wrap;  }
<textarea>    First day  1-one apple.  2-one orange.  3-two cups of milk.  </textarea>    <br><br>    <label>    First day  1-one apple.  2-one orange.  3-two cups of milk.  </label>

Besides the white-space property combined with a new line character (e.g. \n), the HTML way to break line is using <br>, add here is a small sample how they work and differ.

Note, even space's are preserved using e.g. white-space: pre, as seen in the "inline" code sample

var sample_script = document.querySelector('.sample.script');    var name = "One Two Three";  var name1 = name.replace(/ /g, '\n');  var name2 = name.replace(/ /g, '<br>');    sample_script.innerHTML += name1;  sample_script.innerHTML += "<br><br>";  sample_script.innerHTML += name2;
div.pre {    white-space: pre;  }      /*  styling for this demo  */  body {display: flex;}  .sample {flex: 1; margin: 0 20px;}
<div class="sample inline">      Inline    <hr>    <div>      One      Two      Three    </div>      <div class="pre">      One      Two      Three    </div>  </div>    <div class="sample script">    Script    <hr>    </div>
like image 126
Asons Avatar answered Sep 22 '22 19:09

Asons