π Introduction
The text-overflow property in CSS controls how overflowing text is displayed when it doesnβt fit inside its container. Itβs commonly used in UI design, especially for cards, tables, labels, and mobile layouts where space is limited. This property helps you keep your design clean and readable. β¨
π― Why Use text-overflow?
- To show ellipsis (β¦) when text is too long π€
- To hide overflowing text safely π
- To keep UI layouts from breaking π±
- To handle long titles, descriptions, or filenames π§Ύ
β¨ Syntax
Basic Syntax
selector {
text-overflow: clip | ellipsis | "<string>";
}Note
text-overflow only works when:β’ overflow: hidden (or scroll) is applied, AND β’ white-space: nowrap is used.
π§© Available Values
1. clip βοΈ (default)
Cuts off the text at the edge of the container without any visible indicator.
clip Example
p {
white-space: nowrap;
overflow: hidden;
text-overflow: clip;
}2. ellipsis (β¦) β
Replaces clipped text with an ellipsis. This is the most popular option for UI layouts.
ellipsis Example
p {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}3. custom string π
You can replace the ellipsis with any string you like.
Custom Overflow Text
p {
white-space: nowrap;
overflow: hidden;
text-overflow: ">>>"; /* custom string */
}π₯ Practical Examples
1. Truncate a Long Title
Long Title Truncation
.title {
width: 200px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}2. Overflow Inside Flex or Grid Layouts
Inside Flexbox
.item {
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}3. Custom Decorative Overflow
Decorative Example
.fancy {
max-width: 180px;
white-space: nowrap;
overflow: hidden;
text-overflow: ">>>"; /* stylish custom string */
}π Comparison Table
| Value | Effect |
|---|---|
| clip | Cuts off text abruptly |
| ellipsis | Adds "β¦" for trimmed content |
| "string" | Replaces overflow with custom characters |
πΌοΈ Visual Example
Further details can be found at: https://dummyimage.com/900x240/eeeeee/000000&text=text-overflow+clip+|+ellipsis
π Conclusion
The text-overflow property is essential for clean, responsive, and user-friendly layouts. Whether you're designing dashboards, mobile apps, product cards, or tables, it helps maintain structure while improving readability. Pair it with white-space and overflow for perfect results. π