Sometimes text is too long to fit in a small space. We use text overflow and truncation to cut the text nicely and show it with dots.
Text overflow and truncation in Tailwind
Start learning this pattern below
Jump into concepts and practice - no test required
class="truncate"The truncate class in Tailwind applies overflow: hidden;, white-space: nowrap;, and text-overflow: ellipsis; to cut off text with dots.
Use it on block or inline-block elements with a fixed width or max-width.
<div class="w-48 truncate">This is a very long text that will be cut off with dots.</div><p class="max-w-xs truncate">Another example with max width and truncation.</p><span class="inline-block w-32 truncate">Short text that fits.</span>This page shows three boxes with different widths. The first two have long text that gets cut off with dots. The last one has short text that fits fully.
We use Tailwind's truncate class and fixed widths to control the text overflow.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Text Overflow and Truncation</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <h1 class="text-xl font-bold mb-4">Text Overflow and Truncation Example</h1> <div class="w-48 border border-gray-400 p-2 mb-4 truncate" aria-label="Truncated text example"> This is a very long text that will not fit in the box and will be truncated with dots at the end. </div> <p class="max-w-xs border border-blue-400 p-2 truncate" aria-label="Another truncated text"> Another example with a max width set and text that is too long to fit inside. </p> <span class="inline-block w-32 border border-green-400 p-2 truncate" aria-label="Short text example"> Short text fits. </span> </body> </html>
Make sure the container has a fixed width or max-width for truncation to work.
Use aria-label to describe truncated text for screen readers.
Truncation only works on single-line text. For multi-line truncation, other techniques are needed.
Use Tailwind's truncate class to cut off long text with dots.
Set a fixed width or max-width on the container for truncation to work.
Always consider accessibility by labeling truncated text.
Practice
truncate do to text inside an element?Solution
Step 1: Understand the purpose of
Thetruncatetruncateclass in Tailwind applies CSS rules to hide overflow text and show an ellipsis.Step 2: Recognize the visual effect
This means long text that doesn't fit the container ends with dots (…) instead of wrapping or overflowing.Final Answer:
It cuts off overflowing text and adds an ellipsis (…) at the end. -> Option BQuick Check:
truncate = text cut off with dots [OK]
- Thinking truncate changes text style like bold or color
- Confusing truncate with text alignment classes
- Assuming truncate wraps text instead of cutting it
Solution
Step 1: Identify required classes for truncation
Truncation needstruncateplus a fixed width likew-48to limit container size.Step 2: Check each option
truncate w-48usestruncateand a fixed width, so it works. Others either lack fixed width or have conflicting overflow.Final Answer:
<code>truncate w-48</code> -> Option DQuick Check:
truncate + fixed width = truncation works [OK]
- Using truncate without fixed width or max-width
- Setting overflow-visible which disables truncation
- Confusing text alignment with truncation
<div class="w-32 truncate">This is a very long text that should be cut off.</div>
What will the user see in the browser?
Solution
Step 1: Analyze container width and truncate class
The container has fixed widthw-32andtruncatewhich cuts off overflow text with ellipsis.Step 2: Predict visible text
Text longer than container width is cut and ends with (…) showing partial text.Final Answer:
"This is a very long text that shoul…" with an ellipsis at the end -> Option AQuick Check:
Fixed width + truncate = partial text + dots [OK]
- Expecting full text to show without wrapping
- Thinking text wraps instead of truncates
- Assuming empty box means truncation
Solution
Step 1: Understand truncation requirements
Truncation needs a fixed width or max-width to limit container size; otherwise text won't cut off.Step 2: Identify common mistakes
Missing width means text expands fully. Options B, C, D do not affect truncation as directly.Final Answer:
Missing a fixed width or max-width class on the container -> Option AQuick Check:
Width needed for truncate to work [OK]
- Forgetting to set container width or max-width
- Adding overflow-visible which disables truncation
- Confusing text alignment with truncation
Solution
Step 1: Understand accessibility needs
Screen readers need full text, so usearia-labelwith full title on truncated element.Step 2: Evaluate options for best practice
Usetruncateon the title and addaria-labelwith full text on the same element usestruncatevisually andaria-labelfor accessibility, which is correct. Usetruncateand add a tooltip with the full text usingtitleattribute is helpful but less reliable for screen readers.Final Answer:
Use <code>truncate</code> on the title and add <code>aria-label</code> with full text on the same element -> Option CQuick Check:
Truncate + aria-label = visible dots + accessible full text [OK]
- Hiding full text with display:none removes it from screen readers
- Using deprecated tags like marquee
- Relying only on title attribute for accessibility
