~/
hackweb.dev
CSS Overflow & White-space
Quiz
⌘K
...
~/
/tutorials
/css/css-overflow/edit
~ Contribute
Suggest a correction or improvement. The author reviews it before it goes live.
Loading...
Comment
0 / 300
Typo
Grammar
Broken link
Clarify
Code
en/tutorials/css/15css-overflow
Write
Preview
Diff
# 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: ```css .container { overflow: visible; } ``` ### overflow: hidden Content is clipped: ```css .container { overflow: hidden; } ``` ### overflow: scroll Always shows scrollbar: ```css .container { overflow: scroll; } ``` ### overflow: auto Scrollbar only when needed: ```css .container { overflow: auto; } ``` ### Individual Axes ```css .container { overflow-x: hidden; overflow-y: auto; } ``` ## Text Truncation ### Single Line Truncation ```css .truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 200px; } ``` ### Multi-Line Truncation ```css /* 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: ```css .white-space-normal { white-space: normal; } ``` ### nowrap Text doesn't wrap: ```css .white-space-nowrap { white-space: nowrap; } ``` ### pre Preserves spaces and line breaks: ```css .white-space-pre { white-space: pre; } ``` ### pre-wrap Preserves spaces but wraps: ```css .white-space-pre-wrap { white-space: pre-wrap; } ``` ### pre-line Preserves line breaks but collapses spaces: ```css .white-space-pre-line { white-space: pre-line; } ``` ### break-spaces Like pre-wrap but breaks at any whitespace: ```css .white-space-break-spaces { white-space: break-spaces; } ``` ## Word Break ### word-break: normal Default behavior: ```css .word-break-normal { word-break: normal; } ``` ### word-break: break-all Breaks at any character: ```css .word-break-all { word-break: break-all; } ``` ### word-break: keep-word Breaks at word boundaries: ```css .word-break-keep { word-break: keep-word; } ``` ## Overflow Wrap ### overflow-wrap: normal Default behavior: ```css .overflow-wrap-normal { overflow-wrap: normal; } ``` ### overflow-wrap: break-word Breaks long words: ```css .overflow-wrap-break { overflow-wrap: break-word; } ``` ## Complete Example: Card with Truncation ```css .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; } ``` ```html <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 ```css .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; } ``` ```html <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 ```css .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; } ``` ```html <pre class="code-block"> .container { display: flex; justify-content: space-between; align-items: center; padding: 1rem; } </pre> ``` ## Complete Example: Table with Fixed Headers ```css .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 1. **Use text-overflow: ellipsis** — Clean truncation 2. **Use overflow: auto** — Only show scrollbar when needed 3. **Use overflow-wrap: break-word** — Prevent long word overflow 4. **Test with long content** — Ensure truncation works 5. **Provide tooltips** — For truncated content ## Common Mistakes 1. **Missing white-space: nowrap** — Text-overflow doesn't work without it 2. **Missing overflow: hidden** — Content overflows instead of truncating 3. **Using overflow: scroll always** — Scrollbar shows even when not needed 4. **Not testing long content** — Truncation may not work as expected 5. **Ignoring accessibility** — Truncated content needs tooltip
No changes yet
Reset to original
Submit suggestion
cancel