31. Overflow, Text Truncation, Lists & Table Styling
🟢 Beginner
1. Definition
overflow (visible, hidden, scroll, auto) controls box clipping. Text truncation (text-overflow: ellipsis) truncates long strings. list-style: none removes bullet markers, and border-collapse: collapse creates clean data tables.
2. Why It Is Used
Overflow properties prevent content boxes from breaking page layouts when user-generated text exceeds container bounds.
3. How It Works
Combining white-space: nowrap; overflow: hidden; text-overflow: ellipsis; truncates overflowing single-line text with an ellipsis (...).
4. Syntax
.truncate-text {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.table-clean {
border-collapse: collapse;
width: 100%;
}
5. Overflow Values Reference Table
| Value | Behavior |
|---|---|
visible | Default; content renders outside container box |
hidden | Clips overflowing content without scrollbars |
scroll | Always displays scrollbars regardless of overflow |
auto | Displays scrollbars ONLY when content overflows |
6. Beginner Example
.scroll-box {
max-height: 200px;
overflow-y: auto;
}
7. Intermediate Example
.clean-table {
width: 100%;
border-collapse: collapse;
}
.clean-table th, .clean-table td {
padding: 12px;
border-bottom: 1px solid #e2e8f0;
}
8. Advanced Multi-line Text Truncation Example
.truncate-3-lines {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
9. Line-by-Line Code Explanation
-webkit-line-clamp: 3;: Truncates paragraph text after exactly 3 lines, adding trailing ellipsis (...).
10. Expected Terminal / Browser Output
Clean single-line or multi-line text cards and striped data tables without broken layout boxes.
11. Common Mistakes & Pitfalls
- Using
overflow: scrollinstead ofauto:scrollforces permanent scrollbars even when content fits inside the container. Useauto.
12. Important Rules & Limitations
- Single-line
text-overflow: ellipsisrequires settingoverflow: hiddenandwhite-space: nowrap.
13. Multilingual Explanation: English
overflow: auto adds scrollbars when content overflows. Use text-overflow: ellipsis for text truncation.
14. Multilingual Explanation: Hindi (Roman Script)
Content box se bahar jaane par overflow: auto scrollbar deta hai. Text truncate karne ke liye text-overflow: ellipsis ka use karein.
15. Multilingual Explanation: Marathi (Roman Script)
Overflow content sathi overflow: auto vapara ani extra text truncate karnyasathi text-overflow: ellipsis vapara.
16. Practice Questions
- What three CSS properties are required for single-line text truncation with an ellipsis?
17. Practical Coding Challenge
Create a .card-description class that truncates text to 2 lines using -webkit-line-clamp: 2.