What is Overflow in CSS?
- Siddharth Sharma
- Mar 2, 2025
- 2 min read
In CSS, overflow refers to the behavior of content that exceeds the boundaries of its container. When the content inside an element is too large to fit within the defined dimensions (width and height), the overflow property determines how the extra content should be handled.
The overflow Property
The overflow property is used to control how content is displayed when it overflows its container. It can take the following values:
visible (default):
Content is not clipped and will be visible outside the container.
This can cause the content to overlap other elements on the page.
css
.container { overflow: visible; }
hidden:
Content that overflows is clipped and hidden.
No scrollbars are provided, and users cannot see the overflowed content.
css
.container { overflow: hidden; }
scroll:
Scrollbars are always added to the container, regardless of whether the content overflows or not.
Users can scroll to see the hidden content.
css
.container { overflow: scroll; }
auto:
Scrollbars are added only when necessary (i.e., when content overflows).
This is the most commonly used value for responsive designs.
css
.container { overflow: auto; }
clip:
Similar to hidden, but it also disables programmatic scrolling (e.g., via JavaScript).
Introduced in CSS3.
css
.container { overflow: clip; }
overlay (deprecated):
Scrollbars are displayed on top of the content instead of taking up space.
This is no longer recommended and is not widely supported.
Overflow in Specific Directions
CSS also provides properties to control overflow in specific directions:
overflow-x: Controls horizontal overflow (left and right).
overflow-y: Controls vertical overflow (top and bottom).
Example:
css
.container {
overflow-x: hidden; /* Hide horizontal overflow */
overflow-y: scroll; /* Add vertical scrollbar */
}Common Use Cases for Overflow
Scrollable Containers:
Use overflow: auto or overflow: scroll to create scrollable areas for long content, such as tables or lists.
Hiding Overflow:
Use overflow: hidden to hide content that exceeds the container, such as images or text.
Responsive Design:
Use overflow: auto to ensure scrollbars appear only when needed, improving usability on different screen sizes.
Custom Scrollbars:
Combine overflow: scroll with custom scrollbar styling (using ::-webkit-scrollbar in WebKit browsers).
Example: Overflow in Action
html
<div class="container">
This is a long piece of text that will overflow the container.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>css
.container {
width: 200px;
height: 100px;
border: 1px solid black;
overflow: auto; /* Scrollbars appear only when needed */
}Potential Issues with Overflow
Unintended Layout Shifts:
Scrollbars appearing/disappearing can cause layout shifts, especially in responsive designs.
Accessibility Concerns:
Hidden content (overflow: hidden) may not be accessible to screen readers or keyboard users.
Performance:
Excessive use of scrollable areas can impact performance, especially on low-end devices.
Best Practices
Use overflow: auto instead of overflow: scroll to avoid unnecessary scrollbars.
Test overflow behavior on different screen sizes and devices.
Ensure hidden content is still accessible to all users.
By understanding and using the overflow property effectively, you can create more flexible and user-friendly layouts in CSS.




Comments