CSS Property: overflow
Like a cup of coffee that’s brimming over, the CSS overflow property manages what happens when content overflows, or spills outside, its container. Ever encountered a piece of text or image that’s too large for its designated box and it spills over? Well, that’s where our good friend overflow comes in, instructing the browser on how to handle these pesky overflow situations. This could mean showing scrollbars, cutting off the excess content, or expanding the box.
Syntax
The overflow property is quite straightforward to use. You declare it on an element, and it comes with a few possible values to control the behavior of overflow content.
Here’s a simple code snippet:
1 |
div { |
2 |
overflow: auto; |
3 |
}
|
In this example, auto tells the browser to provide a scrollbar if the content overflows the div container. It’s like having an automatically deployable rescue boat when the flood (overflow) occurs!
By default, the overflow property is set to visible, which means that overflowing content is not clipped and can render outside the box.
This property applies to block containers, flex containers, and grid containers.
The overflow property value is not inherited. So, if you want all your divs to have the same overflow behaviour, you’ll need to specify it for each one.
Finally, CSS animations cannot be applied to the overflow property. It’s a stickler for stability and doesn’t really enjoy the limelight of flashy animations!
Example
This beautiful lemon yellow div has fixed dimensions. See what happens to the content within it when you alter the overflow value: