What is overflow in CSS
The overflow property in CSS controls what happens when content exceeds the size of its container. It determines whether content should be clipped, hidden, or scrolled.
1. What is overflow in CSS?
overflowspecifies how to handle content that doesn’t fit within an element's box.- It applies to block elements with a set height or width.
2. Why Use overflow?
- Prevent Layout Breaks – Stops overflowing content from disrupting designs.
- Scrollbars – Enable scrolling for hidden content.
- Clipping – Hide unnecessary content to improve aesthetics.
- Maintain Responsive Design – Control large content in flexible layouts.
overflow Works (Example).box {
width: 200px;
height: 100px;
overflow: hidden;
border: 1px solid #333;
}
<div class="box">
This text might overflow if it is too long to fit within the container.</div>
- Effect: Content that exceeds 200x100px will be hidden.
| Value | Description |
|---|---|
visible | Default. Content extends beyond the box and appears outside the container. |
hidden | Content that overflows is clipped and not scrollable. |
scroll | Scrollbars appear even if content fits inside the box. |
auto | Scrollbars appear only if content overflows. |
clip | Similar to hidden, but no scrolling or interaction is possible. |
1. Overflow: Visible (Default)
.box {width: 150px;height: 80px;overflow: visible;}
- Content spills out of the container if it overflows.
.box {width: 150px;height: 80px;overflow: hidden;}
- Any content outside the box is clipped and invisible.
.box {width: 150px;height: 80px;overflow: scroll;}
- Scrollbars appear even if the content fits.
.box {width: 150px;height: 80px;overflow: auto;}
- Scrollbars appear only if needed (content exceeds the box).
6. Overflow on Both Axes (overflow-x and overflow-y)
- Control horizontal and vertical overflow independently.
.box {width: 200px;height: 150px;overflow-x: scroll; (Horizontal scrolling)overflow-y: hidden; (No vertical overflow)}
- Horizontal scroll only; vertical content is clipped.
.scroll-box {width: 300px;height: 200px;border: 1px solid #000;overflow: auto;}<div class="scroll-box"><p>Content goes here...</p><p>This is additional text that may overflow, causing scrollbars to appear.</p></div>
- Effect: Scrollbars appear if the content exceeds 300x200px.
8. Overflow with Flex and Grid Layouts
- Overflow behaves differently in flex/grid containers.
.flex-container {display: flex;}.flex-item {flex: 1;overflow: hidden;}
- Flex items prevent overflow by clipping content.
.text {width: 200px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}
- Effect:
- Text that overflows is truncated with..
10. Best Practices for Using overflow
- Choose
autofor Flexibility – It adapts to content size dynamically. - Avoid
hiddenfor Important Content – Users cannot access clipped content. - Scroll for Usability – Enable scrolling for content-heavy sections.
- Use Ellipsis for Long Text – Prevent layout breaks with
text-overflow. - Test on Mobile – Overflow affects mobile usability (horizontal scrolling issues).
By mastering overflow, you can control layouts effectively, ensuring that content behaves predictably across different screen sizes and devices.