Overflow and scrolling

Topic 3 of 4 in Normal flow7 min readBeginnerFree course

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

Four boxes with too much content. overflow: visible — the content spills out of the box (the default). overflow: hidden — clipped, but still scrollable by code. overflow: scroll — always shows scrollbars. overflow: auto — scrollbars only when needed. Prefer auto for scroll areas. overflow: clip clips without creating a scroll container, so sticky still works inside.

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
CSS
.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: sticky sticks relative to the nearest scroll container — an overflow: hidden ancestor 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:

CSS
.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 “…”:

CSS
.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. Add min-inline-size: 0 to 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:

CSS
.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:

CSS
.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:

CSS
.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:

CSS
.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:

CSS
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

CSS
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: visible makes problems visible. Clip or scroll only where you’ve designed for it, and fix the cause of unexpected overflow instead of hiding it.

Exercise

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.

HTML
<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>

Summary

  • Overflow happens when content is bigger than its box; by default it stays visible and spills out.
  • overflow: auto adds scrollbars only when needed; hidden and clip cut content off.
  • Any overflow value except visible and clip creates a scroll container.
  • Single-line ellipsis needs white-space: nowrap, overflow: hidden and text-overflow: ellipsis.
  • Scroll snap aligns scroll positions; scrollbar-gutter: stable stops layout shifting when scrollbars appear.