Sub topic 10
Alignment, wrapping, truncation, and line clamping
This chapter assumes you know basic HTML and CSS, can read Tailwind class lists, and have a project with Tailwind installed. It covers the utilities that decide how text sits in its box and what happens when it does not fit.
Text alignment
The text-left, text-center, text-right, and text-justify utilities set text-align. They align the lines of text inside a block. They do not move the block itself. To center a block, use mx-auto with a constrained width.
<p class="text-center">Centered lines</p>
<p class="text-right">Right-aligned lines</p>
<p class="text-justify">Justified text stretches each line to fill the width.</p>text-start and text-end follow the writing direction, so they flip automatically in right-to-left languages.
Wrapping and overflow
By default, the browser breaks lines at spaces. A long word or URL with no spaces will overflow its container. These utilities change that behavior.
| Class | Effect |
|---|---|
| `whitespace-nowrap` | Keeps the text on one line |
| `break-words` | Breaks a long word only if it would overflow |
| `break-all` | Breaks anywhere to fill each line |
| `text-balance` | Evens out line lengths, good for headings |
| `text-pretty` | Avoids a single orphaned word on the last line |
text-balance and text-pretty need Tailwind v3.4 or later. break-words is the usual fix for user-generated content such as long links.
<p class="max-w-xs break-words">
https://example.com/a/very/long/path/with/no/spaces/that/would/overflow
</p>
<h2 class="text-balance text-2xl font-semibold">
A heading that wraps into evenly sized lines
</h2>Truncation
truncate cuts a single line of text with an ellipsis. It is shorthand for three declarations: overflow: hidden, text-overflow: ellipsis, and white-space: nowrap. The element needs a limited width, or the text never overflows.
<p class="w-48 truncate">This title is too long to fit in twelve rem.</p>Inside a flex row, a truncating child also needs min-w-0. Flex items default to min-width: auto, so they refuse to shrink below their content size and truncation never starts.
<div class="flex w-64 gap-2">
<span class="min-w-0 truncate">A long file name that should be shortened.pdf</span>
<span class="shrink-0">2 MB</span>
</div>Line clamping
line-clamp-3 truncates text after three lines and adds an ellipsis. Use it for multi-line previews. It requires Tailwind v3.3 or later. Use line-clamp-none to remove the limit, for example at a larger breakpoint.
<p class="line-clamp-3">
Long text that is cut after three lines with an ellipsis. Add enough words
here so that the paragraph actually wraps past the third line in a narrow
container.
</p>
<p class="line-clamp-2 md:line-clamp-none">
Clamped to two lines on small screens, shown in full from md upward.
</p>Choosing between them
Common errors
truncatehas no effect. The element has no width limit, or it is a flex child missingmin-w-0.text-centerdoes not center a box. It only aligns the text inside it. Usemx-autofor the box.line-clamp-*on an inline element does nothing. Put it on a block element such aspordiv.break-allsplits ordinary words in the middle. Preferbreak-wordsunless you need that.
Exercise
A flex row holds a file name and a size label. The file name has truncate but still pushes the row wider instead of showing an ellipsis. Which classes fix it?
Show answer
Add min-w-0 to the file name so the flex item can shrink below its content width. Optionally add shrink-0 to the size label so only the name shrinks.
Exercise
Write the classes that show a paragraph in full on medium screens and wider, but cut it to two lines with an ellipsis below that.
Show answer
line-clamp-2 md:line-clamp-none