TExcelTAD
Home › CSS › CSS Overflow

CSS Overflow

Sometimes content is bigger than the box that contains it — a long paragraph in a fixed-height card, a wide table in a narrow column. The overflow property decides what happens to the part that doesn't fit.

Overflow values

Value Effect
visible (default) Content spills outside the box and may overlap other things
hidden Extra content is clipped; no scrollbars
clip Like hidden, but even scripts can't scroll it
scroll Clipped, and scrollbars are always shown
auto Clipped, scrollbars appear only when needed

Overflow only matters when the box has a restricted size (a height, max-height, or width).

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; gap: 14px; flex-wrap: wrap; }
  .box { width: 130px; height: 90px; padding: 6px; border: 2px solid #5e35b1; background: #ede7f6; font-size: 14px; }
  .v { overflow: visible; }
  .h { overflow: hidden; }
  .s { overflow: scroll; }
  .a { overflow: auto; }
  b { color: #5e35b1; }
</style>
</head>
<body>
  <div class="box v"><b>visible</b> — this text is too long for the box, so it spills out the bottom.</div>
  <div class="box h"><b>hidden</b> — this text is too long for the box, so the rest is simply cut off.</div>
  <div class="box s"><b>scroll</b> — this text is too long for the box, so scrollbars appear.</div>
  <div class="box a"><b>auto</b> — this text is too long for the box; scrollbar only if needed.</div>
</body>
</html>

Tip: Prefer auto over scroll. scroll shows empty scrollbar tracks even when nothing overflows (on systems with permanent scrollbars).

overflow-x and overflow-y

You can control each axis separately. A common case: a horizontal strip of cards that scrolls sideways.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .strip { display: flex; gap: 10px; overflow-x: auto; overflow-y: hidden; padding: 10px; background: #263238; }
  .strip div { flex: 0 0 110px; height: 90px; border-radius: 10px; color: white; display: grid; place-items: center; font-size: 26px; }
  .strip div:nth-child(odd) { background: #ff7043; }
  .strip div:nth-child(even) { background: #29b6f6; }
</style>
</head>
<body>
  <p>Scroll sideways:</p>
  <div class="strip">
    <div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div>
  </div>
</body>
</html>

Text overflow and ellipsis

For single-line text that's too long, combine three properties to show "…":

css
.truncate {
  white-space: nowrap;      /* keep on one line */
  overflow: hidden;         /* clip the rest */
  text-overflow: ellipsis;  /* show … */
}

For multiple lines, -webkit-line-clamp (supported in all modern browsers) cuts after N lines:

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .card { width: 260px; padding: 10px; margin-bottom: 10px; background: #fff3e0; border-left: 5px solid #fb8c00; }
  .one { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .three { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; }
</style>
</head>
<body>
  <div class="card one">A very long product title that definitely does not fit on one line</div>
  <div class="card three">This description is clamped to three lines. Anything after the third line is hidden and replaced by an ellipsis, which keeps card heights consistent in a grid of products.</div>
</body>
</html>

Overflow and layout side effects

Setting overflow to anything except visible creates a block formatting context. That has two useful side effects: the box contains its floated children, and its margins no longer collapse with its children's. It also breaks position: sticky for descendants and clips absolutely positioned children — watch out when tooltips or dropdowns get cut off.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; gap: 30px; padding: 20px; }
  .wrap { position: relative; width: 140px; height: 60px; background: #e8f5e9; border: 2px solid #43a047; padding: 6px; }
  .clip { overflow: hidden; }
  .pop { position: absolute; top: 45px; left: 40px; background: #212121; color: white; padding: 6px 10px; border-radius: 6px; white-space: nowrap; }
</style>
</head>
<body>
  <div class="wrap">visible<div class="pop">Tooltip shows fully</div></div>
  <div class="wrap clip">hidden<div class="pop">Tooltip gets cut</div></div>
</body>
</html>

Test yourself

Which overflow value shows scrollbars only when content actually overflows?

  1. visible
  2. scroll
  3. auto
  4. hidden

Which three properties together produce a single-line ellipsis?

  1. overflow: auto; text-wrap: none; ellipsis: on
  2. white-space: nowrap; overflow: hidden; text-overflow: ellipsis
  3. display: block; overflow: clip; content: '...'
  4. line-clamp: 1 only

Why might an absolutely positioned tooltip be cut off?

  1. Its z-index is too low
  2. An ancestor has overflow: hidden
  3. It uses position: absolute
  4. It has white-space: nowrap