CSS Overflow
The CSS overflow property controls what happens when the content inside an element is larger than the element's defined width or height.
For example, imagine a box with a fixed height of 120px. If the content inside that box needs more than 120px of vertical space, the content may extend outside the box.
The overflow property tells the browser how to handle that extra content.
Basic Syntax
selector {
overflow: value;
}
Overflow Diagram
Content is larger than the container:
The container controls how the extra content is displayed.
Simple Explanation
Think of a small box containing a large amount of text.
If the text does not fit inside the box, the extra content is called overflow.
CSS provides different ways to handle this extra content.
You can allow the content to remain visible, hide it, always show scrollbars, or let the browser decide whether scrolling is necessary.
Main Overflow Values
visible
Extra content remains visible outside the element.
hidden
Extra content is clipped and is not visible outside the element.
scroll
Scrollbars are provided so the user can access the overflowing content.
auto
The browser adds scrollbars when they are required.
Example 1: overflow: visible
The visible value allows content to continue outside the element when it does not fit.
.box {
width: 300px;
height: 120px;
padding: 15px;
border: 3px solid blue;
background-color: lightblue;
overflow: visible;
}
Output
Example 2: overflow: hidden
The hidden value clips content that does not fit inside the element.
.box {
width: 300px;
height: 120px;
padding: 15px;
border: 3px solid green;
background-color: lightgreen;
overflow: hidden;
}
Output
With overflow: hidden, the content still exists in the HTML, but the part that goes outside the container is not visible.
Example 3: overflow: scroll
The scroll value adds scrollbars so the user can access content that does not fit.
.box {
width: 300px;
height: 120px;
padding: 15px;
border: 3px solid orange;
background-color: lightyellow;
overflow: scroll;
}
Output
Example 4: overflow: auto
The auto value allows the browser to add scrollbars when the content actually overflows the element.
.box {
width: 300px;
height: 120px;
padding: 15px;
border: 3px solid purple;
background-color: #e9d5ff;
overflow: auto;
}
Output
overflow-x and overflow-y
Sometimes you may want to control horizontal and vertical overflow separately.
.box {
overflow-x: auto;
overflow-y: hidden;
}
overflow-x controls content that overflows horizontally.
overflow-y controls content that overflows vertically.
This is useful when you want horizontal scrolling but do not want vertical scrolling.
Overflow Values Comparison
| Value | What Happens? | Scrollbar |
|---|---|---|
| visible | Extra content remains visible. | No automatic scrollbar. |
| hidden | Extra content is clipped. | No. |
| scroll | Extra content can be accessed by scrolling. | Yes. |
| auto | Browser decides when scrolling is required. | When required. |
Where is Overflow Used?
Long Text
Useful when a container has limited height but may contain a large amount of text.
Scrollable Panels
Useful for creating panels where users can scroll through additional content.
Images
Overflow can help control images or other elements that are larger than their containers.
Responsive Layouts
Horizontal overflow can be controlled when content does not fit smaller screens.
Summary
The CSS overflow property controls what happens when content is larger than its container. The main values are visible, hidden, scroll, and auto. You can also control horizontal and vertical overflow separately using overflow-x and overflow-y. Understanding overflow is important when creating fixed-size containers, scrollable sections, and responsive layouts.