Overflow: Scroll Containers, overflow: clip, text-overflow, line-clamp and Scrollbars
What happens when content doesn't fit: overflow values and scroll containers, hidden versus clip, mixing axes, overflow on the page, truncating text with text-overflow and line clamping, scrollbar-gutter and scrollbar styling, overscroll-behavior and smooth scrolling.
kvi5hnuCode Sprout team member10 min readOn this page
Content often doesn't fit its box: a long word in a narrow column, a fixed-height panel with too much text, a wide table on a phone. By default, CSS lets it overflow: it spills out of the box, visible, without changing the box's size. The overflow property decides what happens instead: clip it, or make the box scroll.
The overflow values
body { font: 0.85rem/1.35 system-ui, sans-serif; margin: 1rem; display: grid; grid-template-columns: repeat(auto-fill, minmax(8.5rem, 1fr)); gap: 3rem 1rem; }
.box { height: 3.5rem; padding: 0.4rem; border: 2px solid #8a3b12; background: #f4efe6; }
.visible { overflow: visible; }
.hidden { overflow: hidden; }
.clip { overflow: clip; }
.scroll { overflow: scroll; }
.auto { overflow: auto; }
| Value | Overflowing content | Scroll container | Scrollbars |
|---|---|---|---|
visible (initial) | drawn outside the box | no | none |
hidden | clipped at the padding edge | yes, scrollable by script and focus | none |
clip | clipped at the padding edge | no | none |
scroll | clipped, scrollable | yes | always shown, where the platform shows scrollbars |
auto | clipped, scrollable | yes | only when needed |
autois the usual choice for scrollable areas: no scrollbars until the content needs them.scrollalways reserves the scrollbars, even when nothing overflows. On macOS and phones, where scrollbars are overlays that appear while scrolling,scrollandautolook the same.- Content is clipped at the padding edge, so padding stays inside the visible area, and rounded corners (article 41) clip the content too.
overflowdoesn't apply to inline elements; use it on blocks, inline-blocks, flex and grid items and so on.
Scroll containers
hidden, scroll and auto make the element a scroll container. That has consequences beyond clipping:
- It creates a new block formatting context (article 44): margins of children don't collapse through it, and it contains floats.
- It becomes the scrolling ancestor for
position: stickydescendants (article 57). A sticky header inside ahiddenorautoancestor sticks inside that ancestor, not to the page, which is the usual reason "sticky doesn't work". overflow: hiddencontent can still be scrolled by JavaScript, byscrollIntoView(), by focusing an element inside, or by an anchor link. It only hides the scrollbars.
hidden or clip?
overflow: clip clips just like hidden, but the element isn't a scroll container: nothing can scroll it, it doesn't create a formatting context, and sticky descendants still stick to the page. Use clip whenever you only want to cut off content, such as decorations that stick out of a card or an image inside rounded corners. It's in all browsers since 2022.
overflow-clip-margin lets clipped content extend a little past the box, by a length or from a chosen box edge: overflow: clip; overflow-clip-margin: 0.5rem. It's in Firefox and partly in Chrome, not in Safari.
Two axes
overflow is shorthand for overflow-x and overflow-y, in that order: overflow: hidden auto clips horizontally and scrolls vertically. The logical versions are overflow-inline and overflow-block (article 47), in all browsers since 2025.
One rule surprises people: if one axis is visible and the other is hidden, scroll or auto, the visible one becomes auto. A box can't scroll in one direction and spill out in the other. clip is the exception: overflow-x: clip with overflow-y: visible stays as written, which makes it the right tool for "cut off sideways overflow, but let dropdowns hang down".

The computed values under each card come from getComputedStyle. Only clip lets one axis stay visible.
.carousel { overflow-x: auto; overflow-y: hidden; } /* horizontal scrolling only */
.section { overflow-x: clip; } /* hide sideways bleed, keep vertical overflow */
Overflow on the page
The page's own scrollbars belong to the viewport. overflow on the root element (<html>) is applied to the viewport instead; if the root's value is visible, the <body>'s value is used for the viewport. So body { overflow: hidden } usually stops the user from scrolling the page, although scripts and anchor links can still scroll it.
That's the common way to lock scrolling while a modal is open:
html:has(dialog[open]:modal) { overflow: hidden; }
Add scrollbar-gutter: stable (below) so the page doesn't shift sideways when its scrollbar disappears.
An unwanted horizontal scrollbar on the whole page usually comes from one element wider than the viewport: an image without max-width: 100%, a 100vw width, which ignores the vertical scrollbar (article 25), or negative margins. Find it with DevTools rather than hiding it with overflow-x: hidden on body, which also breaks sticky positioning in some layouts; overflow-x: clip is the safer patch.
Truncating text
One line: text-overflow
text-overflow: ellipsis shows "…" where a single line is cut off. It needs three declarations together:
.title {
white-space: nowrap; /* keep it on one line */
overflow: hidden; /* clip it */
text-overflow: ellipsis; /* show … at the cut */
}
When the truncated element is itself a flex item, this works as is: overflow: hidden already sets its automatic minimum width to 0. When it's inside a wrapper that is the flex item, such as a title and subtitle in a <div>, give the wrapper min-width: 0 (article 62), or the wrapper grows to fit the text and nothing is cut. Firefox and Chrome 157 also support a custom string, text-overflow: " [more]", and Safari 27.3 adds it. Only Firefox supports a two-value form for both ends.
Several lines: line clamping
To cut text after a number of lines, the only syntax that works today is an old prefixed one, which all browsers support:
.excerpt {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
The standard line-clamp: 3 property is specified, but not yet shipped unprefixed in any stable browser.

The middle row is the case that needs min-width: 0. The ellipsis styles are identical in all three rows.
body { font: 1rem/1.4 system-ui, sans-serif; margin: 1rem; max-width: 18rem; }
.title { font-size: 1rem; margin: 0 0 0.5rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.excerpt { margin: 0; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; }
Truncation hides content, so make sure the full text is available: a link to the full item, a title attribute, or the complete text on a details page. Screen readers still read the whole text.
Scrollbars
scrollbar-gutter
Classic scrollbars take space. When content grows enough to need one, the scrollbar appears and the content narrows, shifting the layout. scrollbar-gutter: stable reserves the space from the start:
html { scrollbar-gutter: stable; } /* no jump when pages are longer or shorter */
.panel { overflow: auto; scrollbar-gutter: stable both-edges; } /* symmetric space */
It has no effect with overlay scrollbars, as on macOS by default and on phones. It's Baseline since December 2024.
Styling scrollbars
Two standard properties style scrollbars, Baseline since December 2025:
.panel {
scrollbar-width: thin; /* auto, thin or none */
scrollbar-color: #8a3b12 #f4efe6; /* thumb, then track */
}
scrollbar-width: nonehides the scrollbar while keeping the element scrollable, for carousels with their own controls. Don't hide scrollbars on ordinary content: they show that more content exists, and some users need them to scroll.scrollbar-colorfollowscolor-scheme(article 34) when left atauto, so dark pages get dark scrollbars without it.- The old
::-webkit-scrollbarpseudo-elements allow more detailed styling in Chrome and Safari, but in Chrome, setting either standard property disables them. Prefer the standard properties. - The old
overflow: overlayvalue is now just an alias forauto.
Scroll behavior
overscroll-behavior
When a scrolling panel reaches its end, further scrolling normally chains to the page behind it, which then scrolls instead. For modals, sidebars and chat panels, that's annoying. overscroll-behavior: contain stops the chaining, and also the pull-to-refresh and bounce effects on the page:
.chat-messages { overflow-y: auto; overscroll-behavior: contain; }
none also removes the element's own bounce effect. Chrome 144 and Firefox 150 extended it to scroll containers that don't currently overflow; Safari applies it only to those that do.
scroll-behavior
scroll-behavior: smooth animates scrolling caused by anchor links and scrollIntoView(), instead of jumping. Respect users who prefer less motion:
@media (prefers-reduced-motion: no-preference) {
html { scroll-behavior: smooth; }
}
Scroll snapping, carousels and scroll-linked animations have their own articles (115 and 113), and scroll-state container queries are in article 79.
Keyboard access to scroll areas
A scroll container with no focusable content inside, such as a scrolling code sample, must still be reachable with the keyboard. Chrome and Firefox make such scrollers focusable automatically. For other browsers, and for screen readers, add tabindex="0" with a role and a label: <div class="scroller" tabindex="0" role="region" aria-label="Code sample">.
What's current
overflow: clipworks everywhere.overflow-clip-marginis missing in Safari and partial in Chrome.scrollbar-gutterandscrollbar-widthare Baseline since 2024;scrollbar-colorsince December 2025.- The standard
line-clampproperty hasn't shipped; use the-webkit-line-clamppattern. overflow-inlineandoverflow-blockare in all browsers since 2025.overscroll-behaviornow also applies to scroll containers without overflow in Chrome 144 and Firefox 150.
Common questions
Why does my position: sticky header not stick?
Usually an ancestor has overflow: hidden, auto or scroll, which makes it the scroll container the header sticks to. Use overflow: clip on that ancestor if you only need clipping.
Why do I get a vertical scrollbar after setting overflow-x: hidden?
overflow-y was visible, which becomes auto when the other axis isn't visible. Use overflow-x: clip to keep vertical overflow visible.
Why doesn't text-overflow: ellipsis work?
It needs overflow: hidden (or clip) and white-space: nowrap on the same block element, and that element must be narrower than its text. In flexbox, if the element sits inside a wrapper that is the flex item, add min-width: 0 to the wrapper.
Why does my sticky header cover the heading when I follow a #section link?
The browser scrolls the target to the very top of the scroll container, under the header. Reserve the space with scroll-padding-top on the scroller, for the page that's html { scroll-padding-top: 4rem; }, or with scroll-margin-top on the targets. The two add up: tested in Chrome, 60px of padding plus 30px of margin left the heading 90px from the top.
How do I hide a scrollbar but keep scrolling?
scrollbar-width: none. Only do it where other controls, such as buttons or swiping, make the overflow obvious.
Why does the page jump sideways when I open a modal?
Locking the scroll removed the page's scrollbar, so the content widened. Add scrollbar-gutter: stable to html.
Summary
overflowcontrols content that doesn't fit:visible(default),hidden,clip,scrollorauto. Content is clipped at the padding edge.hidden,scrollandautocreate scroll containers, which form a new formatting context and capture sticky descendants.cliponly clips.- With one axis
visibleand the other not,visiblebecomesauto, except withclip. overflowonhtmlorbodycontrols the viewport's scrollbars.- Single-line ellipsis needs
white-space: nowrap,overflow: hiddenandtext-overflow: ellipsis. Multi-line clamping uses the-webkit-line-clamppattern. scrollbar-gutter: stableprevents layout shifts;scrollbar-widthandscrollbar-colorstyle scrollbars.overscroll-behavior: containstops scroll chaining, and smooth scrolling belongs inside a reduced-motion check.
Practice questions
Make a sidebar list at most 20rem tall that scrolls vertically when needed, without scrolling the page when it reaches the end.
Answer
.sidebar-list { max-height: 20rem; overflow-y: auto; overscroll-behavior: contain; }
A card with rounded corners has a decorative ribbon that sticks out, and should be clipped, but the card contains a sticky element. Which overflow value do you use, and why?
Answer
overflow: clip. It clips like hidden, but doesn't make the card a scroll container, so the sticky element keeps working relative to the page.
Write CSS that shows a product name on one line, ending in "…" if it's too long.
Answer
.product-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
The rule below makes a dropdown inside the panel get cut off at the bottom. Why, and what's the fix?
.panel { overflow-x: hidden; }
Answer
overflow-y becomes auto because the other axis isn't visible, so the panel clips and scrolls vertically too. Use overflow-x: clip, which leaves overflow-y: visible.
Previous: Outlines and Focus Indicators: outline, outline-offset and Accessible Focus Styles
Next: Normal Flow, the Containing Block and Block Formatting Contexts

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.