CSS Overflow
Control how content behaves when it exceeds its container's boundaries. Master scrolling, clipping, and responsive overflow handling.
What does overflow do in CSS?
overflow decides what happens when content is bigger than its box: visible (the default) lets it spill out, hidden clips it, scroll always shows scrollbars, and auto shows scrollbars only when they're needed. Use overflow-x and overflow-y to control each direction.
.code-block { overflow-x: auto; }
.modal-body { max-height: 70vh; overflow-y: auto; }
.thumb { overflow: hidden; border-radius: 12px; }
.no-scroll { overflow: clip; }Interactive Overflow Demo
Visible Overflow
overflow: visible; /* Default */
overflow-x: visible;
overflow-y: visible;Content extends beyond container boundaries
Hidden Overflow
overflow: hidden;
overflow-x: hidden;
overflow-y: hidden;Clips content that exceeds container bounds
Scroll Overflow
overflow: scroll;
overflow: auto;
overflow-y: scroll;Adds scrollbars when content overflows
Text Overflow
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;Truncate text with ellipsis (...) when it overflows
Code Editor
<div class="box visible">visible: this long text spills out of its box because overflow is visible by default.</div>
<div class="box hidden">hidden: this long text is clipped at the edge of the box and the rest is cut off.</div>
<div class="box auto">auto: this long text gets a scrollbar only because it is longer than the box can hold. Scroll me.</div>
<p class="ellipsis">ellipsis: one long line of text that ends with three dots when it does not fit</p>Preview
Understanding Overflow
The overflow property controls what happens when content is too large to fit in its container. It's crucial for creating scrollable areas, preventing layout breaks, and building responsive interfaces.
Basic Values
- visible: Content overflows (default)
- hidden: Content is clipped
- scroll: Always show scrollbars
- auto: Scrollbars when needed
Directional Control
- overflow-x: Horizontal only
- overflow-y: Vertical only
- Combined: Different values per axis
Text Overflow
Text overflow specifically handles how text behaves when it exceeds container width, commonly used for truncating long text with ellipsis.
For single-line text truncation, you need three properties working together:
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;Practical Applications
Scrollable Content Areas
- • Chat windows and message lists
- • Dropdown menus with many options
- • Comments and feedback sections
- • Code blocks and terminal outputs
Hidden Overflow Uses
- • Creating clean card designs
- • Cropping images to fit containers
- • Preventing layout breaks
- • Masking content during animations
Best Practices
Do
- • Use overflow: auto for most scrollable areas
- • Style custom scrollbars for better UX
- • Test on mobile devices
- • Consider keyboard navigation
- • Provide visual scroll indicators
Avoid
- • Using overflow: scroll unnecessarily
- • Hiding important content without alternatives
- • Creating horizontal scroll on mobile
- • Nested scrollable areas without purpose
- • Forgetting focus management in scrollable content
CSS Overflow — step by step
overflow: visible, hidden, scroll, auto
visible (default) lets content spill out. hidden clips it. scroll always shows scrollbars. auto adds scrollbars only when needed.
🎯 Quick Check
Which overflow value adds scrollbars only when the content actually overflows?
CSS Overflow: summary
Key points
- Overflow only happens if the box has a constrained size, such as a
height,max-heightor width. - Any value other than
visiblecreates a new block formatting context and breaksposition: stickyfor children. overflow: clipclips likehiddenbut doesn't create a scroll container.overscroll-behavior: containstops a scrolling modal from scrolling the page behind it.
Frequently asked questions
›How do I find what is causing horizontal scroll?
In DevTools, temporarily add * { outline: 1px solid red; } to see which element sticks out. Common culprits are 100vw, fixed widths and long words or URLs.
›What is the difference between overflow hidden and auto?
hidden clips extra content with no way to scroll to it. auto adds a scrollbar when the content overflows.