Sub topic 11
Overflow: when content exceeds the box
This chapter assumes you can write CSS rules, know the content, padding, border, and margin layers, and understand how width, height, and box-sizing set a box's size. Here you will see what happens when content is larger than the box that holds it, and how to control it.
Why overflow happens
A box gets its size from width and height, or from its content when those are auto. When you fix a size and the content needs more room, the extra content is overflow. By default, the browser does not clip it. The content spills out past the box's padding edge and can draw over neighbouring elements, but it does not change the layout of those neighbours.
Overflow is most common when you set a fixed height. Width overflow usually comes from long unbroken strings, wide images, or tables.
<style>
.note {
width: 200px;
height: 60px;
padding: 10px;
border: 2px solid #333;
}
</style>
<div class="note">
This text is longer than the box can hold, so the extra lines spill out below the border.
</div>The overflow property
The overflow property controls what the browser does with content that does not fit. It applies to block containers with a constrained size.
| Value | Behaviour |
|---|---|
| `visible` | The default. Content spills outside the box and is not clipped. |
| `hidden` | Content outside the padding box is clipped. No scrollbars appear. |
| `scroll` | Content is clipped, and scrollbars are always shown. |
| `auto` | Content is clipped, and scrollbars appear only when needed. |
| `clip` | Content is clipped like `hidden`, but the box cannot be scrolled, even by script. |
The clipping edge is the padding box. Content can use the padding area, but it is cut off at the inner edge of the border.
.note {
width: 200px;
height: 60px;
overflow: auto;
}With overflow: auto, the user can scroll the text inside the box, and the box keeps its 60px height.
Controlling each axis
You can set each axis separately with overflow-x and overflow-y, or use two values in the shorthand: overflow: hidden auto means x is hidden and y is auto.
.table-wrapper {
max-width: 100%;
overflow-x: auto;
overflow-y: visible;
}There is a catch. If one axis is visible and the other is not, the browser computes the visible axis as auto. So the example above does not leave the y axis visible. Both axes end up clipped or scrollable. The value clip is the exception, since it can be paired with visible.
Common errors
- Fixed heights on text containers. A fixed
heightbreaks when users zoom or change font size. Prefermin-height, so the box can grow. - Hiding a layout bug.
overflow: hiddencan make a problem disappear without fixing its cause. Check which child is too wide first. - Clipped focus rings and shadows. Overflow clipping also cuts off outlines and
box-shadowon children near the edge. Add padding inside the container. - Long strings. A long URL can overflow a narrow column. Add
overflow-wrap: anywhereto let it break. - Unexpected side effects. Any value other than
visibleorclipcreates a new block formatting context. This can change how margins and floats behave, which a later chapter covers.
Exercise
A .panel has height: 100px and overflow-x: hidden, and you leave overflow-y unset. Content is 300px tall. What happens on the vertical axis, and why?
Show answer
The vertical content is not left visible. Since overflow-x is not visible, the unset overflow-y: visible is computed as auto. The panel stays 100px tall, clips the content, and shows a vertical scrollbar.
Exercise
You need a 150px-tall card whose text must never be cut off, even if the user increases the font size. Which property should you change, and to what?
Show answer
Replace height: 150px with min-height: 150px. The card then keeps at least 150px but grows as the text needs more room, so nothing overflows.