
How to Build a Horizontal Navigation Menu with HTML and CSS
12/10/2019
How to Remove the Gap Between Inline-Block Elements
12/10/2019
Overflow occurs when content extends beyond its box. A long word, a wide table, or a box with a fixed height can all cause it. Decide how the overflow should behave before choosing a value.
15. The CSS overflow property
15.1. Overflow values
| Value | What happens |
|---|---|
visible | Content may extend beyond the box. This is the initial value. |
hidden | Content is clipped. No scrollbar is provided, but the box can still be scrolled programmatically. |
scroll | The element is a scroll container, and scrollbars are requested even if there is no overflow. The browser and operating system decide whether bars are shown persistently. |
auto | The box lets the user scroll when content overflows. |
clip | Overflow is clipped and the element does not become a scroll container. |
Create a box with a fixed width and height, add more text than will fit, and try each value to see what changes. You can also use a maximum height to make a box scroll only when it gets too tall.
.panel {
max-height: 12rem;
overflow: auto;
}Compare overflow values in the original CodePen example
15.2. Controlling each axis
overflow-x controls horizontal overflow and overflow-y controls vertical overflow. With two values in the shorthand, the first is horizontal and the second is vertical.
15.2.1. Horizontal overflow
The same values apply on the horizontal axis. In this example you can scroll sideways to see content wider than 200px. For a wide table on a narrow screen, a wrapper with overflow-x: auto lets readers scroll when needed.
.container {
width: 200px;
height: 100px;
overflow-x: scroll;
}15.2.2. Vertical overflow
Use auto when all the content must remain available, or choose hidden for purely decorative content you intentionally want to crop.
.container {
width: 300px;
height: 150px;
overflow-y: hidden;
}The first rule hides content below 150px without displaying a vertical scrollbar. The next example combines the two axes. It lets readers scroll horizontally when needed and requests vertical scrolling even when the content fits.
.container {
width: 400px;
height: 200px;
overflow-x: auto;
overflow-y: scroll;
}The axes interact. For example, when one axis is auto, an explicit visible on the other axis computes to auto too. Test both directions with real content.
Quick check
Which value lets readers scroll only when a box has too much content?
auto. It preserves access to the overflowing content without requesting a permanent scrollbar.