Tailwind CSS - Text Wrap
Tailwind CSS - Text Wrap
Section titled “Tailwind CSS - Text Wrap”Utilities for controlling how text wraps within an element. These classes provide modern solutions for preventing awkward line breaks and creating more balanced, aesthetically pleasing text blocks.
Text Wrapping Utilities
Section titled “Text Wrapping Utilities”Tailwind provides a set of classes based on the CSS text-wrap property to manage text flow. These are especially powerful for responsive typography.
| Class | CSS Properties | Description |
|---|---|---|
text-wrap | text-wrap: wrap; | Allows text to wrap to the next line as normal (default browser behavior). |
text-nowrap | text-wrap: nowrap; | Prevents text from wrapping, causing it to overflow its container if necessary. |
text-balance | text-wrap: balance; | Balances text across lines to make them roughly equal in length. Best for headings. |
text-pretty | text-wrap: pretty; | Improves typography by preventing a single word (an “orphan”) from ending up on the last line. |
Side-by-Side Comparison
Section titled “Side-by-Side Comparison”The best way to understand these utilities is to see them in action. The example below demonstrates each class on a block of text, highlighting their different effects on layout and readability.
Example: Comparing Text Wrap Utilities
Section titled “Example: Comparing Text Wrap Utilities”<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Text Wrap Comparison</title> <!-- For demonstration, we use the Tailwind Play CDN. In a real project, set up Tailwind CSS with a build step. --> <script src="https://cdn.tailwindcss.com"></script></head><body class="bg-white p-4 sm:p-8 antialiased">
<div class="max-w-6xl mx-auto"> <h1 class="text-3xl font-bold text-center text-slate-900 mb-8">Text Wrap Utilities</h1>
<div class="grid md:grid-cols-2 lg:grid-cols-4 gap-6">
<!-- Text Wrap --> <div class="bg-slate-100 p-4 rounded-lg"> <h2 class="font-mono text-slate-700 mb-2">text-wrap</h2> <p class="text-wrap w-48 text-slate-600"> This is standard text wrapping. Lines break wherever they need to, which can sometimes lead to uneven line lengths. </p> </div>
<!-- Text NoWrap --> <div class="bg-slate-100 p-4 rounded-lg overflow-x-auto"> <h2 class="font-mono text-slate-700 mb-2">text-nowrap</h2> <p class="text-nowrap w-48 text-slate-600"> This text will never wrap to a new line, and will instead overflow its container horizontally. </p> </div>
<!-- Text Balance --> <div class="bg-slate-100 p-4 rounded-lg"> <h2 class="font-mono text-slate-700 mb-2">text-balance</h2> <p class="text-balance w-48 text-slate-600"> This heading is balanced. Notice how the lines are made to be a more similar length for better visual flow. Great for titles. </p> </div>
<!-- Text Pretty --> <div class="bg-slate-100 p-4 rounded-lg"> <h2 class="font-mono text-slate-700 mb-2">text-pretty</h2> <p class="text-pretty w-48 text-slate-600"> This paragraph uses text-pretty to avoid leaving a single word on the final line, like the word 'typography'. </p> </div>
</div> </div>
</body></html>Best Practices and Considerations
Section titled “Best Practices and Considerations”Browser Support
Section titled “Browser Support”text-balance and text-pretty are modern CSS features. While support is growing in major browsers, it is not yet universal. Always test on your target browsers. For unsupported browsers, the text will fall back to the default text-wrap behavior, so it degrades gracefully.
Performance
Section titled “Performance”The browser needs to perform calculations to balance or prettify text. For this reason:
- Use
text-balanceprimarily for headings, titles, and other short blocks of text (typically under 10 lines). Applying it to large paragraphs can impact performance. - Use
text-prettyfor body copy where good typography is desired. It is generally less performance-intensive thantext-balancebut should still be used thoughtfully.
When to Use text-nowrap
Section titled “When to Use text-nowrap”text-nowrap is extremely useful in UI components where line breaks are undesirable, such as in buttons, tags, or table cells where content should stay on a single line.