HTML & CSSEasy
What is overflow?
overflow controls what happens when an element's content is larger than its box: visible (default, spills out), hidden (clipped), scroll (always shows scrollbars), auto (scrollbars only when needed) and clip (clipped without creating a scroll container). It can be set per axis with overflow-x and overflow-y.
Values
visible: content draws outside the box.hidden: clipped; still scrollable from JavaScript; creates a scroll container (and a new block formatting context, so it contains floats).clip: clipped with no scroll container at all, so it does not breakposition: stickyin descendants.scroll: scrollbars always present.auto: scrollbars appear when needed; the usual choice for scrollable panels.
Related properties
text-overflow: ellipsis(withwhite-space: nowrap; overflow: hidden) truncates single lines with "…".-webkit-line-clamp/line-clamptruncates after N lines.overscroll-behavior: containstops scroll chaining to the page (useful for modals and chat panels).scrollbar-gutter: stablereserves scrollbar space to avoid layout shift.scroll-snap-typecreates carousel-style snapping.
Common gotchas
overflow: hiddenon an ancestor breaksposition: stickyand clips dropdowns and tooltips.- Setting one axis to
hiddenand the other tovisiblemakesvisiblecompute toauto. - Horizontal page overflow on mobile usually comes from a fixed-width element; find it with DevTools.
Code example
html
1<style>2 body { font-family: system-ui; }3 .box { width: 220px; height: 70px; border: 2px solid #0b1020; margin: 8px 0; padding: 6px; }4 .visible { overflow: visible; }5 .hidden { overflow: hidden; }6 .auto { overflow: auto; overscroll-behavior: contain; }7 .ellipsis { width: 220px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border: 2px solid #0b1020; padding: 6px; }8 .clamp { width: 220px; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; }9</style>10<div class="box visible">visible: This text is too long for the box so it spills out below the border and overlaps.</div>11<br>12<div class="box hidden">hidden: This text is too long for the box so the extra lines are clipped and never shown.</div>13<div class="box auto">auto: This text is too long for the box so a scrollbar appears and you can scroll to read everything here.</div>14<div class="ellipsis">ellipsis: A single line that is far too long to fit</div>15<p class="clamp">line-clamp: This paragraph is limited to two lines, and anything after the second line is replaced by an ellipsis automatically.</p>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.