CSS overflow Property

CSS overflow property specifies how the overflowing content should be handled within the element, whether it should be clipped, displayed normally or scrollbars should be added to fit the content within the element’s boundary.

In the example below, we have used an image inside the <div> element, which is larger than the <div> itself. Therefore, the image flows out the <div> boundaries. Now, how that overflowing image content should be displayed that we have controlled using the overflow property. Try it out:

Example:

.div1{
  overflow: visible;
}
.div2{
  overflow: hidden;
}
.div3{
  overflow: scroll;
}
.div4{
  overflow: auto;
}

It is recommended to use overflow: auto; instead of using overflow: scroll;. Because overflow: auto; adds scroll bars only if it’s required. While overflow: scroll; adds scroll bar even if it is not required. See the example below:

Example:

.div1{
  overflow: scroll;
}
.div2{
  overflow: auto;
}

CSS Syntax

The overflow property has the below syntax:

overflow: visible|hidden|scroll|auto|initial|inherit;

Property Values

The overflow property accepts the following values:

visibleThe overflowing content is displayed normally. This is the default value.
hiddenThe overflowing content is clipped completely.
scrollThe overflowing content is forced to display inside the containing element’s boundary by adding horizontal and vertical scrolls. It adds scroll bars even if they are not required.
autoIt is almost similar to overflow: scroll;. The only difference is that it adds scroll bars only when required.
initialSets the overflow property to its default value(visible).
inheritInherits the overflow property from the parent element.

General Info

Default Valuevisible
InheritedNo
JavaScript Usageelement.style.overflow = “auto”;

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

Leave a Comment