CSS's word-break and word-wrap (also known as overflow-wrap) are properties that control text wrapping behavior. They serve different purposes and are used to manage text overflow within containers.
word-break
The word-break property is primarily used to specify how line breaks occur within words.
normal: Follows the default line break rules.break-all: Allows line breaks at any position within a word. It applies to multi-byte scripts (such as Chinese, Japanese, and Korean) and also affects non-multi-byte characters (like English), potentially breaking words in the middle.keep-all: For CJK (Chinese, Japanese, Korean) scripts, it prevents line breaks within words; for non-CJK scripts, it behaves likenormal.
Example:
css.word-break-example { word-break: break-all; }
In this example, if a long English word or string without spaces exceeds the container width, it will wrap at any point within the container boundary, disregarding word boundaries.
word-wrap / overflow-wrap
The word-wrap property (renamed to overflow-wrap in CSS3) specifies whether a word that is too long to fit within its container should be broken and wrapped to the next line.
normal: Words are not broken; line breaks occur only at permitted break points.break-word: Breaks long words or URLs within the word to prevent text from overflowing its containing element.
Example:
css.word-wrap-example { word-wrap: break-word; }
Alternatively, using overflow-wrap:
css.overflow-wrap-example { overflow-wrap: break-word; }
In both examples, if the text contains a long word, it will be broken at the container edge and continue on the next line, preventing overflow.
Summary
The key difference lies in how they handle line breaks within words:
- When using
word-break: break-all;, long words and sequences of non-whitespace characters break at any character position to prevent overflow. - When using
word-wrap: break-word;(oroverflow-wrap: break-word;), text breaks within words only when necessary, prioritizing word integrity while preventing overflow.
In practice, word-wrap: break-word; is generally regarded as more elegant because it preserves word integrity as much as possible. However, word-break: break-all; may break words at undesirable positions, potentially impacting readability. For certain languages (such as Chinese), the effects may be similar since Chinese does not rely on spaces to separate words.