CSS Overflow: hidden, scroll, auto, clip and Text Truncation
CSS Overflow: hidden, scroll, auto, clip and text-overflow
The CSS overflow property controls what happens when content is too big for its box. Should it spill out, be cut off, or get a scrollbar? Overflow is behind scrollable panels, modals, carousels, truncated text and the dreaded accidental horizontal scrollbar, so it is one of the most practical properties to master.
What You Will Learn
- All overflow values:
visible,hidden,scroll,auto,clip overflow-xandoverflow-yand their surprising interaction- Truncating text with
text-overflow: ellipsisandline-clamp - Breaking long words with
overflow-wrap scrollbar-gutter,overscroll-behaviorand scroll snapping basics- How overflow interacts with
position: sticky, margin collapse and block formatting contexts - How to find and fix unwanted horizontal scrolling
When Does Overflow Happen?
Overflow happens when the content of a box does not fit inside its set size. For example, a box with a fixed height: 100px that contains 300px of text will overflow vertically. If the height is auto, the box grows and there is no vertical overflow.
The Overflow Values
visible(default): content is not clipped and spills outside the box. It can overlap other elements.hidden: content outside the padding box is clipped and cannot be reached by the user with scrollbars. It can still be scrolled with JavaScript or by focusing an element inside.scroll: content is clipped and scrollbars are always shown, even if nothing overflows.auto: scrollbars appear only when needed. This is usually the best choice for scrollable panels.clip: like hidden, but no scroll container is created and programmatic scrolling is impossible. It is stricter and slightly cheaper.
overflow-x and overflow-y
You can control each axis separately. There is an important rule: if one axis is visible and the other is not, the visible value computes to auto. So overflow-x: auto; overflow-y: visible; does not give you visible vertical overflow. Use overflow-y: clip or restructure the layout if you need that. The shorthand overflow: hidden auto sets x then y.
Scroll Containers
Any element with overflow set to hidden, scroll or auto becomes a scroll container. A scroll container also creates a new block formatting context, which means it contains its floats and stops margin collapse between itself and its children. This is why overflow: hidden used to be a popular clearfix hack. Today prefer display: flow-root.
Truncating Text with an Ellipsis
Single line truncation needs three properties together:
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}For multi-line truncation use line-clamp. The widely supported form is:
.clamp-3 {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
line-clamp: 3;
overflow: hidden;
}Breaking Long Words: overflow-wrap
Long URLs or unbroken strings can overflow their container. Use overflow-wrap: anywhere (or break-word) to let the browser break them. In flex and grid children, also set min-width: 0, because flex items default to min-width: auto and refuse to shrink below their content.
scrollbar-gutter
When a scrollbar appears, it can shift content sideways because it takes space. scrollbar-gutter: stable; reserves that space in advance so the layout does not jump when content grows. Use stable both-edges to keep centered content symmetric.
overscroll-behavior
overscroll-behavior: contain; on a scrollable panel, like a modal body or chat list, stops the page behind from scrolling when the user reaches the end of the panel (scroll chaining).
Scroll Snap Basics
On a scroll container, scroll-snap-type: x mandatory; together with scroll-snap-align: start; on children creates smooth carousel-like snapping without JavaScript.
overflow and position: sticky
A sticky element sticks relative to its nearest ancestor scroll container. If any ancestor has overflow: hidden, auto or scroll, sticky may seem broken because that ancestor becomes the scroll container. Fix it by removing overflow from the ancestor or by using overflow: clip, which does not create a scroll container.
overflow: hidden and Positioned Children
Overflow clipping only affects descendants whose containing block is the element or one of its descendants. An absolutely positioned child whose containing block is an ancestor outside the overflow element is not clipped by it. Dropdown menus often break for exactly this reason: they are clipped when their parent has overflow hidden and is the positioned ancestor.
Finding Unwanted Horizontal Scroll
- Open DevTools and check which element is wider than the viewport. A quick debug rule is
* { outline: 1px solid red; }. - Look for
width: 100vw. Thevwunit includes the vertical scrollbar width, so it causes overflow on desktop. Use100%instead. - Check for fixed widths, wide images without
max-width: 100%, wide tables and long unbroken text. - Check negative margins and absolutely positioned elements pushed off-screen.
Avoid fixing this with overflow-x: hidden on the body, because it hides the symptom and can break sticky positioning. Fix the element that is too wide.
Accessibility Notes
- Scrollable regions must be reachable by keyboard. Add
tabindex='0'and an accessible name (for examplerole='region'witharia-label) to scrollable areas that contain no focusable elements. - Do not hide important content with
overflow: hiddenand fixed height. Text zoom and translation can make text larger than expected. - Truncated text should be available in full elsewhere, such as a title attribute or expanded view.
Common Mistakes
- Using
overflow: scrollwhenautois enough. - Expecting
overflow-y: visibleto work whenoverflow-xis auto. - Forgetting
white-space: nowrapwithtext-overflow: ellipsis. - Using
overflow-x: hiddenon body to hide layout bugs. - Forgetting
min-width: 0on flex children that hold long text.
Frequently Asked Questions
What is the difference between overflow hidden and clip?
Both cut the content. hidden creates a scroll container (so it can still be scrolled by script and affects sticky), while clip does not.
Why is my text-overflow ellipsis not working?
You need a constrained width, overflow other than visible and white-space: nowrap. In flex layouts add min-width: 0 to the item.
Why is there a horizontal scrollbar on my page?
Some element is wider than the viewport. Common causes are 100vw, fixed widths and wide media. Find it in DevTools.
Does overflow: auto always show a scrollbar?
No. The scrollbar appears only when the content actually overflows.
Key Takeaways
- Default is visible. Use
autofor scroll panels,hiddenorclipto cut content. - Ellipsis needs nowrap, overflow hidden and text-overflow together.
- overflow creates a scroll container, which affects sticky, floats and margin collapse.
- Use
scrollbar-gutterandoverscroll-behaviorfor polished scrolling. - Fix the wide element instead of hiding overflow on the body.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
The title below wraps onto several lines and overflows its 200px card. Using only the .title rule, make it stay on one line and end with an ellipsis (three dots) when it is too long.
Press Run to execute.
Show expected output
The title stays on one line and ends with an ellipsis.This is a self-check — compare your result with the expected output above.
Make the .chat panel 150px tall with a vertical scrollbar that appears only when needed. Also stop the page behind it from scrolling when the user reaches the end of the panel.
Press Run to execute.
Show expected output
The panel is 150px tall, scrolls vertically only when needed and does not chain scrolling to the page.This is a self-check — compare your result with the expected output above.
Write a function isOverflowing(scrollWidth, clientWidth, scrollHeight, clientHeight) that returns true if the content overflows on either axis.
Press Run to execute.
Show expected output
true
true
falseThis is a self-check — compare your result with the expected output above.
Was this page helpful?