CSS Overflow

CSS Overflow

·

2 min read

CSS Overflow

The CSS overflow property is used to specify how to handle content that's too large to fit in its container. This is a useful property when dealing with large amounts of content or dynamic sizes.

Contrast Bootstrap UI Kit

Basic Usage

There are four values that the overflow property can take:

  • visible: This is the default value. The content is not clipped, and it renders outside the element's box.

  • hidden: The content is clipped, and any content that is outside the element's box is not visible.

  • scroll: The content is clipped, but a scrollbar is added to see the rest of the content.

  • auto: Similar to scroll, but the scrollbar only appears when necessary.

Here is an example of how to use overflow:

.container {  width: 200px;  height: 200px;  overflow: auto;}

In this example, if the content inside .container exceeds 200px in either width or height, scrollbars will appear to allow users to scroll to view the content.

Overflow in X and Y Directions

CSS also provides two properties overflow-x and overflow-y that allow you to specify how overflow is handled in the horizontal (x) and vertical (y) directions respectively.

For example:

.container {  width: 200px;  height: 200px;  overflow-x: hidden;  overflow-y: auto;}

In this example, if the content inside .container exceeds 200px in width, it will be clipped and not visible. If it exceeds 200px in height, a vertical scrollbar will appear.

Using Overflow with Text

The overflow property can be particularly useful when dealing with text content. For instance, you might want to limit the display of a block of text to a certain number of lines. In combination with other properties like text-overflow and white-space, you can control how overflowed text content is displayed:

.container {  width: 200px;  height: 50px;  overflow: hidden;  text-overflow: ellipsis;  white-space: nowrap;}

In this example, if the text inside .container exceeds the width of the container, it will be cut off and an ellipsis (...) will be displayed to indicate that there is more content.

Remember, the CSS overflow property is a powerful tool for controlling how objects displayed when it overflows its container. Use it wisely to improve the usability and aesthetics of your web pages.

Contrast Bootstrap UI Kit