CSS Text Properties Playground
Once you understand individual properties, the real complexity emerges: how do they interact?
Playground
Box Model
Text Behavior
Overflow Handling
Refinements
The quick brown fox jumps over the lazy dog. This is a longer text to demonstrate text overflow, wrapping, and truncation behaviors with various CSS properties. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Common Patterns
Single-line truncation with ellipsis
css
white-space: nowrap; contain: content; text-overflow: ellipsis;
Multi-line truncation (line clamping)
css
display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
Code block with wrapping
css
white-space: pre-wrap; overflow-wrap: break-word;
Tight button text alignment
css
line-height: 1.2; margin-block: calc(0.5cap - 0.5lh); padding: 0.75em 1.5em;
Aggressive wrapping for constrained spaces
css
word-break: break-all; width: 150px;
Hyphenated text
css
hyphens: auto;
Requires lang attribute on the element, e.g. <div lang="en">
Preserving line breaks in user content
css
white-space: pre-line;
Fade-out overflow effect
css
white-space: nowrap; overflow: hidden; mask-image: linear-gradient( to right, black 0%, black calc(100% - 7ch), transparent 100% );
Understanding Property Interactions
white-space vs overflow-wrap
- If
white-space: nowrap, thenoverflow-wraphas no effect (text doesn’t wrap at all) white-spacecontrols whether wrapping happensoverflow-wrapcontrols how wrapping happens when long words appear
text-overflow vs overflow
text-overflow: ellipsisrequiresoverflow: hidden(orauto,scroll,clip)overflow: visibleprevents text-overflow from working
line-clamp vs line-height
- Line clamp counts line boxes, not visual lines
- Changing
line-heightchanges the height of each clamped line
margin-block vs line-height
- The
calc(0.5cap - 0.5lh)formula depends on both cap height and line-height - Larger line-height = larger negative margin (pulls text up more)
Resources
- Text Breaking & Wrapping for white-space and word-break
- How to Vertically Center Text in CSS for text-box-trim and margin-block
- The Many Ways to Deal with Overflowing Text for practical solutions