Understanding CSS Text Wrapping and Related Properties
This article explains why long English strings may not wrap in browsers, analyzes the default white-space behavior, and provides practical CSS solutions—including word-break, white-space, overflow-wrap, and word-wrap—along with usage recommendations for mixed Chinese‑English content.
Introduction
Hello, I'm Shi Xiaoshi. In many projects, text overflow and line‑break handling is a frequent CSS issue that often leads to bugs.
Recently I received two bug reports where long English strings did not wrap inside a modal, while Chinese characters wrapped correctly.
Problem Analysis
The problematic code did not define any line‑break rules:
.hover-content {
max-width: 420px;
max-height: 420px;
}Therefore the browser applied its default white-space: normal behavior, which prioritises word integrity for English and treats each Chinese character as a break point.
Consequently, continuous English or numeric strings without spaces remain on a single line, while Chinese text wraps naturally.
Solution
A single CSS declaration can fix the issue:
word-break: break-all;The word-break: break-all rule forces line breaks between any characters, including English words and numbers.
Purpose : defines how words are broken.
Values : normal (default): Chinese breaks per character, English per word. break-all : break anywhere. keep-all : Chinese per character, English no break inside words or numbers.
Related CSS Properties
Besides word-break , you may also need to understand white-space and overflow-wrap (formerly word-wrap ).
white-space
The white-space property controls how whitespace inside an element is handled and whether text may wrap. Its default normal allows wrapping.
white-space: nowrap; /* prevent wrapping */
overflow: hidden; /* hide overflow */
text-overflow: ellipsis; /* show ellipsis for overflow */Setting white-space: nowrap disables wrapping for all languages.
overflow-wrap
The overflow-wrap (alias word-wrap ) property determines whether long words may be broken to prevent overflow.
normal : do not break long words.
break-word : allow breaking within words.
anywhere : similar to break-word but with higher priority.
In practice, overflow-wrap: break-word behaves similarly to word-break: break-all , but the latter is more aggressive.
Recommended Practices
Chinese‑dominant content : use word-break: normal .
Mixed Chinese‑English : use overflow-wrap: break-word .
English‑dominant or numeric content : use word-break: break-all when forced wrapping is needed.
Considering complex scenarios, word-break: break-all is a safe fallback.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.