Question
Why does text-overflow: ellipsis not truncate the text in this link? The link should be limited to about 140 pixels wide and display an ellipsis after roughly the fourth occurrence of “Test”.
.app a {
height: 18px;
width: 140px;
padding: 0;
overflow: hidden;
position: relative;
margin: 0 5px;
text-align: center;
text-decoration: none;
text-overflow: ellipsis;
white-space: nowrap;
color: #000;
}
<div class="app">
<a href="">Test Test Test Test Test Test</a>
</div>
Short Answer
You will learn the conditions required for CSS ellipsis truncation and why it commonly fails on links. In particular, you will see how an element’s display type affects whether its width, height, and overflow rules can take effect.
Concept
text-overflow: ellipsis displays ... when text cannot fit inside an element’s available inline space. It does not make an element narrower by itself; the browser must first have a constrained box from which text can overflow.
For the usual one-line ellipsis pattern, these rules work together:
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
The element also needs a usable width or another layout constraint.
The key issue with the original CSS is that an <a> element is display: inline by default. Normal inline elements do not use width and height in the way block-level boxes do. Since the link is allowed to grow to fit all of its text, there is no horizontal overflow to truncate.
Set the link to inline-block or block so its width: 140px applies. Then the extra text can overflow, be clipped, and be represented with an ellipsis.
Mental Model
Imagine text overflow as a long ribbon inside a fixed-width tray:
widthdefines the tray’s size.white-space: nowrapkeeps the ribbon on one line instead of folding it.overflow: hiddenhides the part hanging outside the tray.text-overflow: ellipsisputs...at the visible end when text is hidden.
An inline link is not acting like a fixed-width tray. It expands with the ribbon. Changing it to inline-block or block gives it a real tray with a measurable width.
Syntax and Examples
Use inline-block when a link should remain inline with surrounding content but still accept a width. Use block when it should occupy its own line or fill a layout area.
.app a {
display: inline-block;
width: 140px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
text-align: center;
text-decoration: none;
color: #000;
}
<div class="app">
<a href="/details">Test Test Test Test Test Test</a>
</div>
The link is now a 140-pixel-wide box. Because its text stays on one line and cannot all fit, the browser hides the excess and displays an ellipsis.
If the link should fill the width of its parent, use a block link instead:
.app a {
display: block;
width: 100%;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Step by Step Execution
Consider this CSS:
.app a {
display: inline-block;
width: 140px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
And this HTML:
<a href="/details">Test Test Test Test Test Test</a>
- The browser creates an anchor element containing the text.
display: inline-blockmakes the anchor participate inline while also creating a box that honorswidth.width: 140pxlimits that box to 140 pixels.white-space: nowrapprevents the browser from moving later words onto a second line.- The text is wider than 140 pixels, so it overflows horizontally.
overflow: hiddenclips the text outside the box.text-overflow: ellipsisreplaces the visible end of the clipped line with an ellipsis when the browser can do so.
The exact word where truncation occurs depends on the active font, font size, letter spacing, and browser rendering. CSS limits pixel width, not a specific number of words.
Real World Use Cases
Text ellipsis is useful whenever a UI has limited space but labels may vary in length:
- File names in a file manager, such as
quarterly-report-final-revised.pdf. - Product names in fixed-width catalog cards.
- Navigation links in a narrow sidebar.
- Usernames in chat headers.
- Table cells containing IDs, URLs, or long titles.
- Buttons whose labels must stay on one line.
For important content, consider exposing the full value through a tooltip, title attribute, expandable detail view, or accessible label. Truncation improves layout, but it should not make essential information impossible to access.
Real Codebase Usage
In production CSS, developers often create a reusable utility class rather than repeating the same four declarations:
.truncate {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
The component or layout then supplies the width constraint:
.file-name {
display: block;
max-width: 18rem;
}
<a class="file-name truncate" href="/files/42">
annual-financial-summary-and-forecast.pdf
</a>
In Flexbox layouts, another common issue is that a flex item refuses to shrink enough for overflow to occur. Add min-width: 0 to the flex item that contains the truncated text:
.row {
display: flex;
gap: 0.5rem;
}
.row__title {
min-width: 0;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
This lets the title shrink within the row instead of forcing the whole row wider.
Common Mistakes
Leaving a link as an inline element
This often fails because width does not constrain a normal inline element:
/* Problematic for a fixed-width link */
a {
width: 140px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Fix it with display: inline-block or display: block.
a {
display: inline-block;
width: 140px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Forgetting overflow: hidden
text-overflow does not clip content by itself.
/* Ellipsis will not reliably appear */
.label {
width: 140px;
white-space: nowrap;
text-overflow: ellipsis;
}
Add overflow: hidden.
Allowing text to wrap
Without white-space: nowrap, text can wrap to another line instead of overflowing horizontally.
.label {
display: block;
width: 140px;
overflow: hidden;
text-overflow: ellipsis;
/* missing white-space: nowrap */
}
Expecting a particular word to be cut off
A width of 140px does not mean “show four words.” Fonts and text metrics determine what fits. Use a width requirement, not a word-count requirement.
Using position: relative as a fix
position: relative is not required for standard ellipsis behavior. Keep it only if you need it for positioning child elements.
Missing Flexbox shrinking behavior
If ellipsis fails inside a flex row even with the usual rules, try min-width: 0 on the flex item that contains the text.
Comparisons
| Approach | Best use | Important behavior |
|---|---|---|
display: inline | Short text within a sentence | Usually ignores specified width and height. |
display: inline-block | A fixed-size link beside other inline content | Honors width while still flowing inline. |
display: block | Links that should form their own row or fill a container | Honors width and typically starts on a new line. |
width: 140px | Exact, fixed visual width | Consistent size, but may not adapt to small screens. |
max-width: 140px | Text that may be shorter than the limit | Can shrink to content width up to the limit. |
overflow: hidden | Hide excess content and enable ellipsis | Extra text is clipped. |
overflow: visible | Let content extend outside its box | Ellipsis cannot represent clipped overflow. |
For multi-line truncation, the single-line white-space: nowrap pattern is not enough. Multi-line clamping uses a different technique, commonly -webkit-line-clamp, and has different browser and layout considerations.
Cheat Sheet
/* Standard single-line truncation */
.truncate {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
/* Fixed-width anchor */
a {
display: inline-block; /* or block */
width: 140px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Rules to remember:
- The text must have a constrained available width.
- For a normal
<a>, adddisplay: inline-blockordisplay: blockbefore relying onwidth. - Use
white-space: nowrapfor one-line ellipsis. - Use
overflow: hidden;text-overflowalone is not enough. text-overflow: ellipsisapplies to overflowing inline content, not arbitrary overflowing elements.- In Flexbox,
min-width: 0may be necessary on the shrinking item. - The browser truncates by rendered width, not by character or word count.
FAQ
Why is text-overflow: ellipsis not working on my anchor tag?
Anchors are inline by default, so a declared width usually does not constrain them. Set display: inline-block or display: block, then apply the truncation rules.
Do I need all three properties for a one-line ellipsis?
Usually yes: overflow: hidden, white-space: nowrap, and text-overflow: ellipsis. You also need a constrained width.
Does text-overflow: ellipsis work without a width?
It can work when another layout rule constrains available space, such as a grid column or a shrinking flex item. A fixed width or max-width is the simplest approach.
Why does the truncation point change when I change the font?
Ellipsis uses rendered pixel width. Different fonts, sizes, weights, and letter spacing change how much text fits.
Can I show an ellipsis after exactly four words?
Not with standard CSS ellipsis alone. CSS truncates based on visual space. Exact word-based truncation requires changing the text in JavaScript or preparing a shortened value.
Why does ellipsis fail inside a flex container?
A flex item may have a default minimum size based on its content. Add min-width: 0 to the item that must shrink, then apply the usual ellipsis rules.
Is position: relative required for ellipsis?
No. It does not enable truncation and can be removed unless it is needed for another positioning task.
Mini Project
Description
Build a compact list of document links. Each row has an icon and a filename that must stay within the available horizontal space. Long filenames should display an ellipsis rather than stretching the row or wrapping to a second line.
Goal
Create a responsive file list where long link text truncates correctly, including inside a Flexbox row.
Requirements
Use a link for each filename. Keep every filename on one line. Show an ellipsis when a filename is wider than its available space. Use Flexbox for the icon-and-filename row. Ensure the filename can shrink in the flex layout.
Keep learning
Related questions
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
Apply CSS to an iframe: Same-Origin Styling in HTML
Learn why parent-page CSS does not style iframe content, how same-origin rules work, and practical ways to style embedded pages safely.
Apply Multiple CSS Transforms: rotate() and translate()
Learn how to combine multiple CSS transform functions such as rotate() and translate(), understand transform order, and avoid overwritten rules.