The CSS overflow property controls what happens to content that is too large to fit inside an element's designated box. It helps manage scrolling and the visibility of extra content.
visible, hidden, scroll, and auto.Syntax:
element {
overflow: visible | hidden | scroll | auto;
}
The overflow property can take the following values:
| Value | Description |
|---|---|
visible |
Default. The content is not clipped and is visible outside the element's box. |
hidden |
The overflow is clipped, and the rest of the content becomes invisible. No scrollbar is added. |
scroll |
The overflow is clipped, but a scrollbar is always added (both horizontally and vertically) to see the rest of the content. |
auto |
Similar to scroll, but it only adds a scrollbar when it is necessary (i.e., when the content actually overflows). |
Let's explore how each property works using practical examples. Note that for the overflow property to have an effect, the element must have a restricted height or width.
The overflow: auto; property automatically adds a scrollbar only when the content is larger than the box. This is generally the most preferred way to handle overflow.
<!DOCTYPE html>
<html>
<head>
<style>
.auto-box {
width: 200px;
height: 60px;
border: 2px solid navy;
background-color: lightgray;
padding: 5px;
overflow: auto;
}
</style>
</head>
<body>
<h3>IntricateDevo: Overflow Auto</h3>
<div class="auto-box">
This is an example of CSS overflow. When the content inside the box is too long to fit within the fixed height of 60px, a scrollbar automatically appears to let you see the rest of the text.
</div>
</body>
</html>
With overflow: visible;, the content spills out of the element's box and overlaps whatever is beneath it. This is the default behavior in CSS.
<!DOCTYPE html>
<html>
<head>
<style>
.visible-box {
width: 150px;
height: 60px;
border: 2px solid navy;
background-color: lightblue;
padding: 5px;
overflow: visible;
margin-bottom: 60px; /* Added margin so overlapping is visible */
}
</style>
</head>
<body>
<h3>IntricateDevo: Overflow Visible</h3>
<div class="visible-box">
The CSS overflow controls big content. It tells whether to clip content or to add scroll bars. Since this is visible, I will spill out of the navy box!
</div>
</body>
</html>
Setting overflow: scroll; forces a scrollbar to appear, regardless of whether the content actually overflows or not. This keeps the layout consistent but can look messy if scrollbars aren't needed.
<!DOCTYPE html>
<html>
<head>
<style>
.scroll-box {
width: 150px;
height: 80px;
border: 2px solid brown;
background-color: lightpink;
padding: 5px;
overflow: scroll;
}
</style>
</head>
<body>
<h3>IntricateDevo: Overflow Scroll</h3>
<div class="scroll-box">
The CSS overflow controls big content. Scrollbars are always visible here!
</div>
</body>
</html>
The overflow: hidden; property simply cuts off any content that exceeds the element's dimensions. The extra content is completely hidden from the user, and no scrollbars are provided.
<!DOCTYPE html>
<html>
<head>
<style>
.hidden-box {
width: 150px;
height: 60px;
border: 2px solid red;
background-color: lightcoral;
padding: 5px;
overflow: hidden;
}
</style>
</head>
<body>
<h3>IntricateDevo: Overflow Hidden</h3>
<div class="hidden-box">
The CSS overflow controls big content. You will not be able to read the rest of this sentence because it is hidden.
</div>
</body>
</html>
overflow-x and overflow-yInstead of applying overflow to both directions simultaneously, CSS allows you to manage horizontal and vertical overflows independently:
overflow-x: Deals with the horizontal edges (left and right).overflow-y: Deals with the vertical edges (top and bottom).
<!DOCTYPE html>
<html>
<head>
<style>
.directional-box {
width: 150px;
height: 60px;
border: 2px solid purple;
background-color: #f3e5f5;
padding: 5px;
/* Hide horizontally, scroll vertically */
overflow-x: hidden;
overflow-y: scroll;
}
</style>
</head>
<body>
<h3>IntricateDevo: Overflow X and Y</h3>
<div class="directional-box">
This box hides content horizontally but allows you to scroll vertically to see the rest of the text.
</div>
</body>
</html>
Which CSS overflow value adds a scrollbar ONLY if the content is too large to fit in the box?