I feel silly for not being able to figure this out, but how do I turn off wordwrap? the css word-wrap
property can be forced on with break-word
, but cannot be forced off (only can be left alone with normal
value).
How do I force word wrap off?
Jon
425k79 gold badges733 silver badges803 bronze badges
asked Jan 10, 2011 at 23:19
Alexander BirdAlexander Bird
38k42 gold badges124 silver badges159 bronze badges
2
You need to use the CSS white-space
attribute.
In particular, white-space: nowrap
and white-space: pre
are the most commonly used values. The first one seems to be what you ‘re after.
answered Jan 10, 2011 at 23:21
0
Added webkit specific values missing from above
white-space: -moz-pre-wrap; /* Firefox */
white-space: -o-pre-wrap; /* Opera */
white-space: pre-wrap; /* Chrome */
word-wrap: break-word; /* IE */
answered Jul 7, 2015 at 15:07
I wonder why you find as solution the «white-space» with «nowrap» or «pre», it is not doing the correct behaviour: you force your text in a single line!
The text should break lines, but not break words as default. This is caused by some css attributes: word-wrap, overflow-wrap, word-break, and hyphens. So you can have either:
word-break: break-all;
word-wrap: break-word;
overflow-wrap: break-word;
-webkit-hyphens: auto;
-moz-hyphens: auto;
-ms-hyphens: auto;
hyphens: auto;
So the solution is remove them, or override them with «unset» or «normal»:
word-break: unset;
word-wrap: unset;
overflow-wrap: unset;
-webkit-hyphens: unset;
-moz-hyphens: unset;
-ms-hyphens: unset;
hyphens: unset;
UPDATE: i provide also proof with JSfiddle: https://jsfiddle.net/azozp8rr/
answered May 12, 2018 at 9:55
zodzod
3773 silver badges4 bronze badges
2
white-space: nowrap;
: Will never break text, will keep other defaults
white-space: pre;
: Will never break text, will keep multiple spaces after one another as multiple spaces, will break if explicitly written to break(pressing enter in html etc)
answered Nov 13, 2020 at 20:28
1
This worked for me to stop silly work breaks from happening within Chrome textareas
word-break: keep-all;
answered Oct 2, 2019 at 21:54
If you want a HTML only solution, we can just use the pre
tag. It defines «preformatted text» which means that it does not format word-wrapping. Here is a quick example to explain:
div {
width: 200px;
height: 200px;
padding: 20px;
background: #adf;
}
pre {
width: 200px;
height: 200px;
padding: 20px;
font: inherit;
background: #fda;
}
<div>Look at this, this text is very neat, isn't it? But it's not quite what we want, though, is it? This text shouldn't be here! It should be all the way over there! What can we do?</div>
<pre>The pre tag has come to the rescue! Yay! However, we apologise in advance for any horizontal scrollbars that may be caused. If you need support, please raise a support ticket.</pre>
answered Nov 4, 2020 at 19:41
corn on the cobcorn on the cob
1,8043 gold badges19 silver badges26 bronze badges
In this snippet, we’re going to demonstrate how you can disable word wrapping in HTML. Actually, this can be done with a few steps using some CSS properties.
To prevent the text from wrapping, you can use the CSS white-space property with the “nowrap” or “pre” value. In this snippet, you can see examples with both of them.
- Usa an <h2> element and add a <div> element.
<h2>Example</h2>
<div>
Lorem Ipsum is simply dummy text.Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text.Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text.
</div>
- Set the white-space property to “nowrap” for the <div> element.
div {
white-space: nowrap;
}
Now, let’s see the result.
Example of disabling word wrapping with the “nowrap” value of the white-space property:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
div {
white-space: nowrap;
border: 1px solid #cccccc;
}
</style>
</head>
<body>
<h2>Example</h2>
<div>
Lorem Ipsum is simply dummy text.Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text.Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text.
</div>
</body>
</html>
Result
Lorem Ipsum is simply dummy text.Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text.Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text.
Example of disabling word wrapping with the “pre” value of the white-space property:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
div {
white-space: pre;
}
</style>
</head>
<body>
<h2>Example</h2>
<div>
Lorem Ipsum is simply dummy text.Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text.Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text. Lorem Ipsum is simply dummy text.
</div>
</body>
</html>
Note: This article is focused on the semantics of the English Language as the Writing System. Other systems, especially CJK (Chinese Japanese Korean) have conventions and overflow requirements that vary from English and are out of the scope of this article.
Text Wrapping
In CSS, overflow is the scenario when the content inside a fixed-width container, is wider than the container’s width. The default behavior of CSS is to render the content flowing out of the container. This may look ugly but this helps the developer see the issue and fix it — instead of the issue getting hidden which can cause potential missing information for the user. For example, a form submission button overflowing and becoming inaccessible. So to avoid such issues, CSS by default prevents Data Loss.
Content overflowwwwwwwwwww
CSS offers multiple capabilities to fix this issue.
Property: overflow-wrap (alias word-wrap)
This property applies to inline elements. It determines whether the browser should break an otherwise unbreakable string to avoid it from overflowing its parent’s width.
It has the following possible keyword values.
- normal
- Anywhere
- break-word
overflow-wrap: normal
When set to normal, the browser will break the string on default/natural opportunities, such as a blank space or a hyphen (‘-’) character. It will also leverage soft-hyphen entity ­
to break.
This is the initial value of the overflow-wrap
property. So by default, every string will be broken at soft wrap opportunities, if any, on overflow.
This is how ‘ContentOverflowing’ and ‘Content-Overflowing’ will be handled.
ContentOverflowing
Content-Overflowing
overflow-wrap: anywhere;
This value allows the browser to break the string anywhere to avoid overflow.
Consider the following scenario with the default overflow-wrap: normal;
value for a fixed-width container.
ContentOverflow
There is no blank space, a hyphen, or any other soft wrap opportunity in the string. Therefore, it overflows. If we apply overflow: anywhere;
, we get the following, wrapped result.
ContentOverflow
overflow-wrap: break-word;
It behaves the same as overflow-wrap: anywhere;
. The difference is that the former does not consider soft-wrap opportunities when calculating min-content intrinsic sizes. In case you have not explored extrinsic vs intrinsic sizing, Ahmed Shadeed provides a great resource. It breaks only those words which have a width smaller than the available width.
Content is Overflowing
Property: word-break
CSS offers another property, word-break for handling the same issue — overflows.
It has the following keyword values
- normal
- break-all
- keep-all
- break-word
word-break: normal;
Words will break at the default rules — such as a blank space, a hyphen, etc.
This is how ‘ContentOverflow’ and ‘Content-Overflow’ will be handled.
ContentOverflow
Content-Overflow
word-break: break-all;
Break the word at the point it overflows. It does not take into account if placing the overflowing word onto the next line will eliminate the overflow in the first place or not. This doesn’t apply to CJK writing systems.
ContentOverflow
word-break: keep-all;
For Non-CJK systems, the behavior is the same as word-break: normal
.
ContentOverflow
word-break: break-word;
It has the same effect that word-break: normal;
and overflow-wrap: anywhere;
has. But unlike word-break: break-all;
, it takes into account if placing the overflowing word onto the next line will eliminate the overflow.
For example, let’s see how word-break: break-word;
handles the following scenario:
Content is Overflowing Again
We observe that the whole word ‘Overflowing’ was moved onto the next line instead of breaking as it can fit the available width without overflowing. If we apply word-break: break-all;
to it, this is what we get:
Content is Overflowing Again
The word ‘Overflowing’ was broken at exactly the point where it otherwise caused the overflow. And it was not considered if moving it onto the next line eliminated the overflow or not.
overflow-wrap vs word-break
At a high level, both properties solve the same issue. But, a key difference lies in how both the properties approach the issue and the subtle aesthetic variation in their outcomes.
To visualize, consider a fixed and short-width container for the test “A Very LongWordThatHasNoBreakingPossibilities”.
A Very LongWordThatHasNoBreakingPossibilities
Let’s solve the overflow with overflow-wrap: break-word;
.
A Very LongWordThatHasNoBreakingPossibilities
Now, let’s solve it with word-break: break-all;
.
A Very LongWordThatHasNoBreakingPossibilities
Notice the difference? word-break: break-all;
breaks the word even if placing the word on the next line would eliminate the need for breaking. This prevents large gaps before the breaks — and produces visually better results. The difference is more clearly visible in the overflow-wrap: anywhere;
vs word-break: break-all;
case. A case of the apparently twin properties. Consider you have a very short space to squeeze in a checkbox and a text which can not fit on the same line without overflowing. This is how the outcome looks like with overflow-wrap: anywhere;
:
Photosynthesis
We observe that a lot of real estate beside the checkbox has been left unutilized. A better fix is provided by word-break: break-all;
:
Photosynthesis
As observed, word-break
discards the possibility of the word fitting the next line and prefers optimizing the usage of available real estate — this is often the better adjustment visually.
The above example receives its inspiration from MDN’s resource on text wrapping.
Summary
Property | Value | Behavior | When To Use | Example |
---|---|---|---|---|
overflow-wrap |
|
Break at natural line breakpoints such as blank space, a hyphen | When overflow is determined to not be a possibility |
Content |
anywhere |
Break between any 2 characters where the overflow occurs and consider soft wrap opportunities when calculating the min-content intrinsic sizes | When overflow should be handled by breaking long words. As discussed, the alternative option of word-break: break-all; produces visually better results |
ContentOverflow |
|
break-word |
Break between any 2 characters but do not consider soft wrap opportunities when calculating the min-content intrinsic sizes | When overflow should be handled by breaking only those words which have a width smaller than the available width |
Content is Overflowing |
|
word-break |
normal |
Break at default rules | When overflow is determined to not be a possibility |
Content |
break-all |
Break exactly where the content overflows | When overflow should be handled by breaking text exactly at the point of overflow — even if placing the word on a new line eliminates the overflow |
Content is Overflowing Again |
|
break-word |
Same as word-break: normal; and overflow-wrap: anywhere; — Break can create gaps unlike word-break: break-all; |
When placing the overflowing word onto the next line eliminates overflow. This can cause gaps. |
Content is Overflowing Again |
Examples
Here are examples from the above summary in a codepen to help demonstrate what the CSS code should look like:
<section class="centered"> <h2>Without Handling Overflow</h2> <div>Content with aVeryVeryVeryLongWord</div> <!----> <h2>Handling Overflow with overflow-wrap</h2> <h3>overflow-wrap: normal;</h3> <div class="ow-normal">Content with aVeryVeryVeryLongWord</div> <h3>overflow-wrap: anywhere;</h3> <div class="ow-anywhere">Content with aVeryLongWordThatDoesNotFit</div> <h3>overflow-wrap: break-word;</h3> <div class="ow-break-word">Content with aVeryLongWordThatDoesNotFit</div> <!----> <h2>Handling Overflow with word-break</h2> <h3>word-break: normal;</h3> <div class="wb-normal">Content with aVeryVeryVeryLongWord</div> <h3>word-break: break-all;</h3> <div class="wb-break-all">Content with aVeryLongWordThatDoesNotFit</div> <h3>word-break: break-word;</h3> <div class="wb-break-word">Content with aVeryLongWordThatDoesNotFit</div> </section>
* { font-family: sans-serif; } section.centered { text-align: center; } div { display: inline-block; width: 130px; border: 3px solid #48abe0; text-align: left; } .ow-normal { overflow-wrap: normal; } .ow-anywhere { overflow-wrap: anywhere; } .ow-break-word { overflow-wrap: break-word; } .wb-normal { word-break: normal; } .wb-break-all { word-break: break-all; } .wb-break-word { word-break: break-word; } h3 { font-weight: normal; font-style: italic; border-top: 1px solid #b5b5b5; width: 30%; margin-left: auto; margin-right: auto; margin-top: 20px; padding-top: 20px; }
Conclusion
This article has scratched the surface of text-wrapping. Wrapping in itself is a deeper topic as it is tightly coupled to the semantics of the target language. Moreover, it is becoming common to offer web content in multiple languages — aka Internatiolaisation/ Localisation — which makes learning it more important than before for the developers.
Unlike paper, web pages can almost infinitely extend sideways. As much as impressive that is, it’s not something we really need while reading. Browsers wrap text depending on the width of the text container and the width of the screen so that we can see all the text without having to scroll sideways much (only downwards).
Wrapping is something browsers do considering many factors, such as the language of the text or the placement of punctuation and spaces—they don’t just push down what doesn’t fit in the box defined for the text content.
Other than wrapping, browsers also take care of the spaces; they merge multiple consecutive spaces in the source code into one single space on the rendered page, and they also register forced line breaks before start working on the wrapping.
Read Also: 15 Beautiful Text Effects Created with CSS
When to change default text wrapping
That’s all great, and much appreciated. But, we can easily end up in circumstances where the default browser behaviour isn’t what we are looking for. It can be a headline that shouldn’t be wrapped or a word in a paragraph that better be broken than descend a line, leaving an odd-looking empty space at the end of the line.
It may also happen that we just desperately need those spaces preserved in our text, however the browser keeps combining them into one, forcing us to add multiple
in the source code.
Wrapping preferences might also change with the language and purpose of the text. A Mandarin news article and a French poem not necessarily need to be wrapped in the exact same way.
There are a fair number of CSS properties (and HTML elements!) that can control the wrapping and the breakpoints and also define how spaces and line breaks are processed before wrapping.
Soft wrap opportunities & soft wrap breaks
Browsers decide where to wrap an overflowing text depending on word boundaries, hyphens, syllables, punctuations, spaces and more. These places are all called soft wrap opportunities and when the browser does break the text at one such place, the break is called a soft wrap break.
The simplest way to force an extra break can be done by using the good old <br>
element.
Whitespace
If you are familiar with the white-space
CSS property I bet you came to first know it in the same fashion as many others; while searching for a way to prevent the wrapping of text. The nowrap
value of white-space
does exactly that.
However, the white-space
property is about more than just wrapping. First of all, what is whitespace? It’s a set of space characters. Each space in the set varies from each other in length, direction, or both.
A typical single horizontal space character is what we add by pressing the spacebar key. Tab key also adds a similar space but a with bigger length. Enter key adds a vertical space to start a new line, and
in HTML adds a single unbreakable space to web pages. Like this, there are plenty types of spaces that constitute “whitespace”.
As I mentioned in the beginning, browsers collapse multiple spaces (both horizontal and vertical) in the source into a single space. They also consider these space characters for wrapping opportunities (places where a text can be wrapped) when wrapping is needed.
And, it’s precisely these browser actions that we can control with white-space
. Note that the white-space
property doesn’t affect all kinds of space, just the most frequent ones such as the regular horizontal single space, tab space, and line feeds.
Below, you can see a screenshot of a sample text that’s wrapped by the browser to fit inside its container. The overflow happens at the bottom of the container and the overflown text is colored differently. You’ll notice the collapse of the consecutive spaces in the 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; }
After applying white-space: nowrap;
rule, the wrapping of the text changes in the following way:
.textContainer { /* ... */ white-space: nowrap; }
The pre
value of white-space
preserves all the whitespaces and prevents the wrapping of the text:
.textContainer { /* ... */ white-space: pre; }
The pre-wrap
value of white-space
preserves all the whitespaces and wraps the text:
.textContainer { /* ... */ white-space: pre-wrap; }
Finally, the pre-line
value of white-space
preserves the vertical whitespaces such as new lines and wraps the text:
.textContainer { /* ... */ white-space: pre-line; }
Word breaks
Another important CSS property you should know for controlling text wrap is word-break
. You can see in all the above screenshots that the browser wrapped the text before the word “hello” on the right side, beyond which the text overflowed. The browser didn’t break the word.
However, if you have to allow the breaking of letters in a word so that the text would look even at the right side you need to use the break-all
value for the word-break
property:
.textContainer { /* ... */ word-break: break-all; }
The word-break
property has a third value besides break-all
and normal
(belonging to the default line breaking). The keep-all
value doesn’t allow breaking of words.
You might not be able to see the effect of keep-all
in English. But, in languages where letters in a word are meaningful units on their own, the browser might break the words when wrapping, and this can be prevented using keep-all
.
For instance, the letters in Korean words, initially broken for wrapping, are kept together when the white-space: keep-all;
rule is specified.
<div class='textContainer'>세계를 ֥Õœ 대Ù”, 유니코드로 Õ˜ì‹Â시오. 제10ÚŒ 유니코드 êµÂ제 ÚŒì˜가 1997년 3월 10ì¼부Ä° 12ì¼까지 ëÂ…ì¼ì˜ 마ì¸즈ì—Â서 열립니다. 지금 등ë¡ÂÕ˜ì‹Â시오. ì´ ÚŒì˜ì—Â서는 업계 전반ì˜ 전문가들ì´ Õ¨ê»˜ 모여 다ìŒ과 같ì€ 분야를 다룹니다.</div>
.textContainer { /* ... */ word-break: keep-all; }
This property might support another value called break-word
in the future. You’ll see how break-word
works later, in the “Overflow wrap” section of this article.
Word break opportunities
Developers can also add wrap opportunities inside words, using the <wbr>
HTML element. If a browser needs to wrap a text string it will consider the spot where <wbr>
is present for a wrapping opportunity.
<div class='textContainer'> ⚘ Hello. ⚘ Hello. ⚘ H<wbr>ello ⚘ 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; }
Without <wbr>
, the whole “Hello” word would have been rendered in a new line. By adding <wbr>
to the HTML code, we informed the browser that it’s okay to break the word at that point for wrapping, in case it’s necessary.
Hyphens
The hyphens
CSS property is another way to control breaks between letters in a word. We have a separate article on CSS hyphenation if you’re interested. In short, the property allows you to create wrapping opportunities through hyphenation.
Its auto
value prompts the browser to automatically hyphenate and break words where needed while wrapping. The manual
value forces browsers to wrap (if needed) at hyphenation opportunities added by us, such as the hyphen character (‐) or
(soft hyphen). If none
was given as value there would be no wrapping done near 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; }
Overflow wrap
The overflow-wrap
CSS property controls if a browser may break words (or preserved spaces, support for which might happen in the near future) on overflow. When the break-word
value is given for overflow-wrap
, the word will be broken in case no other soft wrap opportunities are found in the line.
Note that overflow-wrap
is also known as word-wrap
(they are aliases).
<div class='textContainer'> bluehousebluehousebluehousebluehousebluehousebluehousebluehousebluehousebluehousebluehousebluehousebluehouse </div>
.textContainer { /* ... */ overflow-wrap: break-word; }
With no space in-between the letters in the HTML code above (i.e. no wrapping opportunities), the text wasn’t wrapped at first and was preserved as a single word.
However, when the permission was given to wrap the text by breaking words (i.e. the break-word
value was given to overflow-wrap
), the wrapping happened by breaking the whole string wherever it was necessary.
Read Also: Working with Text in Scalable Vector Graphics (SVG)
Editor’s note: This complete guide to word-wrap
, overflow-wrap
, and word-break
in CSS was last updated 24 February 2023 to reflect the reflect the most recent version of CSS, include interactive code examples, and include a section on how to wrap text using CSS. To learn more about the overflow
property, check out our guide to CSS overflow
.
Making a site responsive so that it displays correctly on all devices is very important in this day and age. Unfortunately, despite your best efforts to do so, you may still end up with broken layouts. Broken layouts can happen when certain words are too long to fit in their container. Content overflow can occur when you are dealing with user-generated content you have no control over, such as the comments section of a post. Therefore, you need to apply styling to prevent content from overflowing their container.
Content overflow is a common problem for frontend developers. On the web, overflow occurs when your content doesn’t fit entirely within its containing element. As a result, it spills outside. In CSS, you can manage content overflow mainly using the overflow
, word-wrap
, overflow-wrap
, and word-break
CSS properties. However, our focus in this article will be on the word-wrap
, overflow-wrap
, and word-break
CSS properties.
Jump ahead:
- Using
word-wrap
,overflow-wrap
, andword-break
CSS properties- How does content wrapping occur in browsers?
- What is the difference between a soft wrap break and a forced line break?
- Understanding the
Word-wrap
andoverflow-wrap
CSS propertiesNormal
Anywhere
Break-word
- Implementing the
Word-break
CSS property- Setting
word-break
toNormal
- The
Break-all
value - Using the
Keep-all
value
- Setting
- What is the difference between
overflow-wrap
andword-break
? - How to wrap text using CSS
- Troubleshooting CSS content overflow with Chrome DevTools
Using word-wrap
, overflow-wrap
, and word-break
CSS properties
You can use the word-wrap
, overflow-wrap
, or word-break
CSS properties to wrap or break words that would otherwise overflow their container. This article is an in-depth tutorial on the word-wrap
, overflow-wrap
, and word-break
CSS properties and how you can use them to prevent content overflow from ruining your nicely styled layout. Before we get started, let us understand how browsers wrap content in the next section.
How does content wrapping occur in browsers?
Browsers and other user agents perform content wrapping at allowed breakpoints, referred to as soft wrap opportunities. A browser will wrap content at a soft wrap opportunity, if one exists, to minimize content overflow. In English and other similar writing systems, soft wrap opportunities occur by default at word boundaries in the absence of hyphenation. Because words are bound by spaces and punctuation, that is where soft wraps occur.
Although soft wraps occur in space characters in English texts, the situation might be different for non-English writing systems. Some languages do not use spaces to separate words, meaning that content wrapping depends on the language or writing system. The value of the lang
attribute you specify on the HTML
element is mostly used to determine which language system is used.
This article will focus mainly on the English language writing system. The default wrapping at soft wrap opportunities may not be sufficient if you are dealing with long, continuous text, such as URLs or user-generated content, which you have very little or no control over. Before we go into a detailed explanation of these CSS properties, let’s look at the differences between soft wrap break and forced line break in the section below.
What is the difference between a soft wrap break and a forced line break?
Any text wrap that occurs at a soft wrap opportunity is referred to as a soft wrap break. For wrapping to occur at a soft wrap opportunity, you need to make sure you’ve enabled wrapping. For example, setting the value of white-space
CSS property to nowrap
will disable wrapping. Forced line breaks are caused by explicit line-breaking controls or line breaks marking the end or start of blocks of text.
Understanding the Word-wrap
and overflow-wrap
CSS properties
The name word-wrap
is the legacy name for the overflow-wrap
CSS property. Word-wrap
was originally a non-prefixed Microsoft extension and was not part of the CSS standard, though most browsers implemented it with the name word-wrap
. According to the draft CSS3 specification, browsers should treat word-wrap
as a legacy name alias of the overflow-wrap
property for compatibility.
Most recent versions of popular web browsers have implemented the overflow-wrap
property. The draft CSS3 specification refers to the overflow-wrap
property as:
This property specifies whether the browser may break at otherwise disallowed points within a line to prevent overflow when an otherwise-unbreakable string is too long to fit within the line box.
If you have a white-space
property on an element, you need to set its value to allow wrapping for overflow-wrap
to have an effect. Below are the values of the overflow-wrap
property:
overflow-wrap: normal; overflow-wrap: anywhere; overflow-wrap: break-word;
You can also use the global values inherit
, initial
, revert
, and unset
with overflow-wrap
, but we won’t cover them here. In the subsections below, we will look at the values of the overflow-wrap
CSS property outlined above to understand the behavior of this property.
Normal
Applying the value normal
will make the browser use the default line-breaking behavior of the system. For English and other related writing systems, line breaks will therefore occur at whitespaces and hyphens, as shown below:
.my-element{ overflow-wrap: normal; }
In the example below, there is a word in the text that is longer than its container. Because there is no soft wrap opportunity and the value of the overflow-wrap
property is normal
, the word overflows its container. It describes the default line-breaking behavior of the system:
See the Pen
overflow-wrap-normal by Joseph Mawa (@nibble0101)
on CodePen.
Anywhere
Using the value anywhere
will break an otherwise unbreakable string at arbitrary points between two characters. It will not insert a hyphen character even if you apply the hyphens
property on the same element.
The browser will break the word only if displaying the word on its line will cause an overflow. If the word still overflows when placed on its line, it will break the word at the point where an overflow would otherwise occur. When you use anywhere
, the browser will consider the soft wrap opportunities introduced by the word break when calculating min-content
intrinsic sizes:
.my-element{ overflow-wrap: anywhere; }
Unlike in the previous section, where we used overflow-wrap: normal
, in the example below, we are using overflow-wrap: anywhere
. The overflowing word that is otherwise unbreakable is broken into chunks of text using overflow-wrap: anywhere
so that it fits in its container:
See the Pen
overlow-wrap-anywhere by Joseph Mawa (@nibble0101)
on CodePen.
Most recent versions of desktop browsers support overflow-wrap:
anywhere
. However, support for some mobile browsers is either lacking or unknown. The image below shows the browser support:
Break-word
The value break-word
is like anywhere
in terms of functionality. If the browser can wrap the overflowing word to its line without overflowing, that is what it will do. However, if the word still overflows its container even when it is on its line, the browser will break it at the point where the overflow would otherwise occur:
.my-element{ overflow-wrap: break-word; }
The example below shows how the browser breaks the overflowing text when you apply overflow-wrap: break-word
:
See the Pen
overflow-wrap-break-word by Joseph Mawa (@nibble0101)
on CodePen.
Notice that the text appears the same as in the last subsection. The difference between overflow-wrap: anywhere
and overflow-wrap: break-word
is in the min-content
intrinsic sizes.
The difference between anywhere
and break-word
is apparent when calculating the min-content
intrinsic sizes. With break-word
, the browser doesn’t consider the soft wrap opportunities introduced by the word break when calculating min-content
intrinsic sizes, but it does with anywhere
. For more about min-content
intrinsic sizes, check out our guide here.
The value break-word
has decent coverage among the most recent versions of desktop browsers. Unfortunately, you cannot say the same about their mobile counterpart. It is, therefore, safer to use the legacy word-wrap: break-word
instead of the more recent overflow-wrap: break-word
.
The image below shows browser support for overflow-wrap: break-word
:
The most recent versions of desktop browsers have support, while support for some mobile browsers is unknown.
Implementing the Word-break
CSS property
Word-break
is another CSS property you can use to specify soft wrap opportunities between characters. You can use this property to break a word at the exact spot where an overflow would occur and wrap it onto the following line.
The draft CSS3 specification refers to the word-break
CSS property as:
This property specifies soft wrap opportunities between letters, i.e., where it is “normal” and permissible to break lines of text. It controls what types of letters the browser can glom together to form unbreakable “words” — causing CJK characters to behave like non-CJK text or vice versa.
Below are the possible values of the word-break
CSS property. Like overflow-wrap
, you can use the global values inherit
, initial
, revert
, and unset
with word-break
, but we won’t cover them here:
word-break: normal; word-break: break-all; word-break: keep-all;
Break-word
is also a value of the word-break
CSS property, though it was removed. However, browsers still support it for legacy reasons. Specifying this property has the same effect as word-break: normal
and overflow-wrap: anywhere
.
Now that we know the break-word
CSS property and its corresponding values, let us look at them in the subsections below.
Setting word-break
to Normal
Setting the value of the word-break
property to normal
will apply the default word breaking rules:
.my-element{ word-break: normal; }
The example below illustrates what happens when you apply the styling word-break: normal
to a block of text that contains a word longer than its container:
See the Pen
word-break-normal by Joseph Mawa (@nibble0101)
on CodePen.
What you see is the browser’s usual word-breaking rules in effect.
The Break-all
value
The value break-all
will insert a line break at the exact point where the text would otherwise overflow for non-Chinese, non-Japanese, and non-Korean writing systems. It will not put the word on its own line, even if doing so will prevent the need to insert a line break:
.my-element{ word-break: break-all; }
In the example below, I am applying word-break: break-all
styling to a p
element of width 240px
containing an overflowing text. The browser will insert a line break at the point where an overflow would occur and wrap the remaining text to the following line:
See the Pen
word-break-break-all by Joseph Mawa (@nibble0101)
on CodePen.
Using break-all
will break a word between two characters at the exact point where an overflow would occur in English and other related language systems. However, it won’t apply the same behavior to Chinese, Japanese, and Korean (CJK) texts.
It doesn’t apply the same behavior for CJK texts because CJK writing systems have their own rules for applying breakpoints. Creating a line break between two characters arbitrarily just for the sake of avoiding overflow might significantly change the overall meaning of the text. For CJK systems, the browser will apply line breaks at the point where such breaks are allowed.
Using the Keep-all
value
If you use the value keep-all
, the browser will not apply word breaks to CJK texts, even if there is content overflow. The effect of applying keep-all
value is the same as that of normal
for non-CJK writing systems:
.my-element{ word-break: keep-all; }
In the example below, applying word-break: keep-all
will have the same effect as word-break: normal
for a non-CJK writing system such as English:
See the Pen
word-break-keep-all by Joseph Mawa (@nibble0101)
on CodePen.
The image below shows the browser support for word-break: keep-all
:
This value has support in most popular desktop browsers. Unfortunately, it is not the case for mobile browsers. Now that we have looked at the overflow-wrap
and word-break
CSS properties, what is the difference between the two? The section below will shed light on that.
What is the difference between overflow-wrap
and word-break
?
You can use the CSS properties overflow-wrap
and word-break
to manage content overflow. However, there are differences in the way the two properties handle it.
Using overflow-wrap
will wrap the entire overflowing word to its line if it can fit in a single line without overflowing its container. The browser will break the word only if it cannot place it on a new line without overflowing. In most cases, the overflow-wrap
property or its legacy name word-wrap
might manage content overflow. Using word-wrap: break-word
will wrap the overflowing word onto a new line and goes ahead to break it between two characters if it still overflows its container.
Word-break
will ruthlessly break the overflowing word between two characters even if placing it on its line will negate the need for word break. Some writing systems, like the CJK writing systems, have strict word breaking rules the browser takes into consideration when creating line breaks using word-break
.
How to wrap text using CSS
As hinted above, if you want to wrap text or break a word overflowing the confines of its box, your best bet is the overflow-wrap
CSS property. You can also use its legacy name, word-wrap
. Try the word-break
CSS property if the overflow-wrap
property doesn’t work for you. However, be aware of the differences between overflow-wrap
and word-break
highlighted above.
Below is an illustration of the overflow-wrap
and word-wrap
CSS properties. You can play with the CodePen to understand their effects:
See the Pen
how-to-wrap-text by Joseph Mawa (@nibble0101)
on CodePen.
Troubleshooting CSS content overflow with Chrome DevTools
More often than not, you might need to fix broken layouts caused by content overflow, as complex user interfaces are now commonplace in frontend development. Modern web browsers come with tools for troubleshooting such layout issues, such as Chrome DevTools.
It provides the capability to select an element in the DOM tree so that you can view, add, and remove CSS declarations and much more. It will help you track down the offending CSS style in your layout and fix it with ease.
To open the Chrome DevTools, you can use the F12
key. When open, it looks like in the image below. Selecting an element in the DOM tree will display its corresponding CSS styles. You can modify the styles and see the effect on your layout as you track down the source of the bug:
As already mentioned, if you have white-space
property on an element, set its value to allow wrapping for overflow-wrap: anywhere
or overflow-wrap: break-word
to work.
Setting the value of overflow-wrap
property to anywhere
or break-word
on a table
content won’t break an overflowing word like in the examples above. The table will overflow its container and create a horizontal scroll if necessary. To get the table to fit within its container and overflow-wrap
to work, set the value of the table-layout
property to fixed
and set the table width to 100%
or to some fixed value.
Conclusion
As pointed out in the above sections, overflow-wrap
and word-break
are similar in so many ways, and you can use both of them for line-breaking controls. The name overflow-wrap
is an alias of the legacy word-wrap
property. Therefore, you can use the two interchangeably. However, it is worth mentioning that the browser support for the newer overflow-wrap
property is still low. You are better off using word-wrap
instead of overflow-wrap
if you want near-universal browser support.
According to the draft CSS3 specification, browsers and user agents should continue supporting word-wrap
for legacy reasons. If you are looking to manage content overflow, overflow-wrap
or its legacy name word-wrap
might be sufficient. You can also use word-break
to break a word between two characters if the word overflows its container. Just like overflow-wrap
, you need to tread with caution when using word-break
because of limitations in the browser support.
Now that you know the behavior associated with the two properties, you can decide where and when to use them. Did I miss anything? Leave a comment in the comments section. I will be happy to update this article.
Is your frontend hogging your users’ CPU?
As web frontends get increasingly complex, resource-greedy features demand more and more from the browser. If you’re interested in monitoring and tracking client-side CPU usage, memory usage, and more for all of your users in production, try LogRocket.https://logrocket.com/signup/
LogRocket is like a DVR for web and mobile apps, recording everything that happens in your web app, mobile app, or website. Instead of guessing why problems happen, you can aggregate and report on key frontend performance metrics, replay user sessions along with application state, log network requests, and automatically surface all errors.
Modernize how you debug web and mobile apps — Start monitoring for free.