CSS Overflow & White-space
Control what happens when content exceeds its container and how whitespace is handled.
Overflow Property
overflow: visible (Default)
Content overflow is visible:
.container {
overflow: visible;
}
overflow: hidden
Content is clipped:
.container {
overflow: hidden;
}
overflow: scroll
Always shows scrollbar:
.container {
overflow: scroll;
}
overflow: auto
Scrollbar only when needed:
.container {
overflow: auto;
}
Individual Axes
.container {
overflow-x: hidden;
overflow-y: auto;
}
Text Truncation
Single Line Truncation
.truncate {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
max-width: 200px;
}
Multi-Line Truncation
/* 2 lines */
.truncate-2 {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
/* 3 lines */
.truncate-3 {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
White-space Property
normal (Default)
Text wraps normally:
.white-space-normal {
white-space: normal;
}
nowrap
Text doesn’t wrap:
.white-space-nowrap {
white-space: nowrap;
}
pre
Preserves spaces and line breaks:
.white-space-pre {
white-space: pre;
}
pre-wrap
Preserves spaces but wraps:
.white-space-pre-wrap {
white-space: pre-wrap;
}
pre-line
Preserves line breaks but collapses spaces:
.white-space-pre-line {
white-space: pre-line;
}
break-spaces
Like pre-wrap but breaks at any whitespace:
.white-space-break-spaces {
white-space: break-spaces;
}
Word Break
word-break: normal
Default behavior:
.word-break-normal {
word-break: normal;
}
word-break: break-all
Breaks at any character:
.word-break-all {
word-break: break-all;
}
word-break: keep-word
Breaks at word boundaries:
.word-break-keep {
word-break: keep-word;
}
Overflow Wrap
overflow-wrap: normal
Default behavior:
.overflow-wrap-normal {
overflow-wrap: normal;
}
overflow-wrap: break-word
Breaks long words:
.overflow-wrap-break {
overflow-wrap: break-word;
}
Complete Example: Card with Truncation
.card {
width: 300px;
border: 1px solid #ddd;
border-radius: 10px;
overflow: hidden;
}
.card-image {
width: 100%;
height: 200px;
object-fit: cover;
}
.card-content {
padding: 1.5rem;
}
.card-title {
font-size: 1.25rem;
margin-bottom: 0.5rem;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.card-text {
color: #666;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
margin-bottom: 1rem;
}
.card-footer {
display: flex;
justify-content: space-between;
align-items: center;
}
<div class="card">
<img src="image.jpg" alt="Image" class="card-image" />
<div class="card-content">
<h3 class="card-title">This is a very long title that should be truncated</h3>
<p class="card-text">
This is a longer description that should be truncated after three lines.
It provides more context about the content.
</p>
<div class="card-footer">
<span>Jan 1, 2026</span>
<a href="#">Read more</a>
</div>
</div>
</div>
Complete Example: Scrollable Container
.scroll-container {
width: 300px;
height: 200px;
overflow-y: auto;
border: 1px solid #ddd;
padding: 1rem;
}
.scroll-container::-webkit-scrollbar {
width: 8px;
}
.scroll-container::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 4px;
}
.scroll-container::-webkit-scrollbar-thumb {
background: #888;
border-radius: 4px;
}
.scroll-container::-webkit-scrollbar-thumb:hover {
background: #555;
}
<div class="scroll-container">
<p>Long content that scrolls...</p>
<p>More content...</p>
<p>Even more content...</p>
<p>Keep scrolling...</p>
<p>Almost there...</p>
<p>End of content</p>
</div>
Complete Example: Code Block
.code-block {
background-color: #1e1e1e;
color: #d4d4d4;
padding: 1rem;
border-radius: 5px;
overflow-x: auto;
white-space: pre;
font-family: 'Fira Code', monospace;
font-size: 0.9rem;
line-height: 1.5;
}
.code-block::-webkit-scrollbar {
height: 8px;
}
.code-block::-webkit-scrollbar-track {
background: #2d2d2d;
}
.code-block::-webkit-scrollbar-thumb {
background: #555;
border-radius: 4px;
}
<pre class="code-block">
.container {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem;
}
</pre>
Complete Example: Table with Fixed Headers
.table-container {
width: 100%;
max-height: 400px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 5px;
}
.table {
width: 100%;
border-collapse: collapse;
}
.table thead {
position: sticky;
top: 0;
background-color: #f5f5f5;
}
.table th,
.table td {
padding: 0.75rem;
text-align: left;
border-bottom: 1px solid #ddd;
}
.table th {
font-weight: 600;
background-color: #f5f5f5;
}
.table tbody tr:hover {
background-color: #f9f9f9;
}
Best Practices
- Use text-overflow: ellipsis — Clean truncation
- Use overflow: auto — Only show scrollbar when needed
- Use overflow-wrap: break-word — Prevent long word overflow
- Test with long content — Ensure truncation works
- Provide tooltips — For truncated content
Common Mistakes
- Missing white-space: nowrap — Text-overflow doesn’t work without it
- Missing overflow: hidden — Content overflows instead of truncating
- Using overflow: scroll always — Scrollbar shows even when not needed
- Not testing long content — Truncation may not work as expected
- Ignoring accessibility — Truncated content needs tooltip