Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 139 Β· The Box Model

Overflow

Control what happens when content is too big for its box with CSS overflow. Compare visible, hidden, clip, scroll and auto, set each axis, and style scroll areas.

Sooner or later, content won't fit in its box. A user types a very long name, a code sample is wider than the screen, a list grows taller than its panel. When a box has a fixed size and its content is bigger, the extra part is called overflow, and CSS lets you decide what happens to it.

You can let it spill out, cut it off, or turn the box into a scrollable area. This lesson covers each overflow value, controlling the two axes separately, the difference between hidden and the newer clip, and a few habits that keep scroll areas pleasant to use.

When does overflow happen?

Overflow only happens when a box is smaller than its content. Most boxes grow to fit what's inside them, so overflow usually involves one of these:

  • A fixed height or max-height on a box with lots of text.
  • A fixed width with content that can't wrap: a long URL, a wide image, a table or a <pre> block.
  • Flex and grid items that refuse to shrink below their content's size.

By default, overflowing content is simply drawn outside the box. It doesn't push anything else; it just overlaps whatever comes next.

The overflow values

The overflow property has five values:

  • visible (the default): content spills out of the box and is fully shown.
  • hidden: content is clipped at the padding edge. No scrollbars appear, though the box can still be scrolled with code or by focusing something inside it.
  • clip: content is clipped, and the box cannot be scrolled at all, not even by code.
  • scroll: content is clipped and scrollbars are always shown, even when nothing overflows.
  • auto: content is clipped and scrollbars appear only when needed.
The five overflow values

In the visible box, the text runs over the next heading. hidden and clip look identical here. scroll and auto look similar too, but scroll shows a horizontal scrollbar track even though nothing overflows sideways (on systems with overlay scrollbars, such as macOS and phones, the tracks may stay hidden until you scroll).

Prefer auto over scroll. Scrollbars that appear for no reason look broken. Use auto unless you specifically want space reserved for a scrollbar; for that, the scrollbar-gutter property (shown below) is a cleaner tool.

Controlling each axis

overflow is a shorthand for overflow-x and overflow-y. With two values, the first is for x (horizontal), the second for y (vertical):

CSS
.code-block {
  overflow-x: auto;     /* scroll sideways if lines are too long */
}

.panel {
  overflow: hidden auto; /* x: hidden, y: auto */
}

There's also overflow-inline and overflow-block, the logical versions, which follow the writing mode.

There is one quirk worth knowing. You can't mix visible on one axis with a scrolling value on the other. If one axis is hidden, scroll or auto, a visible on the other axis is quietly turned into auto. So overflow-x: visible; overflow-y: auto; actually behaves like auto auto. The exception is clip: clip combined with visible works as written.

A wide code block that scrolls sideways

Remove overflow-x: auto and the dark block pokes out of the card. This is the standard way to handle code blocks, wide tables and image strips.

hidden vs clip

Both values cut off content at the box's edge, and they look the same. The differences are behind the scenes:

  • Scrolling. An overflow: hidden box is still a scroll container. JavaScript can scroll it, and focusing a link inside it (with Tab) can scroll hidden content into view, which sometimes causes odd jumps. An overflow: clip box can't be scrolled at all.
  • Sticky positioning. A scroll container becomes the reference box for any position: sticky descendants. A stray overflow: hidden on a wrapper is the classic reason a sticky header suddenly stops sticking. clip doesn't create a scroll container, so sticky elements keep working with the page's scroll.
  • Single axis. As you saw, clip can be combined with visible on the other axis.
  • Clip margin. overflow-clip-margin lets clipped content extend a little past the edge, like overflow-clip-margin: 8px. It works only with clip.

overflow: clip is supported in all current browsers. When you only want to hide content that pokes out, such as an image in a rounded corner, clip is usually the better choice. Use hidden when you need programmatic scrolling, as in a JavaScript carousel.

Any overflow value other than visible (and clip) also creates a new block formatting context. That's why an old trick used overflow: hidden to make a parent contain its floated children. Today, display: flow-root does that job without clipping anything; see Floats.

Scrollable areas that feel good

When you make a box scrollable, a few small rules improve it a lot.

Use max-height, not height. A panel with max-height: 300px; overflow-y: auto; stays compact when the content is short and scrolls only when it grows.

Reserve the scrollbar's space. On systems with classic scrollbars, content shifts sideways when a scrollbar appears. scrollbar-gutter: stable keeps the space reserved, so nothing moves.

Stop scroll chaining. When you reach the end of a scrollable panel, the browser starts scrolling the page behind it. overscroll-behavior: contain keeps the scroll inside the panel. You'll meet both properties again in Scroll Snap.

Make it reachable by keyboard. Keyboard users can only scroll an area that can receive focus. Chromium and Firefox make scroll containers without focusable children focusable automatically, but adding tabindex="0" and an accessible name with aria-label keeps it reliable everywhere.

Style the scrollbar lightly. scrollbar-width (auto, thin or none) and scrollbar-color (thumb color, then track color) are the standard properties. Don't hide scrollbars entirely on content people need to scroll; they are an important visual hint.

A polished scrolling panel

Scroll the panel with the mouse, then Tab into it and use the arrow keys. Change the loop to create only 3 items, and the panel shrinks to fit with no scrollbar.

Overflowing text

Overflow also applies to a single line of text that is too long for its box. Combined with white-space: nowrap and text-overflow: ellipsis, overflow: hidden gives the familiar "This is a very long tit…" effect:

CSS
.file-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

That trick, plus wrapping long words and clamping text to a number of lines, is covered in Wrapping, Overflow and Hyphens.

Check your understanding

Quick check Β· 5 questions

of 5 answered You scored / 5 Β· perfect!

  1. 1. Which value shows scrollbars only when the content actually overflows?

    auto adds scrollbars only when needed. scroll always shows them.
  2. 2. What is the computed behavior of overflow-x: visible; overflow-y: auto;?

    visible can't be paired with a scrolling value, so it is turned into auto. Only clip mixes with visible.
  3. 3. A sticky header stopped sticking after you added overflow: hidden to a wrapper. Why?

    Sticky positioning works within the nearest scroll container. hidden creates one; clip does not, so switching to clip usually fixes it.
  4. 4. Which property stops scrolling from passing to the page when a panel reaches its end?

    overscroll-behavior: contain prevents scroll chaining to the parent or page.
  5. 5. You want a list panel that is short when it has few items and scrolls when it has many. What do you use?

    max-height lets the panel shrink to fit, and overflow-y: auto adds a scrollbar only when the content is taller than 300px.

Summary

  • Overflow is content that doesn't fit a box, usually because of a fixed size or unbreakable content.
  • visible (default) shows it; hidden and clip cut it off; scroll and auto make the box scrollable.
  • overflow-x and overflow-y control each axis; visible paired with a scrolling value becomes auto.
  • clip can't be scrolled and doesn't create a scroll container, so it doesn't break position: sticky.
  • Good scroll areas use max-height, scrollbar-gutter, overscroll-behavior, keyboard focus and light scrollbar styling.
  • Single-line text overflow uses overflow: hidden with white-space: nowrap and text-overflow: ellipsis.

Next: Styling Text, the first lesson of the Text and Fonts chapter.