Nope, I'm not writing a rap song on CSS π€.
But something better π.
A cheat sheet on various ways of text wrapping and overflow handling in CSS.
π
I know what you're thinking! "I know how to handle text wrapping and overflow in CSS. I've been doing them since forever." But believe me, there are some new properties and some major depreciations ongoing. Wouldn't hurt to spend 3min reading.
So without further adieu.
Text overflow handling in CSS is done using mostly these properties.
Quite a handful of options right! Wondering all properties and values to use?
Lucky for you, here's a list of the most common text-overflow handling scenarios and their CSS code snippets. π
If you would like to read about text-overflow in general and what many of the above properties do, I highly recommend you read this MDN blogpost.
case 1
overflow-wrap: break-word;
This property will move the last word in the line to the next line if it is too long to fit on the same line by itself without overflowing.
Downsides:
π But the downside is, you might get some empty spaces if the next word does not fit in the line.
Upsides:
π Good enough when container size is not small (>500px).
π Good browser support.
case 2
text-align: justify;
overflow-wrap: break-word;
Let's say you like case-1 but don't want those empty spaces at the line end. One solution is to justify the line text so that the end spaces are distributed throughout the line.
Downsides:
π Spaces between words might look uneven. Much evident when container size is small (<300px).
π justify only works if there are at least two words in the line.
Upsides:
π Good enough when container size is medium or larger (>300px).
π Good browser support.
case 3
text-align: justify;
word-break: break-all;
If you have a small container and want the whole line to be filled with letters and at the end of the line break on any word/letter. Then use this.
Downsides:
π if used on large container widths, can make reading the line's harder as the last words as split up in between.
Upsides:
π Best for small container widths.
π The whole container looks filled from side to side per line. (some designs might need that.)
π Good browser support.
case 4
hyphens: auto;
if you wanna a mix of
overflow-wrap: break-word;
andword-break: break-all;
, I would say use this. Withhyphens:auto
, the browser is free to automatically break words at appropriate hyphenation points, following whatever rules it chooses.
Downsides:
π Word/letter breaking not predictable.
π Needs vendor prfixes to work with all browsers
Upsides:
π Adds a hyphen(-) when breaking words in between. Makes readability slightly better.
π hyphens
is language sensitive based on lang
attribute.
Before we go to the next case, two bonus tips .
-
word-break: break-word;
(just the break-word value) has been deprecated. Useoverflow-wrap: break-word;
instead. -
word-wrap:
has also been deprecated as a property. Useoverflow-wrap
instead.
case 5
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
Use this when you don't wanna show the overflowing text. 3 dots(...) is shown to convey that there is text being overflown and being hidden.
case 5
Multiline ellipsis
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow:hidden;
Downsides:
π Not so good browser support
π You can't have padding on the text container as it will expose extra lines. π€·ββοΈ
That's it. Those were the most common text wrapping and overflow handling cases I could think of.
If you liked what you read, and wanna see what I'm lately up to, follow me on Twitter.
Top comments (0)