Sub topic 10
Alignment, wrapping, truncation, and line clamping
This chapter covers the utilities that decide how text sits inside its box and what happens when the text is longer than the box. It assumes you know the HTML class attribute, the CSS box model, and the font size, weight, and leading utilities from earlier chapters. It also assumes a project with Tailwind CSS installed. The line-clamp-* utilities need Tailwind v3.3 or later. Before v3.3 they came from a separate plugin.
Text alignment
Alignment utilities set the text-align property. They affect the inline content of a block, such as text, inline elements, and inline-block elements. They do not move the block itself.
| Class | CSS |
|---|---|
| `text-left` | `text-align: left` |
| `text-center` | `text-align: center` |
| `text-right` | `text-align: right` |
| `text-justify` | `text-align: justify` |
| `text-start` | `text-align: start` |
| `text-end` | `text-align: end` |
text-start and text-end follow the writing direction. In a left-to-right language, text-start is the same as text-left. In a right-to-left language such as Arabic, it aligns to the right. Prefer them when your page may be rendered in both directions.
<div class="max-w-prose space-y-2">
<p class="text-left">Left aligned (the default in left-to-right text)</p>
<p class="text-center">Centered</p>
<p class="text-right">Right aligned</p>
<p class="text-justify">
Justified text stretches each line to fill the width, which can leave
uneven gaps between words in narrow columns.
</p>
</div>text-justify often produces wide gaps between words in narrow columns. Use it only in wide columns, and consider adding hyphens-auto.
Text alignment versus block alignment
A common mistake is using text-center to center a box. It centers the text inside the box and leaves the box where it is. To center the box itself, give it a constrained width and mx-auto.
<div class="max-w-md mx-auto text-center">
The box is centered by mx-auto. The text inside is centered by text-center.
</div>You need both classes here. Without max-w-md, the block fills its parent, so mx-auto has no free space to split.
Text wrapping
By default, text wraps at spaces and hyphens. Two groups of utilities change that.
Whitespace control
The whitespace-* utilities set the white-space property.
| Class | Collapses spaces | Honors line breaks in source | Wraps at the container edge |
|---|---|---|---|
| `whitespace-normal` | Yes | No | Yes |
| `whitespace-nowrap` | Yes | No | No |
| `whitespace-pre` | No | Yes | No |
| `whitespace-pre-line` | Yes | Yes | Yes |
| `whitespace-pre-wrap` | No | Yes | Yes |
Use whitespace-nowrap for labels that must stay on one line, such as a price or a button caption. Use whitespace-pre-line for user-entered text where newlines should show up but extra spaces do not matter.
Balanced wrapping
text-balance sets text-wrap: balance. The browser tries to make each line about the same length, which avoids a single orphaned word on the last line of a heading. text-pretty sets text-wrap: pretty, which targets body text and avoids short last lines. Both utilities need Tailwind v3.4 or later. Browsers that do not support the property ignore it, and the text wraps normally. Browsers limit balancing to a small number of lines, so it suits headings and short paragraphs.
<h1 class="max-w-xl text-3xl font-bold text-balance">
A heading that the browser balances so no single word is left alone
</h1>Breaking long words
A long unbroken string, such as a URL or an email address, can overflow its container. Two utilities handle it.
break-wordssetsoverflow-wrap: break-word. It breaks a long word only when the word would overflow, and it keeps normal wrapping elsewhere.break-allsetsword-break: break-all. It allows breaks between any two characters, even when the word would fit on the next line.
Prefer break-words for most content. Reserve break-all for cases like hash strings, where a ragged edge does not matter.
<p class="w-48 break-words">
https://example.com/a/very/long/path/that/does/not/contain/any/spaces
</p>A flexbox gotcha
break-words often seems to do nothing inside a flex item. The reason is that a flex item has min-width: auto, so it will not shrink below the width of its longest word. The browser never sees an overflow to break. Add min-w-0 to the flex item so it can shrink.
<div class="flex gap-4">
<img class="size-10 shrink-0" src="avatar.png" alt="" />
<p class="min-w-0 break-words">
averyveryverylongusernamewithoutanyspacesthatwouldotherwiseoverflow
</p>
</div>The same min-w-0 fix applies to truncation, covered next.
Truncation
truncate is a shortcut for three declarations.
.truncate {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}The text stays on one line, anything past the edge is clipped, and an ellipsis marks the cut. The element needs a width limit for this to work. A block element gets one from its parent. A flex item needs min-w-0, for the same reason as above.
<div class="flex w-64 items-center gap-3">
<img class="size-8 shrink-0 rounded-full" src="avatar.png" alt="" />
<p class="min-w-0 truncate">
A very long file name that will not fit in the available space.pdf
</p>
</div>Two related utilities set only the text-overflow part. text-ellipsis sets text-overflow: ellipsis and text-clip sets text-overflow: clip. They have no effect unless the element also has overflow-hidden and a rule that stops wrapping, such as whitespace-nowrap. Use truncate unless you need to mix the pieces.
<p class="w-40 overflow-hidden whitespace-nowrap text-clip">
This text is cut off with no ellipsis at the edge.
</p>Line clamping
truncate handles one line. To cut text after several lines, use line-clamp-*.
<p class="w-64 line-clamp-3">
Long text that is cut after three lines with an ellipsis. The rest of the
paragraph is still in the page, but the browser hides it and draws an
ellipsis at the end of the third line.
</p>Under the hood, line-clamp-3 expands to four declarations.
.line-clamp-3 {
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
}The -webkit- prefixes are still how browsers implement this behavior. All current major browsers support it.
The available classes are line-clamp-1 through line-clamp-6, line-clamp-none, and line-clamp-[n] for other counts. Use line-clamp-none to undo a clamp at a larger breakpoint, as in line-clamp-3 md:line-clamp-none. That pattern is common for cards that show a preview on phones and full text on desktops.
<p class="line-clamp-3 md:line-clamp-none">
Preview on small screens, full text from the md breakpoint upward.
</p>Gotchas with line clamping
line-clamp-*setsdisplay: -webkit-box. If you addflex,block, orgridto the same element, the display value changes and the clamp stops working.- Padding on a clamped element can show extra lines of text below the visible area, because
overflow: hiddenclips at the padding edge. Put the padding on a wrapper instead. - The hidden text stays in the DOM. Screen readers and search engines still see it. Do not use clamping to hide content that must stay private.
- Clamping counts lines, not characters. The number of visible characters changes with width and font size.
Choosing between the tools
| Goal | Utility |
|---|---|
| One line, cut with an ellipsis | `truncate` |
| Several lines, cut with an ellipsis | `line-clamp-*` |
| Keep text on one line without cutting | `whitespace-nowrap` |
| Keep text visible but break a long word | `break-words` |
| Avoid an orphan word in a heading | `text-balance` |
| Preserve newlines from the source | `whitespace-pre-line` or `whitespace-pre-wrap` |
Decision flow
The diagram below shows how to pick a utility when text is too long for its box.
Responsive variants
Every utility in this chapter accepts breakpoint variants. A card can center its text on small screens and left-align it on larger ones, and it can clamp the summary only on small screens.
<div class="mx-auto max-w-md p-4 text-center md:text-left">
<h2 class="text-xl font-semibold text-balance">Card title</h2>
<p class="mt-2 line-clamp-2 text-gray-600 md:line-clamp-4">
Summary text that shows two lines on small screens and four lines from
the md breakpoint upward. Anything beyond that is replaced by an ellipsis.
</p>
</div>Unprefixed classes apply at every width. The md: classes override them from the breakpoint up.
Common errors
text-centerdoes not center the box. It centers the content. Usemx-autowith a width limit.truncateshows no ellipsis. The element has no width limit. In a flex row, addmin-w-0. In a grid, the same applies to the grid item.text-ellipsisalone does nothing. It needsoverflow-hiddenandwhitespace-nowrap.line-clamp-*stops working after addingflex. Thedisplayvalue was overridden.- Mixing
break-allinto body copy. Words split in the middle even when they would fit on the next line. Usebreak-words.
Exercise
A card has a flex row with an avatar and a title. The title is a long file name with no spaces. You added truncate to the title <p>, but the row grows wider than the card and no ellipsis appears. What do you add to the <p>, and why? Also, how do you show the title on up to two lines instead?
Show answer
Add min-w-0. A flex item has min-width: auto, so it will not shrink below its content width, and truncate never sees an overflow to clip. With min-w-0 the item can shrink to the available space and the ellipsis appears. To show two lines, replace truncate with line-clamp-2 and keep min-w-0. If the file name has no spaces, also add break-all or break-words so that the long word can break across lines.
Exercise
Write the classes for a paragraph that is centered on small screens, left-aligned from the md breakpoint, clamped to 3 lines on small screens, and unclamped from md upward.
Show answer
text-center md:text-left line-clamp-3 md:line-clamp-none