← Back to HTML & CSS, page 4

html-css-0067 · Easy fresher

What do the overflow values visible, hidden, scroll, auto and clip do?

overflow controls what happens to content bigger than its box: visible spills out, hidden cuts it, scroll always shows scrollbars, auto shows them when needed, and clip cuts it without making the box scrollable. If one axis is not visible, the other visible axis becomes auto.

Full answer

Overflow happens when content is bigger than its box. The overflow property says what to do with the extra part.

  • visible: the default. The extra content spills outside the box and is drawn there.
  • hidden: the extra content is cut off. The box can still be scrolled by code, for example with scrollTop, but the user gets no scrollbar.
  • scroll: the box always shows scrollbars, even when nothing overflows.
  • auto: scrollbars appear only when content overflows. This is usually the best choice for scrolling areas.
  • clip: the extra content is cut off like hidden, but the box is not a scroll container and cannot be scrolled at all, even by code.

A box needs a limited size, for example a height or max-height, for vertical overflow to happen. Without it, the box simply grows.

overflow-x and overflow-y can be set separately, with a twist. If one is visible and the other is not, visible is treated as auto. So overflow-x: hidden alone also makes the vertical direction scrollable when content overflows. A box with any overflow value other than visible or clip also becomes a new block formatting context, and it becomes the scroll container that position: sticky looks at.

Each box shows 60px of a 200px tall child. The visible box lets the child spill over the boxes below. The auto box adds a vertical scrollbar. The xonly box was given only overflow-x: hidden, yet its computed overflow-y is auto. The clip box cannot be scrolled even by a script.

Think before adding overflow: hidden to fix a layout problem. It can cut off shadows and focus rings, and it can break sticky positioning for children.

html
<style>
  .box { width: 120px; height: 60px; border: 1px solid #333; margin-bottom: 8px; font: 14px/20px sans-serif; }
  .tall { height: 200px; background: #eef; }
  #hidden { overflow: hidden; }
  #scroll { overflow: scroll; }
  #auto   { overflow: auto; }
  #clip   { overflow: clip; }
  #xonly  { overflow-x: hidden; }
</style>
<div class="box" id="visible"><div class="tall"></div></div>
<div class="box" id="hidden"><div class="tall"></div></div>
<div class="box" id="scroll"><div class="tall"></div></div>
<div class="box" id="auto"><div class="tall"></div></div>
<div class="box" id="clip"><div class="tall"></div></div>
<div class="box" id="xonly"><div class="tall"></div></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • visible is the default and lets content spill out.
  • hidden cuts off the content; scroll and auto add scrollbars.
  • clip cuts content but the box cannot be scrolled at all.
  • A visible axis becomes auto when the other axis is not visible.
  • Non-visible overflow creates a scroll container, which affects sticky positioning.

Common mistakes

  • Adding overflow: hidden to hide a horizontal scrollbar and cutting off dropdown menus or focus rings.
  • Expecting overflow-x: hidden alone to leave overflow-y visible. The computed value becomes auto.
  • Using overflow: scroll everywhere. It shows empty scrollbars on systems with always-visible scrollbars. Use auto.

Follow-ups

  • Why does overflow: hidden on a parent break position: sticky on its child?
  • What is the difference between hidden and clip?

Tips

  • Say that auto is the normal choice for scroll areas, and explain clip as the version that cannot scroll.
  • Mention sticky when talking about overflow. It is the common side effect interviewers ask about.