CSS Overflow, Truncation, and Scrollbar Control
Content does not politely fit. A title runs long, a URL has no spaces, a card grows a paragraph on hover, a code sample is wider than its column â and the box it sits in has to decide what happens when the content's minimum size exceeds the container's size. That decision is the overflow property, and it cascades in a way that surprises people: overflow-x and overflow-y are separate axes, but if one of them computes to visible while the other computes to auto, scroll, or hidden, the visible one is coerced into auto. The practical consequence is that a container with overflow-x: hidden silently becomes scrollable vertically even if you never declared it. This lesson covers choosing an overflow mode, the truncation recipes every card component needs, safe word-breaking for user-generated strings, and scrollbar styling that works beyond WebKit.
Choosing an overflow mode
visible is the default: content spills out of the box and paints over neighbors. hidden clips at the padding box â content beyond the edge is unreachable, and no scrollbars appear. clip also clips, but unlike hidden it does not create a scroll container and ignores scroll-margin, which makes it the correct choice when you want a hard cut with no scrolling machinery at all. scroll always shows scrollbars (or reserved gutter space) whether or not content overflows â useful when a media frame must keep its layout stable as content loads. auto shows scrollbars only when needed, which is the default choice for app panels and modals. Beyond the keywords, the overflow-x / overflow-y pair lets you allow horizontal scrolling while clipping vertically, a common requirement for data tables:
| Value | Scrolls | Scrollbars shown | Creates scroll container | Notes |
|---|---|---|---|---|
| visible | No, content spills | No | No | Default; may overlap siblings |
| hidden | No, clipped | No | Yes | Content unreachable; chaining blocked |
| clip | No, clipped | No | No | No scroll machinery at all |
| scroll | Yes | Always | Yes | Stable layout during load |
| auto | Yes | When needed | Yes | Default for panels and modals |
.panel {
overflow-y: auto;
max-height: min(70vh, 32rem);
overscroll-behavior: contain;
}
.data-table {
overflow-x: auto;
border: 1px solid #e2e8f0;
border-radius: 12px;
}
.data-table table {
min-width: 48rem;
}
.code-sample {
overflow-x: auto;
white-space: pre;
font-family: ui-monospace, monospace;
}
.avatar-crop {
overflow: clip;
border-radius: 50%;
}
overscroll-behavior: contain on the panel stops scroll chaining â scrolling to the end of a modal's body no longer scrolls the page behind it, a small detail that fixes a genuinely disorienting bug. Note the interaction with position: sticky: sticky elements stick within their scroll container, so a sticky table header belongs inside the same element that owns overflow-y: auto.
The single-line ellipsis recipe
One line, three properties: white-space: nowrap prevents wrapping, overflow: hidden clips at the box, text-overflow: ellipsis replaces the clipped tail with three dots. The order matters little, but all three must be present, and the ellipsis only appears when there is genuinely something to truncate â the property is a clipping decoration, not a limit:
.card-title {
font-size: 1.1rem;
font-weight: 600;
max-inline-size: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.title-row {
display: flex;
align-items: baseline;
gap: 0.75rem;
min-inline-size: 0;
}
.title-row .card-title {
flex: 1;
min-inline-size: 0;
}
The min-inline-size: 0 line is the trap everyone hits: flex and grid items default to min-width: auto, meaning they refuse to shrink below their content's minimum size. A nowrap title inside a flex row therefore stretches its row instead of truncating â the ellipsis never fires, and the layout overflows. Setting the item's minimum to zero lets it shrink, at which point the three properties above do their job. The same applies to grid children: minmax(0, 1fr) beats 1fr for columns containing long text.
Multi-line clamping
A one-line ellipsis is often too aggressive for card summaries. The multi-line recipe uses the -webkit- line-clamp syntax â originally WebKit-only, now supported by every major engine and finally standardized without the prefix:
.card-summary {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
line-height: 1.55;
max-height: calc(3 * 1.55em);
}
.card-summary.is-expanded {
-webkit-line-clamp: unset;
max-height: none;
display: block;
}
Two things worth knowing. The clamp needs display: -webkit-box together with the orient declaration; overflow: hidden provides the actual clipping, and text-overflow: ellipsis is ignored in this mode because the box decoration scheme renders its own ellipsis. The standard line-clamp property now covers plain multi-line clamping without the box display, but the -webkit- form remains the compatibility baseline you will see in production. Pair the clamp with an expanded state â as above â so users can still read the full text, because truncation that hides content permanently is an accessibility problem, not just a design choice.
Breaking long words, URLs, and codes safely
Unbroken strings â user-supplied URLs, base64 blobs, error codes â overflow any container that wraps text. overflow-wrap: break-word allows a break anywhere within a word only when the word cannot fit on a line of its own; normal words still wrap at spaces, which is the behavior you want for prose. Its stricter sibling overflow-wrap: anywhere considers soft wrap opportunities everywhere, which affects intrinsic min-content sizing â text can shrink the container further. word-break: break-all breaks every word aggressively, useful for character grids or CJK-adjacent contexts but ugly for Latin prose. The modern, semantically cleaner pairing is word-break: pretty for display text, which avoids awkward mid-word breaks in the last line, plus hyphens: auto with a lang attribute for justified columns:
.comment-body {
overflow-wrap: anywhere;
word-break: normal;
}
.error-detail {
overflow-wrap: break-word;
font-family: ui-monospace, monospace;
}
.hero-title {
word-break: break-word;
text-wrap: balance;
hanging-punctuation: first;
}
.long-url a {
overflow-wrap: anywhere;
}
text-wrap: balance on headings distributes lines evenly for a more polished rag, and text-wrap: pretty on body text reduces hyphenation and ragginess in the final line â typographic refinements that cost one declaration each.
Styling scrollbars across engines
WebKit and Chromium expose ::-webkit-scrollbar pseudo-elements for track, thumb, and hover states; Firefox historically used scrollbar-width and scrollbar-color, now the standardized route:
.panel {
scrollbar-width: thin;
scrollbar-color: #94a3b8 #f1f5f9;
}
.panel::-webkit-scrollbar {
width: 10px;
height: 10px;
}
.panel::-webkit-scrollbar-track {
background: #f1f5f9;
border-radius: 8px;
}
.panel::-webkit-scrollbar-thumb {
background: #94a3b8;
border-radius: 8px;
border: 2px solid #f1f5f9;
}
.panel::-webkit-scrollbar-thumb:hover {
background: #64748b;
}
Style scrollbars with restraint: fat decorative thumb styling reduces the drag target, and hidden scrollbars (scrollbar-width: none) hurt discoverability unless the region has an obvious scroll affordance â the slide-in carousel pattern covered in the scroll snap lesson. Overlay scrollbars on mobile never take layout space, so horizontal measurements that assume a visible vertical scrollbar will shift between desktop and touch; prefer 100% widths over viewport-unit subtraction to sidestep the difference entirely.
contain: paint and content-visibility: the modern cousins
Overflow is one member of a property family that controls how much a box isolates itself. contain: paint applies paint containment â descendants clip to the box's padding edge and establish a containing block for fixed and absolute positioning, like overflow without creating a scrollport. That combination makes it a favorite for badges and overlays: nothing paints outside the shell, and an inner absolutely positioned element anchors predictably even if an ancestor scrolls. Related values contain: layout and contain: style isolate layout and style scoping; contain: size (which you met as part of container queries) lets the box size independently of content.
content-visibility: auto tackles the opposite axis â work the browser does not have to do. It lets the engine skip rendering an off-screen subtree while preserving its accessibility and findability, and content-visibility: hidden skips rendering while also skipping the subtree's content for find-in-page until it is restored. Applied to long feeds or collapsible sections, they can cut initial rendering cost meaningfully:
.feed-item {
content-visibility: auto;
contain-intrinsic-size: auto 12rem;
}
.badge {
contain: paint;
position: relative;
}
The contain-intrinsic-size line is not optional garnish: without it, skipped items collapse to zero height, and the scrollbar jumps wildly as the user scrolls through unrendered territory. Remember the family resemblance to overflow when something misbehaves â any property that establishes containment or a scroll container can change how sticky descendants behave and which element clips a stray pseudo-element.
Common Mistakes
Ellipsis never appears on a title inside a flex or grid row
Fix: The item will not shrink below its content width â add min-inline-size: 0 (or minmax(0, 1fr) for grid columns) so the clip can fire
A panel declared overflow-y: auto starts scrolling horizontally
Fix: The axes interact: setting one axis to auto coerces a visible other axis to auto â declare both axes explicitly or use clip on the axis that must never scroll
Truncated summaries hide essential content with no way to read it
Fix: Pair the clamp with an expand control or an accessible full-text target so content is never permanently unreachable
Long URLs overflow cards despite overflow: hidden
Fix: Clipping is not wrapping â add overflow-wrap: anywhere so the string can break across lines instead of being cut off
Best Practices
- Give flex and grid children min-inline-size: 0 before relying on text truncation
- Use overscroll-behavior: contain on nested scroll panels to stop scroll chaining
- Prefer overflow: clip for pure cropping where no scrolling machinery is wanted
- Set scrollbar-width and scrollbar-color for Firefox alongside ::-webkit-scrollbar for Chromium
- Balance headings with text-wrap: balance and provide expanded states for clamped text
Interview Questions
Q1: What is the difference between overflow: hidden and overflow: clip?
hidden creates a scroll container: content is unreachable by pointer scrolling, but programmatic scrolling and scroll-related behavior still apply, and sticky descendants stick to it. clip cuts content at the box edge with no scroll container at all â no scrolling, no scroll-driven side effects, which makes it cleaner for pure cropping.
Q2: Why does overflow-x: hidden also affect the vertical axis?
When one axis computes to a scrollable or hidden value and the other computes to visible, the visible axis is coerced to auto per the specification. So overflow-x: hidden makes overflow-y effectively auto, and the container can scroll vertically â declare both axes explicitly when you need precise control.
Q3: List the three properties required for a single-line ellipsis and the common reason it fails.
overflow: hidden, text-overflow: ellipsis, and white-space: nowrap. It commonly fails inside flex or grid because items default to min-width: auto and refuse to shrink below content width, so nothing is ever clipped; min-inline-size: 0 (or minmax(0, 1fr)) on the item fixes it.
Key Takeaways
- Overflow modes trade reachability for stability: hidden and clip cut content, scroll and auto make it reachable, visible spills it
- The axes interact â one non-visible axis coerces a visible partner to auto
- Single-line ellipsis needs three properties plus min-inline-size: 0 in flex/grid contexts; multi-line uses line-clamp with an expanded state
- overflow-wrap: anywhere or break-word handles URLs and codes that have no spaces
- Cross-browser scrollbar styling pairs Firefox's scrollbar-width/color with WebKit's ::-webkit-scrollbar pseudo-elements