The overflow property in CSS specifies what should happen if content overflows an element's box. It helps control the behavior of content that exceeds the container's bounds.
Understanding the different values of the overflow property can greatly enhance your ability to manage overflowing content.
overflow: visible; means that the overflow is not clipped. The content will be rendered outside the element's box.
.overflow-visible {
width: 200px;
height: 100px;
background-color: lightblue;
overflow: visible;
border: 1px solid black;
}
overflow: hidden; means that the overflow is clipped, and the rest of the content will be invisible.
.overflow-hidden {
width: 200px;
height: 100px;
background-color: lightcoral;
overflow: hidden;
border: 1px solid black;
}
overflow: scroll; means that the overflow is clipped, and a scrollbar is added to see the rest of the content.
.overflow-scroll {
width: 200px;
height: 100px;
background-color: lightgreen;
overflow: scroll;
border: 1px solid black;
}
overflow: auto; means that the overflow is clipped, but a scrollbar is added only when necessary.
.overflow-auto {
width: 200px;
height: 100px;
background-color: lightpink;
overflow: auto;
border: 1px solid black;
}
You can control the overflow for each axis separately using overflow-x and overflow-y.
overflow-x: visible; overflow-y: hidden; will make the content visible horizontally but clipped vertically.
.overflow-x-visible-y-hidden {
width: 200px;
height: 100px;
background-color: lightyellow;
overflow-x: visible;
overflow-y: hidden;
border: 1px solid black;
}
overflow-x: hidden; overflow-y: scroll; will clip the content horizontally but add a scrollbar for vertical overflow.
.overflow-x-hidden-y-scroll {
width: 200px;
height: 100px;
background-color: lightgray;
overflow-x: hidden;
overflow-y: scroll;
border: 1px solid black;
}
overflow-x: auto; overflow-y: auto; will add scrollbars for both axes only when necessary.
.overflow-x-auto-y-auto {
width: 200px;
height: 100px;
background-color: lightgoldenrodyellow;
overflow-x: auto;
overflow-y: auto;
border: 1px solid black;
}
overflow: clip; will clip the overflow, making the overflowed content inaccessible.
.overflow-clip {
width: 200px;
height: 100px;
background-color: lightblue;
overflow: clip;
border: 1px solid black;
}