How to Change Default Text Wrapping with HTML and CSS

Unlike paper, web pages can almost forever extend sideways. As many as considerable that is, it’s not something we unequivocally need while reading. Browsers hang calm depending on a width of a calm container and a width of a screen so that we can see all a calm though carrying to corkscrew laterally many (only downwards).

Wrapping is something browsers do deliberation many factors, such as a denunciation of a calm or a chain of punctuation and spaces—they don’t only pull down what doesn’t fit in a box tangible for a calm content.

Other than wrapping, browsers also take caring of a spaces; they combine mixed uninterrupted spaces in a source formula into one singular space on a rendered page, and they also register forced line breaks before start operative on a wrapping.

When to change default calm wrapping

That’s all great, and many appreciated. But, we can simply finish adult in resources where a default browser poise isn’t what we are looking for. It can be a title that shouldn’t be wrapped or a word in a divide that better be damaged than deplane a line, withdrawal an odd-looking dull space during a finish of a line.

It competence also occur that we only desperately need those spaces recorded in a text, however a browser keeps mixing them into one, forcing us to supplement mixed nbsp; in a source code.

Wrapping preferences competence also change with a denunciation and purpose of a text. A Mandarin news essay and a French poem not indispensably need to be wrapped in a accurate same way.

There are a satisfactory series of CSS properties (and HTML elements!) that can control a jacket and a breakpoints and also define how spaces and line breaks are processed before wrapping.

Soft hang opportunities soothing hang breaks

Browsers confirm where to hang an superfluous calm depending on word boundaries, hyphens, syllables, punctuations, spaces and more. These places are all called soft hang opportunities and when a browser does mangle a calm during one such place, a mangle is called a soft hang break.

The simplest approach to force an additional break can be finished by regulating a good aged br element.

Whitespace

If we are sensitive with a white-space CSS skill we gamble we came to initial know it in a same conform as many others; while acid for a approach to prevent a jacket of text. The nowrap value of white-space does accurately that.

However, a white-space skill is about some-more than only wrapping. First of all, what is whitespace? It’s a set of space characters. Each space in a set varies from any other in length, direction, or both.

A standard single plane space character is what we supplement by dire a spacebar key. Tab pivotal also adds a similar space though a with bigger length. Enter pivotal adds a vertical space to start a new line, and nbsp; in HTML adds a single unbreakable space to web pages. Like this, there are copiousness forms of spaces that consecrate “whitespace”.

As we mentioned in a beginning, browsers collapse mixed spaces (both plane and vertical) in a source into a singular space. They also consider these space characters for jacket opportunities (places where a calm can be wrapped) when jacket is needed.

And, it’s precisely these browser actions that we can control with white-space. Note that a white-space skill doesn’t impact all kinds of space, just a many visit ones such as a unchanging plane singular space, add-on space, and line feeds.

Below, we can see a screenshot of a representation calm that’s wrapped by a browser to fit inside a container. The crawl happens during a bottom of a enclosure and a overflown calm is colored differently. You’ll notice a collapse of a uninterrupted spaces in a code.

div class='textContainer'
⚘ Hello.  ⚘ Hello.                 ⚘ Hello

⚘ Hello.



⚘ Hello.       ⚘ Hello.   ⚘ Hello.  ⚘ Hello.
⚘ Hello. ⚘ Hello. ⚘ Hello. ⚘ Hello.
⚘ Hello. ⚘ Hello.      ⚘ Hello. ⚘ Hello. ⚘ Hello.

⚘ Hello. ⚘ Hello.    ⚘ Hello. ⚘ Hello. ⚘ Hello.
⚘ Hello.               ⚘ Hello. ⚘ Hello.
⚘ Hello.   ⚘ Hello.
/div
.textContainer {
  width: 500px;
  height: 320px;
}
Text jacket by default browser behaviourText jacket by default browser behaviour

After requesting white-space: nowrap; rule, a jacket of a calm changes in a following way:

.textContainer {
  /* ... */
  white-space: nowrap;
}
Text jacket with whitespace nowrapText jacket with whitespace nowrap

The pre value of white-space preserves all a whitespaces and prevents a jacket of a text:

.textContainer {
  /* ... */
  white-space: pre;
}
Text jacket with whitespace preText jacket with whitespace pre

The pre-wrap value of white-space preserves all a whitespaces and wraps a text:

.textContainer {
  /* ... */
  white-space: pre-wrap;
}
Text jacket with whitespace pre-wrapText jacket with whitespace pre-wrap

Finally, a pre-line value of white-space preserves a straight whitespaces such as new lines and wraps a text:

.textContainer {
  /* ... */
  white-space: pre-line;
}
Text jacket with whitespace pre-lineText jacket with whitespace pre-line

Word breaks

Another critical CSS skill we should know for determining calm hang is word-break. You can see in all a above screenshots that a browser wrapped a calm before a word “hello” on a right side, over that a text overflowed. The browser didn’t mangle a word.

However, if we have to allow a violation of letters in a word so that a calm would demeanour even during a right side we need to use a break-all value for a word-break property:

.textContainer {
  /* ... */
  word-break: break-all;
}
Word mangle with break-allWord mangle with break-all

The word-break skill has a third value besides break-all and normal (belonging to a default line breaking). The keep-all value doesn’t concede violation of words.

You competence not be means to see a outcome of keep-all in English. But, in languages where letters in a word are meaningful units on their own, a browser competence mangle a difference when wrapping, and this can be prevented regulating keep-all.

For instance, a letters in Korean words, primarily damaged for wrapping, are kept together when a white-space: keep-all; order is specified.

div class='textContainer'세계를 향한 대화, 유니코드로 하십시오. 제10회 유니코드 국제 회의가 1997년 3월 10일부터 12일까지 독일의 마인즈에서 열립니다. 지금 등록하십시오. 이 회의에서는 업계 전반의 전문가들이 함께 모여 다음과 같은 분야를 다룹니다./div
.textContainer {
  /* ... */
  word-break: keep-all;
}
Word mangle with keep-allWord mangle with keep-all

This skill competence support another value called break-word in a future. You’ll see how break-word works later, in a “Overflow wrap” territory of this article.

Word mangle opportunities

Developers can also add hang opportunities inside words, regulating a wbr HTML element. If a browser needs to hang a calm fibre it will cruise a mark where wbr is benefaction for a jacket opportunity.

div class='textContainer'
⚘ Hello.  ⚘ Hello.                 ⚘ Hwbrello

⚘ Hello.



⚘ Hello.       ⚘ Hello.   ⚘ Hello.  ⚘ Hello.
⚘ Hello. ⚘ Hello. ⚘ Hello. ⚘ Hello.
⚘ Hello. ⚘ Hello.      ⚘ Hello. ⚘ Hello. ⚘ Hello.

⚘ Hello. ⚘ Hello.    ⚘ Hello. ⚘ Hello. ⚘ Hello.
⚘ Hello.               ⚘ Hello. ⚘ Hello.
⚘ Hello.   ⚘ Hello.
/div
.textContainer {
  /* ... */
  white-space: pre-wrap;
}
Wrap event with a wbr HTML tagWrap event with a wbr HTML tag

Without wbr, a whole “Hello” word would have been rendered in a new line. By adding wbr to a HTML code, we sensitive a browser that it’s fine to mangle a word during that indicate for wrapping, in box it’s necessary.

Hyphens

The hyphens CSS skill is another approach to control breaks between letters in a word. We have a separate article on CSS hyphenation if you’re interested. In short, a skill allows we to create jacket opportunities by hyphenation.

Its auto value prompts a browser to automatically hyphenate and mangle difference where needed while wrapping. The manual value army browsers to wrap (if needed) during hyphenation opportunities combined by us, such as a hyphen impression (hyphen;) or shy; (soft hyphen). If none was given as value there would be no jacket finished nearby hyphens.

div class='textContainer'
bluehouse bluehouse bluehouse bluehouse bluehouse bluehouse bluehouse
bluehouse bluehouse bluehouse bluehouse bluehouse
/div
.textContainer {
  /* ... */
  -webkit-hyphens: auto;
  -ms-hyphens: auto;
  hyphens: auto;
}
Hyphens with automobile valueHyphens with automobile value

Overflow wrap

The overflow-wrap CSS skill controls if a browser competence mangle words (or recorded spaces, support for that competence occur in a nearby future) on overflow. When a break-word value is given for overflow-wrap, a word will be broken in box no other soothing hang opportunities are found in a line.

Note that overflow-wrap is also famous as word-wrap (they are aliases).

div class='textContainer'
bluehousebluehousebluehousebluehousebluehousebluehousebluehousebluehousebluehousebluehousebluehousebluehouse
/div
.textContainer {
  /* ... */
  overflow-wrap: break-word;
}
Overflow hang with break-word valueOverflow hang with break-word value

With no space in-between a letters in a HTML formula above (i.e. no jacket opportunities), a calm wasn’t wrapped during initial and was recorded as a singular word.

However, when a accede was given to hang a calm by violation difference (i.e. a break-word value was given to overflow-wrap), a wrapping happened by violation a whole string wherever it was necessary.

Add Comment