The overflow property in CSS controls what happens when content is too large to fit inside its box. You can choose to:
scroll or auto.overflow-x) and y-axis (overflow-y) separately.width and/or height.Basic syntax to control overflow on both axes:
/* Basic overflow shorthand */
selector {
overflow: visible; /* visible | hidden | scroll | auto */
}
/* Axis-specific control */
selector {
overflow-x: auto; /* horizontal overflow */
overflow-y: hidden; /* vertical overflow */
}
The overflow property can take these main values:
visible — Default; extra content is visible outside the box.hidden — Extra content is clipped and not visible.scroll — Content is clipped, and scrollbars are always shown.auto — Scrollbars appear only if content actually overflows.Here is a set of boxes with different overflow values applied:
/* Different overflow behaviors for demo boxes */
.container-visible {
overflow: visible;
width: 200px;
height: 100px;
background-color: lightblue;
}
.container-hidden {
overflow: hidden;
width: 200px;
height: 100px;
background-color: lightgreen;
}
.container-scroll {
overflow: scroll;
width: 200px;
height: 100px;
background-color: lightcoral;
}
.container-auto {
overflow: auto;
width: 200px;
height: 100px;
background-color: lightyellow;
}
This HTML uses the above classes to show how content behaves when it overflows:
<!-- Overflow demo containers with long content -->
<div class="container-visible">
This is some very long text that will overflow the box
and remain fully visible outside its boundaries.
</div>
<div class="container-hidden">
This is some very long text that will overflow the box,
but anything that does not fit will be clipped.
</div>
<div class="container-scroll">
This is some very long text that will overflow the box,
and scrollbars will always be visible for scrolling.
</div>
<div class="container-auto">
This is some very long text that will overflow the box,
and scrollbars will appear only if needed.
</div>
Imagine the four boxes rendered one below another (each 200px wide and 100px tall) with long text inside:
width and/or height when expecting overflow behavior.overflow: auto instead of scroll to avoid always-visible scrollbars.overflow-x or overflow-y when you only need scrolling on one axis.<div> with a fixed height of 80px and experiment with overflow: visible, hidden, scroll, and auto.overflow-y: auto and add multiple message lines to test scrolling.white-space: nowrap; and overflow-x: scroll; to create a sideways scroller.overflow-x vs overflow-y affects scrollbars.