CSS white-space: nowrap vs text-wrap: balance
While both CSS properties influence how text breaks across lines,
they serve entirely different typographic purposes.
white-space: nowrap strictly prevents text from wrapping at
all, forcing it to remain on a single horizontal line regardless of
container boundaries. In contrast, text-wrap: balance
permits wrapping across multiple lines but calculates line breaks
algorithmically so that each line has roughly equal width, eliminating
awkward single-word lines known as typographic orphans.
Understanding
white-space: nowrap
The white-space: nowrap rule suppresses all automatic
line breaks caused by whitespace within an element. Text will only break
onto a new line if a literal <br> tag is introduced
or if forced by another property like
overflow-wrap: anywhere.
Key characteristics of white-space: nowrap:
- Single-Line Rendering: The browser collapses whitespace sequences into single spaces and renders all inline text on one continuous line.
- Overflow Risk: If the container width is smaller
than the rendered text, the text will overflow its boundary unless
paired with properties like
overflow: hiddenortext-overflow: ellipsis. - Common Use Cases: Ideal for UI elements that must not break, such as buttons, badges, navigation links, table cells, or currency and date formats.
.badge {
white-space: nowrap;
}Understanding
text-wrap: balance
Introduced in modern CSS (CSS Text Module Level 4),
text-wrap: balance is an aesthetic text-formatting
property. Instead of greedily filling each line until it runs out of
space, the browser balances the number of characters and words across
all lines so they appear visually harmonious and roughly equal in
length.
Key characteristics of text-wrap: balance:
- Multi-Line Distribution: Text still wraps within the container, but the browser balances line lengths to avoid trailing words (orphans).
- Performance Limits: Because balancing lines requires the browser to run iterative layout calculations, it is typically restricted by user agents to blocks containing six lines or fewer.
- Common Use Cases: Ideal for headings, subheadings, pull quotes, and short promotional copy where visual symmetry enhances readability.
h1 {
text-wrap: balance;
}Core Differences
- Line Count:
white-space: nowrapforces the text onto a single line, whereastext-wrap: balanceis designed specifically for multi-line text blocks. - Container Boundaries:
white-space: nowrapignores the container width and allows content to overflow.text-wrap: balancerespects container width and wraps content inside it. - Primary Purpose:
white-space: nowrapis a layout constraint tool used to prevent structural breaking.text-wrap: balanceis a typographic enhancement tool used to improve the visual rhythm of wrapped text.