What overflow does
When content is bigger than its box (a fixed height, a long word, a wide table), it overflows. The overflow property decides what happens to the extra:
| Value | What you see | Scroll container |
|---|---|---|
visible (default) | Content spills out of the box and overlaps what follows | No |
hidden | Clipped at the padding edge, no scrollbars (script can still scroll it) | Yes |
clip | Clipped, and no scrolling at all, even by script | No |
scroll | Always shows scrollbars, even if the content fits | Yes |
auto | Scrollbars only when the content does not fit | Yes |
On macOS and phones scrollbars overlay the content and fade out, so scroll and auto may look the same until you scroll.
Making a scrollable box
A box overflows only if something limits its size. A height or max-height, plus overflow-y: auto, turns a long list into a scrolling panel:
max-height is better than height here: a short list stays short, and only a long list scrolls. A scrollable region that has no focusable content inside should get tabindex="0" so keyboard users can scroll it too.
overflow-x and overflow-y
The two axes can be set separately. The common use is a wide table that scrolls sideways inside its container instead of widening the whole page:
One rule to remember: if one axis is hidden, scroll or auto, a visible value on the other axis becomes auto. So overflow-x: hidden alone can give you an unexpected vertical scrollbar. overflow-x: clip does not have that effect:
.a { overflow-x: hidden; } /* overflow-y computes to auto */
.b { overflow-x: clip; } /* overflow-y stays visible */
Cutting text with an ellipsis
text-overflow: ellipsis replaces the clipped end of a line with "…". It needs three things together: a limited width, text that cannot wrap, and hidden overflow:
The -webkit- prefixed line clamp works in Chrome, Firefox and Safari. Screen readers still read the full text, so the clipped words are not lost to them.
Side effects of overflow
Setting overflow to anything other than visible or clip changes more than clipping:
- It contains floats and child margins. The element forms a new block formatting context, so it grows to wrap floated children.
display: flow-rootdoes the same without clipping anything. - It breaks
position: stickybelow it. A sticky child sticks to the nearest scroll container; if that container does not scroll, nothing sticks. - It clips absolutely positioned children (when the overflowing element is also their containing block), including dropdowns and tooltips, and it clips box shadows of children too.
Hiding the scrollbar
You can keep a box scrollable while hiding its scrollbar with scrollbar-width: none. Use it sparingly: without a scrollbar, many readers do not realize there is more content. scrollbar-gutter: stable does the opposite job: it reserves the scrollbar's space so content does not shift when a scrollbar appears.
Common mistakes
- Expecting overflow without a size limit. A box with no
heightormax-heightgrows instead of overflowing. overflow: hiddenonbodyto "fix" horizontal scroll. Find the element that is too wide instead (often a fixedwidth, a long URL, or100vwwith a scrollbar).- Ellipsis without
white-space: nowrap. The text wraps, so nothing overflows horizontally and no ellipsis appears. overflow-x: hiddencreating a vertical scrollbar. Useclip.- A dropdown cut off by its card. The card's
overflow: hiddenclips it; move the overflow rule or render the dropdown outside.
Frequently Asked Questions
What does overflow: hidden do?
It clips content that does not fit inside the element's padding box, with no scrollbars. The hidden part can still be scrolled to by script or by focusing something inside it.
What is the difference between overflow auto and scroll?
scroll always reserves scrollbars, even when the content fits. auto shows a scrollbar only when the content is bigger than the box, which is what you usually want.
How do I make a div scrollable?
Limit its size and allow scrolling: max-height: 300px; overflow-y: auto;. Without a height or max-height limit, the div grows with its content and never overflows.
Why does overflow-x: hidden also add a vertical scrollbar?
If one axis is hidden, scroll or auto, a visible value on the other axis is computed as auto. Use overflow-x: clip instead, which does not create a scroll container and leaves the other axis visible.
How do I add an ellipsis (...) to overflowing text?
For one line: white-space: nowrap; overflow: hidden; text-overflow: ellipsis; on an element with a limited width. For several lines, use display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden;.