Tutorials CSS Overflow
Ch 23 / 60
Chapter 23 Beginner 7 min read CSS

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.

What you'll learn
All overflow values: visible, hidden, scroll, auto, clip
overflow-x and overflow-y — per-axis control
text-overflow: ellipsis — truncate with …
overflow-wrap and word-break
Scrollable containers and scroll snap
Real-world patterns: code blocks, modals, carousels

All 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:

This text keeps going beyond the box boundary and spills outside — it does not get clipped or scrolled. The box border is visible but the content ignores it completely and overlaps whatever is below.
visible
Default. Content spills outside the box. No clipping.
This text keeps going beyond the box boundary but it gets clipped. You cannot scroll to see the rest. The extra content simply disappears at the box edge.
hidden
Clips content at the box edge. No scrollbars.
This text keeps going beyond the box boundary. Scrollbars always appear — even if the content fits. Scroll to see the rest of this text. The scrollbars are always rendered regardless of whether they are needed.
scroll
Always shows scrollbars. Scroll to see clipped content.
This text keeps going beyond the box boundary. Scrollbars appear only when needed — when content actually overflows. This is generally preferred over scroll because it doesn't show unnecessary scrollbars on short content.
auto ✓ preferred
Scrollbars only when content overflows.
This text is clipped like hidden but with a key difference: clip does not create a Block Formatting Context and does not allow any programmatic scrolling. It is purely visual clipping with no side effects.
clip
Clips like hidden but no BFC side effects.
CSS — overflow values
.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:

overflow-x: auto (horizontal scroll)
Card One
Card Two
Card Three
Card Four
Card Five
Card Six
Card Seven
overflow-y: auto (vertical scroll)
Item 1 — scroll down
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
CSS — overflow-x & overflow-y
/* 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. */
The overflow axis quirk
If you set one axis to 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.

no truncation (default)
This is a very long title that overflows its container without any indication
text-overflow: ellipsis
This is a very long title that overflows its container and gets an ellipsis
text-overflow: clip
This is a very long title that overflows its container and gets hard clipped
Multi-line clamp (3 lines)
This is a much longer paragraph that should be clamped to exactly three lines of text. When it exceeds three lines the content gets cut off and an ellipsis appears at the end of the third line to indicate more content exists.
CSS — 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:

normal (default)
https://www.k2infocom.com/tutorial/css/css-overflow.html?tracking=veryLongParameterThatWillOverflow
overflow-wrap: break-word
https://www.k2infocom.com/tutorial/css/css-overflow.html?tracking=veryLongParameterThatWillOverflow
word-break: break-all
https://www.k2infocom.com/tutorial/css/css-overflow.html?tracking=veryLongParameterThatWillOverflow
CSS — overflow-wrap & word-break
/* 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:

Slide 1
Slide 2
Slide 3
Slide 4
Slide 5
Slide 6
CSS — Scroll snap carousel
/* ── 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:

const longFunctionName = (param1, param2, param3) => param1 + param2 + param3;
Code block horizontal scroll
overflow-x: auto; white-space: nowrap — code never wraps, scroll to read.
Modal content that can be very long. Scroll to see more. Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt.
Scrollable modal body
max-height: 80vh; overflow-y: auto — modal grows then scrolls.
This is a very long card title that gets truncated
Card description that is limited to exactly two lines of text and then shows an ellipsis when it exceeds that limit
Card title + description clamp
Single-line ellipsis for title, -webkit-line-clamp:2 for body.
Hover button
Tooltip — overflow: visible on parent
Tooltip breaks out of container
Tooltips need parent with overflow: visible (the default) — overflow: hidden would clip them.
Hover to zoom
Image zoom on hover
overflow: hidden on wrapper clips the scaling image — the zoom stays inside the box.
Menu ▾
Option 1
Option 2
Dropdown from overflowing parent
Parent must NOT have overflow: hidden — the dropdown must be able to escape its container.
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering overflow, text-overflow, overflow-wrap, scroll-snap and all CSS layout properties.
Download PDF

Try It Yourself

Change overflow values, try different text-overflow settings, and experiment with the scroll snap carousel:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 23

Questions & Answers
Both create a scrollable container when content overflows, but they differ in when scrollbars are shown:

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.
All three of these properties must be applied together — omitting any one makes it not work:

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.
Visually they look identical — both clip content at the box edge without showing scrollbars. The differences are under the hood:

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).
This is a specification quirk in the CSS box model. You cannot have one axis overflow freely while the other is clipped — it would require the browser to maintain a contradictory rendering state.

When you write:
overflow-x: hidden;
overflow-y: visible; ← this gets changed to auto

The 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.
Both allow long words to break across lines, but they have different levels of aggressiveness:

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.
Multiple Choice Questions
Question 1 of 5
Which overflow value shows scrollbars only when content actually overflows?
Question 2 of 5
Which property is NOT needed for text-overflow: ellipsis to work?
Question 3 of 5
What happens when you set overflow-x: hidden and overflow-y: visible?
Question 4 of 5
Which CSS property limits a block of text to a maximum number of lines?
Question 5 of 5
Which CSS property breaks long unbreakable words only as a last resort?
Chapter Summary
  • overflow controls 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-y control each axis independently. Cannot mix visible with a non-visible value — the visible axis becomes auto.
  • 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 mandatory on container + scroll-snap-align: start on children = polished carousel.
  • BFC side effect: hidden/scroll/auto create a Block Formatting Context — useful for float clearing, but also why overflow: hidden clips dropdown menus and tooltips.
~7 min read
Beginner level
Chapter 23 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel