box-sizing & Overflow

Two properties control how boxes behave at their edges: box-sizing decides whether padding and borders are included in an element's width, and overflow decides what happens when content is too big for its box.

content-box vs border-box

Try it yourself
Result
  • content-box (the default): width is the content only; padding and border are added.
  • border-box: width includes padding and border; the content shrinks to fit.

border-box is far more intuitive — a 50%-wide column with padding stays 50% wide — so nearly every project applies it globally:

CSS reset.css
*,
*::before,
*::after {
  box-sizing: border-box;
}

Why it matters for layouts

Try it yourself
Result

Overflow

When content doesn't fit — a fixed height, a long word, a wide image — it overflows. The overflow property controls what happens:

Try it yourself
Result
ValueBehaviour
visibleContent draws outside the box (default)
hiddenClipped; still scrollable by script
clipClipped with no scrolling at all
scrollAlways shows scrollbars
autoScrollbars only when needed — usually the best choice

overflow-x and overflow-y control each direction separately — for example, a horizontally scrolling table wrapper (overflow-x: auto).

Text overflow and ellipsis

Try it yourself
Result

The single-line ellipsis needs all three properties. Multi-line clamping uses line-clamp (with the long-standing -webkit- prefixed form for compatibility).

Long words and URLs

Try it yourself
Result

overflow-wrap: anywhere lets long strings break where necessary; hyphens: auto hyphenates words properly (the element needs a lang attribute).

Scroll containers and stacking

Setting overflow to anything other than visible turns an element into a scroll container. This clips children such as dropdown menus and tooltips, and it breaks position: sticky for elements inside. If a menu is mysteriously cut off, look for an ancestor with overflow: hidden. In newer browsers, overflow: clip clips without creating a scroll container.

Try it yourself

Try it yourself
Result