Sub topic 11
Overflow: when content exceeds the box
This chapter assumes you know the content, padding, border, and margin areas, how width and height set the content box, and how box-sizing changes that calculation. It builds on the earlier chapters on the content area and on min and max dimensions. It also prepares you for block formatting contexts, which come next.
Why overflow happens
A box has a size, but its content does not have to fit. Overflow is content that extends past the edge of the box that contains it. CSS does not shrink the content or grow the box when you have fixed the size. By default it lets the content spill out and keeps drawing it.
Common causes:
- A fixed
heighton a box whose text wraps to more lines than expected. - A long unbroken string, such as a URL or a German compound word, that is wider than the container.
- An image, table, or
<pre>block with a fixed or intrinsic width larger than its parent. - Negative margins or positioned children that sit outside the parent's edges.
Overflow only occurs when the size is constrained. A block with height: auto grows to fit its content, so the problem usually starts when you set height yourself.
<style>
.note {
width: 200px;
height: 60px;
border: 2px solid #333;
padding: 8px;
box-sizing: border-box;
}
</style>
<div class="note">
This paragraph has far more text than a 60px-tall box can hold, so the
extra lines are drawn below the border.
</div>The border box here is 200px by 60px, so the content area is 180px by 40px. The text keeps flowing past the bottom padding edge and is painted over whatever follows the box. The overflowing text does not change the size of the box or push neighbouring elements away.
The overflow property
The overflow property controls what the browser does with content that does not fit in the padding box. The clipping edge is the padding edge, so the border is never clipped and the padding area is available to content.
| Value | Clips content | Scrollbars | Notes |
|---|---|---|---|
| `visible` | No | None | The default. Content paints outside the box. |
| `hidden` | Yes | None, but the box is still programmatically scrollable | Content is cut at the padding edge. |
| `clip` | Yes | None, and not scrollable at all | Like `hidden`, but creates no scroll container and no block formatting context. |
| `scroll` | Yes | Always shown | Scrollbar tracks appear even when nothing overflows. |
| `auto` | Yes | Only when needed | The usual choice for scrollable regions. |
A box with any value other than visible or clip is a scroll container. Scroll containers can be scrolled with the mouse wheel, keyboard, or touch, and hidden is no exception. Scripts can change scrollTop on an overflow: hidden element, and focusing an element inside it can scroll it. If you need content to be truly fixed in place, use overflow: clip.
.log {
height: 12rem;
overflow: auto;
border: 1px solid #999;
padding: 0.5rem;
}Controlling each axis
overflow is shorthand for overflow-x and overflow-y. One value sets both axes, and two values set x then y.
.table-wrapper {
overflow-x: auto;
overflow-y: visible; /* does not behave as written, see below */
}There is a trap in that example. If one axis is visible and the other is not, the visible axis computes to auto. The browser cannot let content spill out on one axis while clipping on the other. The wrapper above will be a scroll container on both axes. State both axes if you care about the result:
.table-wrapper {
overflow-x: auto;
overflow-y: hidden;
}The clip value does not have this restriction. overflow-x: clip with overflow-y: visible works as written.
Scrollbars take space
With overflow: scroll, or with auto when content overflows, classic scrollbars sit inside the box and reduce the content area. A 15px scrollbar on a 300px-wide box leaves 285px for content. Overlay scrollbars, common on macOS and mobile, take no space.
This causes layout shift: when content grows enough to trigger a scrollbar under overflow: auto, the content width suddenly shrinks and text rewraps. The scrollbar-gutter property reserves the space ahead of time.
.panel {
overflow: auto;
scrollbar-gutter: stable;
}With stable, the gutter is reserved whether or not a scrollbar appears. Use stable both-edges to reserve equal space on both sides so centred content stays centred.
What counts as overflow
The scrollable area is larger than the padding box in these ways:
- Child boxes that extend past the padding edge, including their border boxes. Margins at the far edge of the content do not always add to the scrollable area, and behaviour at the inline-end and block-end edges has varied between browsers, so do not rely on trailing margin to create scroll space.
- Text and inline content that runs past the edge.
- Transformed elements, whose transformed position is what counts. A
translateXcan create a horizontal scrollbar. - Positioned descendants, with one exception below.
An absolutely positioned child escapes clipping by an ancestor's overflow unless that ancestor is its containing block, or is between the child and its containing block. The usual fix is to give the clipping ancestor position: relative, so the child is clipped as expected. Conversely, a dropdown menu inside an overflow: hidden ancestor that is its containing block will be cut off.
flowchart TDIgnore the line above. Use the diagram below.
Truncating text with an ellipsis
For a single line, three properties work together:
.title {
width: 12rem;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}white-space: nowrap stops wrapping so the line is wider than the box. overflow: hidden clips it. text-overflow: ellipsis replaces the clipped end with "…". Without the overflow declaration, text-overflow has no effect because there is no clipping to decorate. For multiple lines, use line-clamp, which in widely deployed browsers still needs the -webkit- prefixed form with display: -webkit-box.
Long words and wide content
Text overflows horizontally when a word cannot break. Two properties help:
.comment {
overflow-wrap: anywhere;
}overflow-wrap: break-word lets the browser break inside a word only if the word would otherwise overflow. anywhere does the same, but also lowers the minimum content size, which matters in flex and grid items. A flex item with a long word often refuses to shrink below the word's width. Setting min-width: 0 on the item, or overflow-wrap: anywhere, fixes this.
For images, the standard defence is max-width: 100% with height: auto, which applies the min and max sizing you saw earlier. For wide tables and code, wrap them in a container with overflow-x: auto so the page does not scroll sideways.
Overflow and block formatting contexts
Any overflow value other than visible or clip makes the element establish a new block formatting context. Two effects follow:
- The box contains its floated children, so it no longer collapses to zero height around them.
- The margins of its first and last children no longer collapse through its edges.
This is why overflow: hidden was long used as a "clearfix". It has side effects, though: it clips shadows, focus outlines, and anything intentionally hanging outside the box, and it makes the element a scroll container. The next chapter covers display: flow-root, which gives the same containment without clipping.
Also note that position: sticky stops working when an ancestor between the element and the scrolling viewport has overflow set to anything but visible. The sticky element then sticks relative to that nearest scroll container, which usually does not scroll.
Percentage heights and debugging
A percentage height resolves only when the parent has a definite height. If the parent is height: auto, height: 100% on a child behaves as auto, and overflow will not be triggered the way you expect.
To find the element causing a stray horizontal scrollbar, use the browser's developer tools to highlight the elements wider than the viewport, or run this temporary rule:
* {
outline: 1px solid red;
}Outlines do not affect layout, so they show the real box edges. Look for the element that extends past the right edge of the page. Common culprits are a 100vw width (which includes the scrollbar width), a fixed-width element, or a negative margin.
Common errors
- Using a fixed
heightfor a text container. Prefermin-height, so the box can grow. - Setting only
overflow-x: autoand expecting the y axis to stayvisible. - Using
overflow: hiddento hide a bug rather than finding the element that is too wide. - Using
width: 100vwon a block. It ignores the vertical scrollbar and creates a horizontal one. - Forgetting
min-width: 0on flex or grid children that contain long text or<pre>blocks.
Exercise
A div has overflow-x: auto and overflow-y: visible. Its content is taller than the div. What happens on the vertical axis, and how would you get a horizontal scrollbar with unclipped vertical content?
Show answer
Because one axis is not visible, the visible axis computes to auto, so the div clips its content vertically and shows a vertical scrollbar when the content is taller. CSS cannot scroll on one axis and let content spill out on the other with visible. You can use overflow-x: auto with overflow-y: clip only if you want clipping, or restructure the layout, for example by letting the box grow with height: auto so no vertical overflow occurs.
Exercise
This title shows no ellipsis, only wrapped text: .title { width: 10rem; text-overflow: ellipsis; }. Which two declarations are missing, and why?
Show answer
It needs white-space: nowrap and overflow: hidden. Without nowrap the text wraps to a second line instead of running past the box. Without overflow: hidden there is no clipping, so text-overflow has nothing to replace with an ellipsis.