Boxes in CSS have sizes, but content doesn't always cooperate. A paragraph is longer than the card it sits in, a table is wider than a phone screen, someone pastes a 60-character URL without spaces, or a list of 500 chat messages must fit in a 400px panel. When content is bigger than its box, we have overflow.
In this chapter we learn what the browser does with overflow by default, the five overflow values (including the difference between hidden and clip that many developers get wrong), how to truncate text with "...", why flex items refuse to shrink, and the modern scrolling toolkit: smooth scrolling, scroll snapping, overscroll-behavior and scrollbar styling.
What is Overflow?
Overflow happens when an element's content is too big to fit inside its padding box. The
overflowproperty decides whether that extra content is shown, clipped, or made reachable by scrolling.
Overflow only happens when the box has a limited size. A normal block with height: auto simply grows taller to fit its content, so it never overflows vertically. Overflow appears when you set a fixed height/max-height/width, or when something can't wrap (a long word, an image, a white-space: nowrap line).
<div class="box">
This text is far too long for the small box it lives in, so it
overflows past the bottom edge of the box. More words here.
</div>.box {
width: 140px;
height: 70px;
padding: 6px;
border: 2px solid #2563eb;
}Output: the text runs out of the bottom of the blue border and over whatever comes next. This is the default, overflow: visible. CSS chooses to show the content (even if it looks messy) rather than silently hide it, because hidden content means lost information.
The overflow Values
| Value | Extra content | Scrollbars | User can scroll? | Script can scroll? |
|---|---|---|---|---|
visible (default) | Drawn outside the box | No | No | No |
hidden | Clipped at the padding edge | No | No (no scrollbar, no wheel) | Yes |
clip | Clipped at the padding edge | No | No | No |
scroll | Clipped, reachable by scrolling | Always (even if not needed) | Yes | Yes |
auto | Clipped, reachable by scrolling | Only when needed | Yes | Yes |
We rendered the box above with each value in Chrome on Windows:
visible: the last line ("box. More words here.") hangs below the border.hiddenandclip: the text is cut off cleanly at the bottom edge.scrollon a box containing only "Short text.": both a vertical and a horizontal scrollbar appear anyway, greyed out.auto: a vertical scrollbar appears because the text is too tall, but no horizontal one.
In everyday work, auto is the right choice for scrollable areas. scroll is mainly used when you want the layout to stay identical whether or not a scrollbar is needed (though scrollbar-gutter, covered later, is a better tool for that today).
overflow-x and overflow-y
You can control each direction separately:
.table-wrapper {
overflow-x: auto; /* scroll sideways if the table is too wide */
overflow-y: visible;
}The shorthand takes one or two values: overflow: hidden sets both; overflow: hidden auto sets x = hidden and y = auto.
But there's a catch that surprises everyone:
If one axis is
visibleand the other is a scrolling/clipping value likehidden,scrollorauto, thevisibleone is computed asauto.
So the .table-wrapper above actually has overflow-y: auto. Chrome confirms: overflow: visible hidden computes to overflow-x: auto; overflow-y: hidden. The reason is that a box can't sensibly scroll in one direction while spilling content freely in the other. This is why you can't have a horizontally scrolling row whose dropdown menus spill out vertically: the dropdowns get clipped.
Scrollbars take space (sometimes)
On Windows and Linux desktops, classic scrollbars sit inside the box and reduce the space for content. On phones and on macOS by default, scrollbars are thin overlays that take no space. Let's measure the auto box from before (width: 140px, padding: 6px, border: 2px, normal content-box sizing):
offsetWidth = 2 + 6 + 140 + 6 + 2 = 156px (border box)
clientWidth = 6 + 140 + 6 − scrollbar = 152 − 15 = 137px (padding box minus scrollbar)Chrome on Windows reported exactly offsetWidth = 156 and clientWidth = 137, so the scrollbar took 15px. Don't hard-code that number, though: it depends on the operating system and settings. (offsetWidth and clientWidth are the box model measurements from The Box Model, read through JavaScript.)
hidden vs clip
Both look identical on screen, so why have two?
overflow: hiddenmakes the element a scroll container with the scrollbars removed. The content is still scrollable by JavaScript (element.scrollTop = 20works), and by the browser itself, for example when a focused input or an anchor link inside needs to come into view. This can cause "my content suddenly shifted" bugs.overflow: clipjust cuts off the drawing. It is not a scroll container, nothing can scroll it, and it doesn't create a new formatting context. It has been supported in all major browsers since 2022.
We tested both: after setting scrollTop = 20, the hidden box reported scrollTop 20 (its text had moved up), and the clip box stayed at 0.
Side effects of hidden: BFC and floats
Any overflow value other than visible and clip creates a block formatting context (BFC). A BFC contains its floats and stops its margins collapsing with its children's. That's why overflow: hidden was the classic "clearfix" for float layouts (see Float & Clear):
<div class="wrapper">
<div class="floated"></div> <!-- float: left; height: 80px -->
text
</div>.wrapper {
border: 2px solid red;
line-height: 20px; /* one line of text = 20px tall */
}
.floated {
float: left;
width: 50px;
height: 80px;
}| Wrapper style | Wrapper height (our test) | Why |
|---|---|---|
| none | 24px | Floats don't count towards the parent's height; only the 20px text line does (+ 2px border top and bottom) |
overflow: hidden | 84px | New BFC contains the 80px float (+ 2px border top and bottom) |
overflow: clip | 24px | clip does not create a BFC |
display: flow-root | 84px | Creates a BFC with no clipping at all: the modern clearfix |
Takeaway: use overflow: clip when you only want to cut off drawing, display: flow-root when you only want a BFC, and overflow: hidden when you want both (or need it for older code).
Clipping also hides shadows and outlines
Overflow clipping applies to everything outside the padding box, including children's box-shadow, focus outlines and absolutely positioned dropdowns (whose containing block is inside the clipped element). If a card's hover shadow or a tooltip gets cut off, look for an ancestor with overflow: hidden. (overflow: clip plus overflow-clip-margin lets you extend the clip edge a little, but Safari doesn't support overflow-clip-margin yet.)
Truncating Text
One line with "..."
.file-name {
width: 200px;
white-space: nowrap; /* 1. keep it on one line */
overflow: hidden; /* 2. cut off what doesn't fit */
text-overflow: ellipsis; /* 3. show "..." at the cut */
}Output: "The quick brown fox jumps over the lazy dog again..." becomes The quick brown fox jump… with an ellipsis at the end (in Chrome with the default sans-serif font; exactly where the cut falls depends on the font). All three lines are required. text-overflow on its own does nothing, because without nowrap the text would wrap instead of overflowing, and without overflow other than visible nothing is cut. Also, the element must be a block or inline-block with a limited width.
Several lines with "..."
.excerpt {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2; /* show at most 2 lines */
overflow: hidden;
}Output: a long paragraph shows just its first two lines, with "…" at the end of the second line. Despite the -webkit- prefixes, this exact combination works in Chrome, Edge, Firefox and Safari, and is the standard way to clamp lines today. The unprefixed line-clamp property is in the CSS specification, but no major browser supports it yet, so stick with the prefixed version.
Long words and URLs
A very long word or URL can't wrap, so it overflows a narrow column, often causing sideways scrolling on phones. Fix it at the text level:
.comment {
overflow-wrap: anywhere; /* break long words only if they don't fit */
}(See Text & Fonts for overflow-wrap, word-break and hyphens.)
The Flexbox / Grid Overflow Trap: min-width: 0
This is one of the most common real-world CSS bugs. A sidebar layout with a file name that should be truncated:
<div class="row">
<div class="side">Side</div>
<div class="main">
<div class="file-name">A very long file name that should be truncated with an ellipsis.pdf</div>
</div>
</div>.row {
display: flex;
width: 300px;
}
.side {
width: 80px;
flex-shrink: 0;
}
.file-name {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}Output: no ellipsis. The long name sticks out far beyond the 300px row.
Why? A flex item's default is min-width: auto, which for flex items means "never shrink below the width of my content". The content of .main is one long unbreakable line, so .main refuses to shrink, becomes very wide, and .file-name (which is as wide as .main) has nothing to cut off.
The fix is one line on the flex item:
.main {
min-width: 0; /* allow the flex item to shrink below its content width */
}Output: A very long file name that sh… fits neatly in the remaining 220px (without the fix, Chrome measured .main at 459px wide). We confirmed both versions in Chrome. The same applies to grid items (min-width: auto by default) and to vertical flex layouts (use min-height: 0). Making the item a scroll container (for example overflow: hidden or auto) also works, because the automatic minimum size doesn't apply to scroll containers.
Scrolling the Page Smoothly
scroll-behavior
html {
scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
html {
scroll-behavior: auto;
}
}Now clicking an in-page link such as <a href="#syllabus"> glides to the target instead of jumping. It also affects JavaScript calls like element.scrollIntoView(). It doesn't change how the mouse wheel or touch scrolling feels. Turning it off for reduced motion users is good practice (see Transitions).
scroll-margin-top: anchors under a sticky header
With a sticky or fixed header (see Positioning), jumping to #syllabus puts the heading at the very top of the window, hidden behind the header. Fix it on the targets:
h2 {
scroll-margin-top: 80px; /* stop 80px below the top when scrolled into view */
}Or set scroll-padding-top: 80px once on the scroll container (html for the page), which reserves that space for all targets.
Scroll Snap
Scroll snap makes a scroll container settle on neat positions after the user stops scrolling, like an image carousel or a full-screen slide deck, without JavaScript.
<div class="carousel">
<div class="slide">1</div>
<div class="slide">2</div>
<div class="slide">3</div>
<div class="slide">4</div>
</div>.carousel {
display: flex;
gap: 16px;
width: 400px;
overflow-x: auto; /* 1. it must be a scroll container */
scroll-snap-type: x mandatory; /* 2. snap horizontally, always */
}
.slide {
flex: 0 0 300px; /* fixed 300px slides, don't shrink */
height: 120px;
scroll-snap-align: start; /* 3. snap so the slide's start lines up */
}scroll-snap-typeon the container: the axis (x,y,both) and the strictness.mandatoryalways comes to rest on a snap point;proximitysnaps only if the scroll ends near one.scroll-snap-alignon the children:start,centerorend.scroll-snap-stop: alwayson children stops a fast swipe from skipping past slides.
Worked example
The slides start at 0, 316, 632 and 948 px (300px slide + 16px gap each). If a script (or the user) scrolls to 200px, where does the carousel settle?
The nearby snap positions are 0 and 316.
200 is 200px from 0 but only 116px from 316.
So it settles at 316: slide 2 lines up with the left edge.
Chrome agreed: scrollTo({ left: 200 }) ended with scrollLeft = 316. One more detail: the maximum scroll here is 1248 − 400 = 848px (four slides plus three gaps = 1248px of content, minus the 400px visible width), so slide 4's start (948) can never reach the left edge. The browser treats the end of the scroll range as a valid resting place, so the last slide is still fully reachable.
Scroll snap is Baseline widely available. Use mandatory carefully: if a slide is taller or wider than the container, mandatory snapping can make part of it hard to reach.
overscroll-behavior: Stopping Scroll Chaining
Scroll to the bottom of a chat panel or a modal, keep scrolling, and the page behind it starts scrolling. This is called scroll chaining. On mobile, pulling down at the top can also trigger the browser's pull-to-refresh.
.chat-messages {
height: 400px;
overflow-y: auto;
overscroll-behavior: contain; /* don't pass extra scrolling to the page */
}| Value | Meaning |
|---|---|
auto (default) | Scroll chains to the parent; normal bounce/refresh effects |
contain | No chaining to parents; the element's own bounce effect still shows |
none | No chaining and no bounce/pull-to-refresh effects |
It works in all major browsers (Safari since version 16). One limitation: Safari, and Chrome before version 144 and Firefox before version 150, ignore it on a scroll container that currently has nothing to scroll (for example a modal whose content happens to fit), so the page behind can still scroll in that case.
Scrollbars: Gutters and Styling
scrollbar-gutter: no more layout jumps
On desktops with classic scrollbars, content shifts sideways by about 15px when a scrollbar appears (for example, when a list grows from 3 to 30 items). scrollbar-gutter: stable reserves the scrollbar's space all the time:
.list {
height: 300px;
overflow-y: auto;
scrollbar-gutter: stable;
}In our test, a 200px-wide box had a clientWidth of 200 without the property. With scrollbar-gutter: stable it was 185 whether the content was short or long, so the content never moves. It is Baseline newly available (since December 2024). On systems with overlay scrollbars it has no effect, because those scrollbars don't take space.
Styling scrollbars
The standard properties are deliberately simple:
.panel {
scrollbar-width: thin; /* auto | thin | none */
scrollbar-color: #2563eb #e5e7eb; /* thumb colour, track colour */
}scrollbar-width is Baseline since December 2024, and scrollbar-color since December 2025 (when Safari 26.2 added it). You'll also find ::-webkit-scrollbar pseudo-elements in older tutorials. They are non-standard, offer more control and still work in Chrome and Safari, but Chrome ignores them on an element when the standard scrollbar-width/scrollbar-color properties are set. Prefer the standard properties.
Hiding scrollbars with scrollbar-width: none keeps the area scrollable but removes the visual clue that more content exists. Use it only when there's another obvious hint, such as half-visible next slides in a carousel.
Overflow and position: sticky
In Positioning we saw that a sticky element sticks within its nearest scrolling ancestor. Since overflow: hidden, auto and scroll all make an element a scroll container, putting overflow: hidden on any ancestor of a sticky header (often added "just to fix" something else) makes the header stick to that ancestor instead of the page. If the ancestor doesn't actually scroll, the header never sticks at all. Because overflow: clip does not create a scroll container, switching that ancestor to clip usually fixes the problem.
Common Mistakes
Expecting a scrollbar without a size limit.
overflow: autodoes nothing if the element can grow freely. Give it aheight/max-height(or let a flex/grid parent limit it).Using
overflow: scrolland getting ugly empty scrollbars on Windows. Useauto.text-overflow: ellipsisalone. It needswhite-space: nowrapandoverflow: hiddentoo.Truncation not working in flex/grid. Add
min-width: 0to the item.Setting
overflow-x: hiddenand expectingoverflow-yto stayvisible. It becomesauto.Hiding horizontal page scroll with
body { overflow-x: hidden; }instead of finding the element that is too wide. It hides the symptom, can breakposition: sticky, and the real problem (often a100vwwidth or an unbreakable URL) remains.Using
overflow: hiddenjust to contain floats and then wondering why shadows and dropdowns are cut off. Usedisplay: flow-root.
Interview / Exam Questions
1. What is the difference between overflow: auto and overflow: scroll?
Both clip the content and allow scrolling. scroll always shows scrollbars (on systems with classic scrollbars), even if the content fits. auto shows them only when the content actually overflows.
2. What is the difference between overflow: hidden and overflow: clip?
Both cut off overflowing content without scrollbars. hidden still makes the element a scroll container, so it can be scrolled by JavaScript or by focus/anchor navigation, and it creates a block formatting context. clip only clips the painting; it is not a scroll container, can't be scrolled at all, and doesn't create a BFC.
3. What does overflow: visible hidden compute to, and why?
overflow-x: auto; overflow-y: hidden. When one axis is visible and the other is a scrolling or hiding value (hidden, scroll, auto), the visible axis is changed to auto, because a scroll container can't let content spill freely in one direction.
4. Which three properties are needed for single-line ellipsis truncation?
white-space: nowrap, overflow: hidden and text-overflow: ellipsis, on a block or inline-block element with a limited width.
5. Why doesn't text-overflow: ellipsis work inside a flex item, and how do you fix it?
Flex items have min-width: auto, which stops them shrinking below their content's width, so the item grows to fit the whole unbroken line and nothing overflows. Setting min-width: 0 (or making it a scroll container with overflow: hidden/auto) on the flex item lets it shrink, so the text can be truncated.
6. A box has width: 200px, padding: 10px, border: 5px, box-sizing: content-box and overflow-y: scroll. With a 15px scrollbar, what are offsetWidth and clientWidth?
offsetWidth = 5 + 10 + 200 + 10 + 5 = 230px. clientWidth = padding box − scrollbar = (10 + 200 + 10) − 15 = 205px.
7. How do you stop the page from scrolling when the user reaches the end of a scrollable modal?
Add overscroll-behavior: contain to the modal's scroll container. It prevents scroll chaining to the page (and pull-to-refresh on mobile).
8. In a scroll-snap carousel with snap points at 0, 316 and 632px (mandatory), where does it rest if the scroll stops at 500px?
500 is 184px from 316 and 132px from 632, so it snaps to 632.
Summary
Overflow happens when content is bigger than a box with a limited size; the default
visiblelets it spill out.hiddenclips (still scrollable by script),clipclips (not scrollable, no BFC),scrollalways shows scrollbars,autoshows them when needed.Mixing
visiblewith a non-visible value on the other axis turnsvisibleintoauto.Non-visible overflow (except
clip) creates a BFC and clips children's shadows and dropdowns too.Ellipsis needs
nowrap+overflow: hidden+text-overflow: ellipsis; multi-line uses-webkit-line-clamp.Flex and grid items need
min-width: 0to shrink below their content.The scrolling toolkit:
scroll-behavior: smooth,scroll-margin-top, scroll snap,overscroll-behavior,scrollbar-gutter,scrollbar-width/scrollbar-color.overflowon an ancestor changes whereposition: stickysticks.
You now know how CSS handles content that doesn't fit. In Modern CSS: :has(), Nesting, clamp() & Logical Properties, we'll look at newer features that make many of these layouts shorter and more flexible to write.