Live editor for all the examples will be added soon.
The CSS overflow
property allows you to control how content that exceeds the boundaries of its container is handled. It determines whether the overflowed content is clipped, hidden, or made scrollable. Understanding how to use the overflow
property is essential when dealing with layouts, text, images, or any content that may extend beyond the dimensions of its container.
In this guide, we will cover how to use overflow
to manage content that goes beyond the bounds of its container, focusing on the values: scroll
, hidden
, and auto
. We’ll also explore practical use cases for each behavior.
1. What is the CSS overflow
Property?
The overflow
property in CSS determines how content that exceeds the size of an element’s container is displayed. You can control the overflow behavior for both the horizontal (x-axis) and vertical (y-axis) directions.
Syntax:
element {
overflow: value;
}
Common Values for overflow
:
visible
: The default value. Content is not clipped and may overflow beyond the container’s boundaries.hidden
: The overflowed content is clipped, and anything outside the container is not visible.scroll
: Adds scrollbars to the container, allowing users to scroll through the overflowed content.auto
: Adds scrollbars only if the content overflows (if necessary).
2. Using overflow: hidden
The overflow: hidden
property clips any content that overflows beyond the container’s boundaries, effectively hiding the content. This is useful when you want to ensure that no extra content is visible beyond a certain area.
Syntax:
element {
overflow: hidden;
}
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Overflow Hidden Example</title>
<style>
.box {
width: 200px;
height: 100px;
background-color: lightblue;
overflow: hidden;
}
</style>
</head>
<body>
<div class="box">
This is some long text that will overflow the container and get hidden.
</div>
</body>
</html>
Explanation:
- The
.box
has a fixed width and height of 200px and 100px, respectively. - The text inside the box is longer than the container size, but since
overflow: hidden;
is applied, any content that exceeds the box size will be clipped (hidden).
Use Case:
overflow: hidden
is commonly used when you need to prevent elements like images or long text from spilling out of their containers. It’s also useful for creating cropped image galleries or text boxes.
3. Using overflow: scroll
The overflow: scroll
property forces scrollbars to appear on the container, regardless of whether the content overflows or not. This allows users to manually scroll through the content inside the container.
Syntax:
element {
overflow: scroll;
}
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Overflow Scroll Example</title>
<style>
.box {
width: 200px;
height: 100px;
background-color: lightgreen;
overflow: scroll;
}
</style>
</head>
<body>
<div class="box">
This is a lot of text that will overflow the container. Scrollbars will always appear even if there's enough space for the content.
</div>
</body>
</html>
Explanation:
- In this example, the container has a fixed width and height. Since
overflow: scroll;
is applied, scrollbars will always be present, even if there is no content overflow. - Users can manually scroll through the text inside the container.
Use Case:
overflow: scroll
is useful when you want to ensure that the user can always scroll through the content, regardless of whether it overflows or not. This is often used for text boxes, divs with large amounts of content, or content that may be loaded dynamically.
4. Using overflow: auto
The overflow: auto
property adds scrollbars only if the content overflows. If the content fits within the container, no scrollbars are displayed. This is a more dynamic approach to overflow handling compared to scroll
.
Syntax:
element {
overflow: auto;
}
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Overflow Auto Example</title>
<style>
.box {
width: 200px;
height: 100px;
background-color: lightcoral;
overflow: auto;
}
</style>
</head>
<body>
<div class="box">
This is some long text that will cause the container to overflow. Scrollbars will appear only if the content exceeds the size of the container.
</div>
</body>
</html>
Explanation:
- The
.box
container has a fixed size. If the content inside overflows, scrollbars will appear automatically due tooverflow: auto;
. - If the content fits within the container, no scrollbars are shown.
Use Case:
overflow: auto
is useful for creating responsive layouts where you want scrollbars to appear only when the content exceeds the container’s boundaries. It is a preferred choice when you want to handle dynamic content that may or may not overflow.
5. Controlling Horizontal and Vertical Overflow Separately
CSS provides properties to control horizontal and vertical overflow independently using overflow-x
and overflow-y
.
5.1 overflow-x
The overflow-x
property controls the horizontal overflow (left and right scrolling). It can take the same values as the overflow
property: hidden
, scroll
, or auto
.
Example:
element {
overflow-x: scroll;
}
5.2 overflow-y
The overflow-y
property controls the vertical overflow (top and bottom scrolling).
Example:
element {
overflow-y: hidden;
}
Example of Combining overflow-x
and overflow-y
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Overflow X and Y Example</title>
<style>
.box {
width: 200px;
height: 100px;
background-color: lightgray;
overflow-x: scroll;
overflow-y: hidden;
}
</style>
</head>
<body>
<div class="box">
This is a long line of text that will overflow horizontally. A scrollbar will appear for horizontal overflow, but not for vertical overflow.
</div>
</body>
</html>
Explanation:
- In this example, horizontal scrollbars will appear because
overflow-x: scroll;
is applied, but vertical scrollbars are hidden withoverflow-y: hidden;
. - This is useful when you need to scroll in one direction only (e.g., for a horizontal image gallery or horizontal scrolling content).
Use Case:
- Controlling horizontal and vertical overflow separately is helpful in cases where you want scrollbars in only one direction, such as horizontal sliders or fixed-height containers.
6. The overflow: visible
(Default Behavior)
The overflow: visible
property is the default behavior for most elements. It allows content to overflow beyond the boundaries of its container, meaning that content outside the defined box will still be displayed.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Overflow Visible Example</title>
<style>
.box {
width: 200px;
height: 100px;
background-color: lightyellow;
overflow: visible;
}
</style>
</head>
<body>
<div class="box">
This text will overflow beyond the container, and all the content will still be visible.
</div>
</body>
</html>
Explanation:
- Even though the
.box
has fixed dimensions, any overflowed content will remain visible because ofoverflow: visible;
. - This behavior is not ideal when dealing with layout constraints, but it is the default for most block elements.
Use Case:
overflow: visible
is rarely used explicitly but is helpful in situations where content should not be clipped or hidden, such as decorative elements that extend beyond their containers.
7. Practical Use Cases for Each Overflow Behavior
7.1 Overflow Hidden for Image Cropping
<div style
="width: 200px; height: 200px; overflow: hidden;">
<img src="large-image.jpg" alt="Large Image" style="width: 300px; height: 300px;">
</div>
- Use Case:
overflow: hidden
is useful for cropping large images or other content that you want to fit inside a container with fixed dimensions.
7.2 Overflow Scroll for Content Boxes
<div style="width: 300px; height: 200px; overflow: scroll; border: 1px solid black;">
<p>This is a long paragraph that will overflow. You can scroll through the content to read more.</p>
</div>
- Use Case:
overflow: scroll
is perfect for content boxes that need to contain long text or other content that users may want to scroll through manually.
7.3 Overflow Auto for Responsive Content
<div style="width: 300px; height: 150px; overflow: auto; border: 1px solid gray;">
<p>This content will trigger a scrollbar only if it exceeds the container size.</p>
</div>
- Use Case:
overflow: auto
is commonly used for creating responsive layouts where scrollbars only appear when necessary, maintaining a clean appearance.
8. Overflow with position: absolute
and position: fixed
The overflow
property works in tandem with CSS positioning. When using position: absolute
or position: fixed
, the overflow
property ensures that any content extending beyond the positioned container can be managed appropriately.
Example:
<div style="width: 200px; height: 200px; overflow: auto; position: relative;">
<div style="position: absolute; top: 50px; left: 50px; width: 300px; height: 300px; background-color: coral;">
Overflowing absolutely positioned element
</div>
</div>
In this example, the overflow: auto;
will control the overflow of the absolutely positioned child element, ensuring scrollbars appear only when the content exceeds the container.
Conclusion
The CSS overflow
property is a crucial tool for controlling how content behaves when it exceeds its container. By using values like hidden
, scroll
, and auto
, you can manage the visibility of overflowed content and provide better user experience with scrollable areas when necessary.
overflow: hidden
is great for cropping content or images.overflow: scroll
ensures scrollbars are always present, regardless of content overflow.overflow: auto
adds scrollbars dynamically, only when content exceeds the container’s size.
Understanding and mastering the overflow
property allows you to build layouts that handle content overflow gracefully, keeping your designs clean, user-friendly, and functional across different devices and screen sizes.