In CSS, overflow controls what happens when the content of an element is too big to fit inside its container (like text or images spilling out of a box).
Here’s the basic idea: imagine you’ve got a div with a fixed width and height — if the content inside it goes beyond those limits, overflow decides whether to hide it, show scrollbars, or just let it spill out.
🌊 What “Overflow” Really Means:
When you set a width or height on an element, the browser draws a “box” for it (thanks to the CSS box model).
If the content inside that box — like text, images, or other elements — becomes larger than the box itself, that’s called overflow.
By default, the browser just lets that extra stuff spill out beyond the border. But with the overflow property, you take control.
Syntax:
overflow: visible | hidden | scroll | auto;
Values:
| Value |
Description |
| visible |
Default. Content is not clipped — it overflows and remains visible outside the box. |
| hidden |
The overflow is clipped, and the extra content is not visible. |
| scroll |
The overflow is clipped, but scrollbars always appear, even if not needed. |
| auto |
The browser adds scrollbars only if necessary. |
Details of overflow values:
1.Overflow:auto(Smart option)
-
Adds scrollbars only when content actually overflows.
-
Clean and user-friendly.
Example:
.box {
width: 200px;
height: 100px;
border: 2px solid #444;
overflow: auto;
}
<div class="box">
<p>This is a long paragraph that overflows the box boundaries...</p>
<div>
Output:
This is a long paragraph that overflows the box boundaries...
Here, if the text is too long, scrollbars will appear so you can scroll to see the rest.
You can also control horizontal and vertical overflow separately:
overflow-x: hidden; /* horizontal */
overflow-y: scroll; /* vertical */
2. overflow: visible; (Default)
-
The overflowing content is not clipped.
-
It stays visible outside the container.
-
This can break your layout if you’re not careful.
Example:
.box {
width: 200px;
height: 100px;
border: 2px solid red;
overflow: visible;
}
<div class="box">➡️ You’ll see text or images popping out beyond the box — messy but sometimes intentional (for artistic effects).<div>
Output:
➡️ You’ll see text or images popping out beyond the box — messy but sometimes intentional (for artistic effects).
3. overflow: hidden;
- Any content that doesn’t fit is cut off.
-
There’s no scrollbar, so you can’t access the hidden part.
-
Useful for clean layouts, image cropping, or animation clipping.
Example:
.box {
width: 200px;
height: 100px;
border: 2px solid green;
overflow: hidden;
}
<div class="box">➡️ Great when you want to hide what’s overflowing without letting users scroll.(The text is hidden.)<div>
Output:
➡️ Great when you want to hide what’s overflowing without letting users scroll.The text is hidden
4.overflow: scroll;
-
The box always shows scrollbars, whether content fits or not.
-
Gives a clear signal that scrolling is possible.
-
But it can look awkward if the scrollbars are empty.
Example:
.box {
width: 200px;
height: 100px;
border: 2px solid blue;
overflow: scroll;
}
<div class="box">➡️ Scrollbars appear even if your text fits perfectly — so use it sparingly.</div>
Output:
➡️ Scrollbars appear even if your text fits perfectly — so use it sparingly.
🎨 Real-World Uses of overflow:
-
Scrollable containers (chat boxes, sidebars, or feeds)
-
Hiding overflowing elements (image galleries, animations)
-
Custom scroll areas (with CSS scrollbars or JS libraries)
-
Clipping effects (paired with border-radius or transform)