Overflow and scrolling
Overview
Sooner or later, content won’t fit its box: a long word in a narrow column, a table on a phone, a list taller than its panel. Overflow is what happens next. By default CSS lets content spill out rather than hide it — because hidden content is lost content. This lesson covers how to control overflow on purpose.
The overflow property
| Value | Behaviour |
|---|---|
visible |
content spills outside the box (default) |
hidden |
content is clipped; the box is still scrollable by code and by focus |
clip |
content is clipped; no scrolling at all |
scroll |
scrollbars always shown (on systems that display them) |
auto |
scrollbars appear only when content overflows |
.panel { max-height: 20rem; overflow: auto; } /* a scrollable panel */
.avatar { overflow: hidden; border-radius: 50%; } /* clip an image to a circle */Use overflow-x and overflow-y (or overflow-inline / overflow-block) to control each direction separately.
Scroll containers
Any value other than visible and clip makes the element a scroll container. That matters because:
position: stickysticks relative to the nearest scroll container — anoverflow: hiddenancestor can stop a sticky header from working (see Fixed and sticky),- it creates a block formatting context, which contains floats and stops child margins escaping.
overflow: clip was added for cases where you only want to clip, without creating a scroll container.
Don’t hide overflow on the page to “fix” horizontal scrolling.
body { overflow-x: hidden; }hides the symptom but not the cause — something is too wide, and its content may be cut off. Find the wide element instead (see Debugging layout problems).
Text that doesn’t fit
Long words and URLs
A long URL or word can force a box wider than its container. Let it break:
.comment { overflow-wrap: anywhere; } /* break long words only when needed */overflow-wrap: break-word does the same for overflowing words but doesn’t reduce the box’s min-content size, so in flex and grid items anywhere is usually what you want. hyphens: auto (with a lang attribute on the page) adds proper hyphenation.
Ellipsis
To truncate text with “…”:
.title {
white-space: nowrap; /* keep it on one line */
overflow: hidden; /* clip what doesn't fit */
text-overflow: ellipsis; /* show … at the cut */
}All three are required.
In a flex item? Flex items won’t shrink below their content’s width by default (
min-width: auto), so the no-wrap text widens the item instead of truncating. Addmin-inline-size: 0to the item. You’ll learn why in flex-grow, flex-shrink and flex-basis.
For multiple lines, use line clamping. An unprefixed line-clamp property is on its way, but today the -webkit- version is the one that works everywhere:
.excerpt {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3; /* the prefixed form works in every browser */
overflow: hidden;
}Truncate only text that’s available elsewhere (on the full page, in a tooltip) — hidden text is invisible to sighted users.
Scrollable regions
Horizontally scrolling rows are common for carousels and wide tables:
.table-wrapper { overflow-x: auto; }Wrap the table in a scrolling <div>, rather than making the whole page scroll. For keyboard users to scroll it, the region must be focusable if it contains nothing focusable — add tabindex="0", a role="region" and an aria-label describing it.
Scroll snap
Scroll snapping makes a scroller settle at defined positions, like a carousel:
.carousel {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory; /* snap on the x axis; mandatory = always snap */
}
.carousel > * {
flex: 0 0 80%;
scroll-snap-align: center; /* each item snaps to the centre */
}Use proximity instead of mandatory for long content that users should be able to stop anywhere in.
Containing scroll
When a scrollable panel reaches its end, the scroll normally continues to the page behind (“scroll chaining”). Stop it with:
.modal-body { overscroll-behavior: contain; }Stable scrollbars
On systems with classic scrollbars, a scrollbar appearing (when content grows) takes up space and shifts the layout sideways. Reserve the space in advance:
html { scrollbar-gutter: stable; }You can also style scrollbars with scrollbar-width: thin and scrollbar-color: #9a9ea7 transparent — but keep them visible and usable.
Smooth scrolling
html { scroll-behavior: smooth; } /* anchor links scroll smoothly… */
@media (prefers-reduced-motion: reduce) {
html { scroll-behavior: auto; } /* …unless the user prefers less motion */
}
:target { scroll-margin-top: 5rem; } /* leave room for a sticky header */scroll-margin-top is a small but valuable detail: when a link jumps to a heading, the heading won’t hide behind your sticky header.
The default is right:
overflow: visiblemakes problems visible. Clip or scroll only where you’ve designed for it, and fix the cause of unexpected overflow instead of hiding it.
A scrollable course row
Build a horizontal .row of .course cards (each 14rem wide) that scrolls sideways when there are too many to fit, with no vertical scrollbar. Cards should snap so one always lines up with the left edge after scrolling. Each card’s .title must stay on one line, ending with an ellipsis (…) if it’s too long. Finally, make sure the page itself never scrolls horizontally.
<div class="row">
<article class="course"><h3 class="title">CSS layout from the ground up</h3></article>
<article class="course"><h3 class="title">Grid</h3></article>
<article class="course"><h3 class="title">Responsive design in practice for real projects</h3></article>
<article class="course"><h3 class="title">Flexbox</h3></article>
<article class="course"><h3 class="title">Accessibility</h3></article>
</div>
<style>
* { box-sizing: border-box; }
.course { background: #eaf0fc; padding: 1rem; border-radius: 8px; }
/* your code here */
</style><div class="row">
<article class="course"><h3 class="title">CSS layout from the ground up</h3></article>
<article class="course"><h3 class="title">Grid</h3></article>
<article class="course"><h3 class="title">Responsive design in practice for real projects</h3></article>
<article class="course"><h3 class="title">Flexbox</h3></article>
<article class="course"><h3 class="title">Accessibility</h3></article>
</div>
<style>
* { box-sizing: border-box; }
.course { background: #eaf0fc; padding: 1rem; border-radius: 8px; }
.row {
display: flex;
gap: 1rem;
overflow-x: auto; /* scroll sideways only when needed */
overscroll-behavior-x: contain;
scroll-snap-type: x mandatory;
padding-block-end: 0.5rem; /* room for the scrollbar */
}
.course {
flex: 0 0 14rem; /* fixed-width cards that don't shrink */
min-inline-size: 0; /* let the no-wrap title shrink (see note) */
scroll-snap-align: start;
}
/*
Without min-inline-size: 0, flex items refuse to be narrower than their
content. The no-wrap title's full length would widen the card, and the
ellipsis would never appear.
*/
.title {
margin: 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>Summary
- Overflow happens when content is bigger than its box; by default it stays
visibleand spills out. overflow: autoadds scrollbars only when needed;hiddenandclipcut content off.- Any overflow value except
visibleandclipcreates a scroll container. - Single-line ellipsis needs
white-space: nowrap,overflow: hiddenandtext-overflow: ellipsis. - Scroll snap aligns scroll positions;
scrollbar-gutter: stablestops layout shifting when scrollbars appear.