Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break line on white space between words

Tags:

css

whitespace

Is it possible to break line on white space and place the next word on a new line?

In the following example the result should look like the second title :

.test{
  text-align:center;
}
h1{  
  display:inline-block;
}
span{
   position:relative;
   top:50px;
   right:45px;
}
<div class="test">
<h1>split this</h1>
<h2>split <span>this</span> </h2>
</div>

Is it possible to break lines on each white space?

like image 863
deroccha Avatar asked Oct 23 '15 10:10

deroccha


People also ask

Is line break a whitespace?

A white space can be a sequence of spaces (entered using the space or the tab keys) or a line break (entered using the carriage return key (or the Enter key), or <br/> ). This property will specify how the white spaces in the source code of an element will be handled inside the element when rendered on the page.

What does white space pre-line mean?

pre-line. Sequences of whitespace will collapse into a single whitespace. Text will wrap when necessary, and on line breaks.

What is white space collapsing?

White space refers to empty or blank values in the code which the browser reads and renders. Html has a special feature of collapsing these white spaces. If you put extra/consecutive white spaces or newlines in the code it will regard it as one white space this is known as collapsing of white spaces.

What does white space Nowrap do?

nowrap : Multiple whitespaces are collapsed into one, but the text doesn't wrap to the next line.


2 Answers

You could use a very high value for the word-spacing property. It will break lines on each white space between words :

.test{
  text-align:center;
}
h1{
  word-spacing:9999px;
}
<div class="test">
<h1>split this</h1>
</div>
like image 164
web-tiki Avatar answered Oct 05 '22 02:10

web-tiki


You could use word-wrap in your CSS to make sure your words are not cut out and are forced in the next line if the size of your content area doesn't fit in.

Like following:

h1 {
   word-wrap: break-word;
}

If you instead would break the word, no matter the point of break (causes the word to break from the point where it doesn't fit to the area anymore) you can use word-break.

h1 {
   word-break: break-all;
}
like image 43
ProDexorite Avatar answered Oct 05 '22 03:10

ProDexorite