The overflow property controls what happens when content is larger than its container’s box. Use it to clip, scroll, or reveal overflow along the x/y axes.
CSS Overflow
1) overflow: visible, hidden, scroll, auto
.ov-visible { overflow: visible; } /* default */
.ov-hidden { overflow: hidden; }
.ov-scroll { overflow: scroll; } /* always shows scrollbars */
.ov-auto { overflow: auto; } /* only when needed */
visible
Content can overflow and paint outside the box without scrollbars. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vehicula ornare arcu, sed…
hidden
Clipped content. No scrollbars shown even if text is longer than the box.
scroll
Always shows scrollbars. Useful when preserving layout even without overflow.
More lines… More lines… More lines…
auto
Shows scrollbars only when needed. Generally the best default.
More lines… More lines… More lines…
2) Axis Control: overflow-x & overflow-y
.xy { overflow: auto; } /* both axes */
.only-x { overflow-x: auto; overflow-y: hidden; }
.only-y { overflow-y: scroll; overflow-x: hidden; }
3) Text Overflow: Ellipsis
/* Single line */
.ellipsis-1{
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
/* Multi-line (line clamp) */
.ellipsis-3{
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
Single line
Multi-line (3 lines)
-webkit-line-clamp; Firefox support is evolving.
Tip: Combine with a “Read more” link if content is important.
4) Scroll Snapping (Horizontal Cards)
.snap-x { scroll-snap-type: x mandatory; overflow-x: auto; }
.snap-card { scroll-snap-align: start; }
Snap to me while scrolling →
Snap to me while scrolling →
Snap to me while scrolling →
Snap to me while scrolling →
Snap to me while scrolling →
Snap to me while scrolling →
5) Custom Scrollbar (Basic)
.scrollbar-demo { scrollbar-color: #c5e1fa #f2f6fb; scrollbar-width: thin; } /* Firefox */
/* WebKit browsers */
.scrollbar-demo::-webkit-scrollbar { width: 10px; }
.scrollbar-demo::-webkit-scrollbar-thumb { background: #c5e1fa; border-radius: 8px; }
.scrollbar-demo::-webkit-scrollbar-track { background: #f2f6fb; }
Note: Scrollbar styling varies across browsers; test on your targets.
Quick Notes:
overflow: autois a good default for scrollable regions.- Use
text-overflow: ellipsis+white-space:nowrapfor single-line truncation. - Multi-line clipping relies on
-webkit-line-clamptoday; provide graceful fallbacks. - Scroll snapping improves UX for carousels and galleries; avoid over-snapping long documents.