CSS Overflow

The "Overflow" is the CSS property that we will understand in this article.

The CSS overflow property specifies how to handle the content when it overflows its block level container.

Every element on a page is a rectangular box, and the size, positioning, and behavior of these boxes are controlled via CSS.

For example, if you don't set the height of the box, it will grow as large as the content. But if you set a specific height or width of the box, then the content may or may not fit inside the container. If the content fits in the container, then it is good, but if it does not fit, then the CSS overflow property is used to overcome this problem, which specifies whether to clip the content, render a scroll bar, or display the content.

CSS Overflow Property Values

There are various values that the CSS overflow property can take. The values are described below in the table:

ValueDescription
visibleIt specifies that overflow is not clipped and renders the element outside its box. It is the default value of the CSS overflow property.
hiddenIt specifies that the overflow is clipped, and rest of the content will be invisible.
scrollIt specifies that the overflow is clipped, and a scroll bar is used to see the rest of the content.
ClipIt specifies that if overflow is clipped, then the rest of the content will be clipped.
autoIt specifies that if overflow is clipped, a scroll bar is needed to see the rest of the content.
inheritIt inherits the property from its parent element.
initialIt is used to set the property to its initial value.

Illustrations of the CSS Overflow Property

We will comprehend the CSS overflow property with the help of illustrations.

Illustration 1:

  • We will be utilizing the overflow CSS property on the <p> element in this illustration.
  • We will specify the value of the CSS overflow property to be "visible".

Code:

Output:

Here is the output where we can witness that the text written under the <p> element is overflowed. Because we have set the overflow CSS property to "visible" that is why the content is visible outside the container.

css overflow

Illustration 2:

We will utilize the CSS overflow property to the <div> element in this illustration, and we will place the value of the CSS overflow property to "hidden".

Code:

Output:

Here, in the output, we can witness that the text written inside the <div> element has been hidden when the content overflows the boundaries of a container.

css overflow

Illustration 3:

  • We will utilize the CSS overflow property on the <div> element in this illustration.
  • We will be putting the value of the CSS overflow property to "scroll".

Code:

Output:

Here is the outcome in which we can witness that when the text written under the <div> element overflows, then the vertical and horizontal scroll bar emerges, which the user can scroll to read the whole content.

css overflow

Illustration 4:

  • We will make the utilization of the CSS overflow property on <p> elements in this illustration.
  • We will be putting the value of the CSS overflow property to "auto".

Code:

Output:

Here is the outcome in which we can witness that when the width of paragraph-1 is set to 125px, and the text written inside paragraph-1 overflows, then the vertical scroll bar emerges, but when the width of paragraph-2 is set to 150px, then the content inside the paragraph-2 fits in the container.

css overflow

Illustration 5:

  • We will utilize the CSS overflow property on the <p> element in this illustration.
  • We will set the value of the CSS overflow property to "clip".

Code:

Output:

Here is the result in which we can witness that when the content under the <p> element exceeds the container, then the content is clipped.

css overflow

Illustration 6:

  • We are going to utilize the CSS overflow property on the HTML elements in this illustration.
  • We will specify the value of the CSS overflow property to "initial" and "inherit".

Code:

Output:

Here is the outcome in which we can witness that when the text written inside the first paragraph and second paragraph overflows, then the exceeded content is visible.

css overflow

Browser Compatibility

Following are the browsers that support the CSS overflow property:

  • Google Chrome
  • Safari
  • Opera
  • Firefox
  • Microsoft Edge

Conclusion

We have understood the CSS overflow property in this article. We have comprehended that the CSS overflow property can take various values such as visible, hidden, scroll, auto, clip, initial and inherit. These values help to handle the content that has overflowed the container.


Next TopicCSS Padding




Latest Courses