-
Hajipur, Bihar, 844101
overflow
in CSS?In CSS, the overflow
property specifies what should happen if the content of an element overflows its defined area (e.g., if content is too large to fit in a box).
Sometimes content inside an element grows beyond its width or height. To handle this, we use overflow
to decide whether:
It should be visible
Hidden
Scrollable
Or handled automatically
selector {
overflow: value;
}
Possible values:
visible
(default)
hidden
scroll
auto
visible
(Default)This is the default behavior. Content that overflows will still be visible outside the container.
<div class="visible-box">
This is some very long content that will overflow its box but will still be visible outside.
</div>
.visible-box {
width: 200px;
height: 50px;
overflow: visible;
border: 1px solid black;
}
🟢 The content overflows and sticks out beyond the box.
hidden
This value clips the overflowing content and makes it invisible. Scrollbars are not added.
<div class="hidden-box">
This is long content that will be cut off.
</div>
.hidden-box {
width: 200px;
height: 50px;
overflow: hidden;
border: 1px solid red;
}
🟢 Any content that doesn’t fit within the 200×50 box will be hidden.
scroll
This value adds scrollbars always, regardless of whether the content overflows or not.
<div class="scroll-box">
This content is scrollable even if it doesn't overflow!
</div>
.scroll-box {
width: 200px;
height: 50px;
overflow: scroll;
border: 1px solid blue;
}
🟢 Scrollbars will always appear, even if not needed.
auto
This value automatically adds scrollbars only when needed.
<div class="auto-box">
This content will show scrollbars only if it overflows the box.
</div>
.auto-box {
width: 200px;
height: 50px;
overflow: auto;
border: 1px solid green;
}
🟢 Scrollbars appear only when content is larger than the box.
You can also control overflow separately for horizontal and vertical directions using:
overflow-x
overflow-y
.div1 {
overflow-x: scroll; /* horizontal scroll only */
overflow-y: hidden; /* vertical overflow hidden */
}
<div class="scroll-menu">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
<a href="#">Link 4</a>
</div>
.scroll-menu {
white-space: nowrap;
overflow-x: auto;
border: 1px solid #ccc;
width: 300px;
}
.scroll-menu a {
display: inline-block;
padding: 10px;
}
🟢 This creates a horizontal scroll menu.
Creating scrollable boxes
Hiding overflowing content
Adding horizontal scrolling
Responsive card layouts
Avoiding layout breaking due to long text
Q1. Create a box with overflow: hidden
.
Q2. Allow vertical scroll and hide horizontal scroll.
Q3. Create a box that always shows scrollbars.
Q4. Create a box where scrollbars appear only when needed.
Q5. Hide overflow text from a div with fixed width.
Q6. Make an image scrollable inside a container.
Q7. Prevent horizontal overflow using overflow-x
.
Q8. Create a horizontal menu that scrolls.
Q9. Hide overflow and round the corners.
Q10. Combine white-space: nowrap
and overflow-x: auto
.
Q1: What is the default value of overflow?
Q2: Which value hides the content that overflows?
Q3: Which overflow value adds scrollbars only when needed?
Q4: Which property is used to control horizontal overflow only?
Q5: What will overflow: scroll do?
Q6: What happens when you use overflow: visible?
Q7: How to create a horizontally scrollable row?
Q8: Which overflow value works best for responsive boxes?
Q9: How to ensure vertical scrolling only?
Q10: What does overflow-x: hidden do?