CSS Overflow
Every fixed-size container eventually faces a question: what happens when the
content is bigger than the box? The overflow property gives you the
answer — clip it, scroll it, or let it spill out. Mastering overflow also unlocks scrollable containers,
truncated text, horizontal carousels and more.
visible,
hidden, scroll, auto, clip
overflow-x and overflow-y — per-axis controltext-overflow: ellipsis —
truncate with …overflow-wrap and word-breakAll overflow Values — Live Demo
Each card below has the same fixed box size and the same long content. Only the overflow value changes — watch how differently each one handles the
overflow:
.box { overflow: visible; } /* default — spills outside */
.box { overflow: hidden; } /* clips, no scrollbars */
.box { overflow: scroll; } /* always shows scrollbars */
.box { overflow: auto; } /* scrollbars only when needed ← preferred */
.box { overflow: clip; } /* clips, no BFC, no programmatic scroll */
/* overflow requires a defined width/height to take effect */
.scrollable {
height: 300px; /* must have a fixed height */
overflow: auto;
}
| Value | Clips content? | Shows scrollbars? | Creates BFC? | Best for |
|---|---|---|---|---|
| visible | No | No | No | Default — let content overflow |
| hidden | Yes | No | Yes | Clip, clearfix, hover zoom containers |
| scroll | Yes | Always | Yes | Rare — use auto instead |
| auto | Yes | When needed | Yes | Scrollable containers, modals, code blocks |
| clip | Yes | No | No | Visual clipping without BFC side effects |
overflow-x & overflow-y — Per-Axis Control
overflow-x and overflow-y let you control horizontal and vertical overflow independently.
The shorthand overflow sets both axes at once. Scroll each demo:
/* Horizontal scroll only — horizontal carousel */
.carousel {
overflow-x: auto;
overflow-y: hidden;
white-space: nowrap; /* prevent children from wrapping */
}
/* Scrollable container for a sidebar */
.sidebar {
overflow-y: auto;
overflow-x: hidden;
height: 100vh;
}
/* Shorthand: overflow: x y */
.box { overflow: hidden auto; } /* x=hidden y=auto */
/* ⚠ Quirk: setting one axis to visible while other is not
visible will change visible to auto. Never mix visible
with another non-visible value. */
visible while the other is hidden, scroll, or auto, the browser automatically changes the visible axis to auto. You cannot have
one axis truly visible while the other is clipped — it is not technically possible in the CSS box model.
text-overflow — Truncate with Ellipsis
text-overflow controls what visually happens when
text overflows its container horizontally. It requires three properties working together: white-space: nowrap, overflow: hidden,
and text-overflow.
/* Single-line truncation — THREE properties all required */
.truncate {
white-space: nowrap; /* prevents text from wrapping */
overflow: hidden; /* clips overflowing text */
text-overflow: ellipsis; /* adds … at truncation point */
}
/* Hard clip with no ellipsis */
.clipped {
white-space: nowrap;
overflow: hidden;
text-overflow: clip;
}
/* Multi-line clamp — limit to N lines */
.line-clamp {
display: -webkit-box;
-webkit-line-clamp: 3; /* number of lines */
-webkit-box-orient: vertical;
overflow: hidden;
}
/* Modern: line-clamp (no prefix needed in newer browsers) */
.modern-clamp {
line-clamp: 3;
overflow: hidden;
}
overflow-wrap & word-break
Long unbreakable strings (URLs, hash codes, long technical words) can overflow
containers. overflow-wrap and word-break tell the browser where it is allowed to break lines:
/* overflow-wrap: break-word — breaks long words ONLY when
they would overflow (prefers natural word breaks first) */
p, .content {
overflow-wrap: break-word; /* ← recommended for body text */
}
/* word-break: break-all — breaks at any character,
even mid-word. Aggressive. */
.code-block {
word-break: break-all;
}
/* word-break: keep-all — prevents breaks in CJK text
(Chinese, Japanese, Korean) */
.cjk {
word-break: keep-all;
}
/* hyphens — adds hyphens at word break points */
p {
hyphens: auto;
overflow-wrap: break-word;
}
/* Global reset — apply to every project */
* {
overflow-wrap: break-word;
word-break: normal;
}
Scroll Snap — Polished Scrolling
scroll-snap-type on the container and scroll-snap-align on children create a smooth snapping scroll experience —
like a carousel. Scroll the demo below:
/* ── Container ── */
.carousel {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory; /* x axis, hard snap */
scroll-behavior: smooth;
gap: 16px;
padding: 16px;
/* hide scrollbar visually (still scrollable) */
-ms-overflow-style: none;
scrollbar-width: none;
}
.carousel::-webkit-scrollbar { display: none; }
/* ── Each slide ── */
.slide {
flex: none; /* don't shrink */
width: 280px; /* fixed slide width */
scroll-snap-align: start; /* or center */
}
/* scroll-snap-type values: */
/* x mandatory — horizontal, always snaps */
/* y mandatory — vertical, always snaps */
/* both mandatory — both axes */
/* x proximity — snaps only when close to snap point */
Real-World Overflow Patterns
These are the most common uses of overflow in production web applications:
overflow-x: auto; white-space: nowrap — code
never wraps, scroll to read.max-height: 80vh; overflow-y: auto — modal
grows then scrolls.ellipsis for title, -webkit-line-clamp:2 for body.overflow: visible
(the default) — overflow: hidden would clip them.overflow: hidden on wrapper clips the scaling
image — the zoom stays inside the box.overflow: hidden — the
dropdown must be able to escape its container.Try It Yourself
Change overflow values, try different text-overflow settings, and experiment with the scroll snap carousel:
Quick Check — Chapter 23
overflow: scroll — always renders scrollbars, even
when content fits within the container. You see scrollbar gutters regardless of content size. This can
waste horizontal space and look ugly when the content is short.overflow: auto — only shows scrollbars when the
content actually overflows the container. If the content fits, no scrollbars appear. This is almost
always the preferred choice.In practice, use
auto for scrollable containers (modals, sidebars,
code blocks, dropdowns). Only use scroll if you specifically need
the scrollbar gutter to always be present — for example to prevent layout shift when content dynamically
changes height.
white-space: nowrap — prevents the text from
wrapping to the next line. Without this, the text wraps and there is no horizontal overflow to trigger
the ellipsis.overflow: hidden — clips the overflowing text at
the container's edge. Without this, the text would just spill outside visibly.text-overflow: ellipsis — adds the … character at the clipping point. Without overflow:hidden, this has no
visible effect.The container also needs a defined width (or the parent constrains it). Without a width boundary there is nothing to overflow against.
overflow: hidden:— Creates a new Block Formatting Context (BFC)
— Enables float clearing (used as clearfix)
— Allows programmatic scrolling via JavaScript (
element.scrollTop)— Can affect how absolutely positioned children are confined
overflow: clip:— Purely visual clipping — no BFC created
— No float-clearing side effects
— No programmatic scrolling possible
— Content is clipped but the element's layout is unaffected
Use
clip when you want pure visual clipping with no unintended side
effects on layout (e.g., clipping decorative elements that should not create a new formatting context).
When you write:
overflow-x: hidden;overflow-y: visible; ← this gets changed to autoThe browser silently changes the
visible axis to auto. This is why you might set overflow-y: visible and expect a dropdown to escape the container, but
it still gets clipped — because overflow-x: hidden on the same
element forced it to auto.Solution: move the dropdown outside the overflowing container, or use a different positioning strategy.
overflow-wrap: break-word — breaks a word only
as a last resort when it would otherwise overflow the container. The browser first tries natural
word break positions (spaces, hyphens). Only when no natural break is found does it break mid-word. This
produces the most natural-looking text.word-break: break-all — allows breaking at any
character at the end of a line, including within words at non-natural positions. More aggressive
— it will break even when natural breaks are available. Can make text harder to read but guarantees it
fits.Recommendation: use
overflow-wrap: break-word globally for body text
and user-generated content (URLs, emails, names). Use word-break: break-all only for fixed-width technical strings like hash
codes or API keys.
text-overflow: ellipsis to work?overflow-x: hidden
and overflow-y: visible?overflowcontrols what happens when content exceeds its container. Requires a defined width/height to take effect.- 5 values:
visible(default, spills out),hidden(clips + BFC),scroll(always scrollbars),auto(scrollbars when needed ← preferred),clip(clips, no BFC). overflow-x/overflow-ycontrol each axis independently. Cannot mixvisiblewith a non-visible value — the visible axis becomesauto.- Ellipsis needs all three:
white-space: nowrap + overflow: hidden + text-overflow: ellipsis. - Multi-line clamp:
display: -webkit-box; -webkit-line-clamp: N; -webkit-box-orient: vertical; overflow: hidden. overflow-wrap: break-word— breaks long words only as last resort.word-break: break-all— breaks at any character (aggressive).- Scroll snap:
scroll-snap-type: x mandatoryon container +scroll-snap-align: starton children = polished carousel. - BFC side effect:
hidden/scroll/autocreate a Block Formatting Context — useful for float clearing, but also whyoverflow: hiddenclips dropdown menus and tooltips.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.