Sign in
Log inSign up

Overflowing content in CSS

Satish Maurya's photo
Satish Maurya
·Aug 30, 2021·

2 min read

what is overflow?

Everything in CSS is a box. You can constrain the size of these boxes by assigning values of width and height (or inline-size and block-size). Overflow happens when there is too much content to fit in a box.

CSS tries to avoid "data loss"

image.png

  • You might wonder why CSS works in such a messy way, displaying content outside of its intended container. Why not hide overflowing content? Why not scale the size of the container to fit all the content?

  • Wherever possible, CSS does not hide content. This would cause data loss.

  • The problem with data loss is that you might not notice.

  • Website visitors may not notice.

  • If the submit button on a form disappears and no one can complete the form, this could be a big problem! Instead, CSS overflows in visible ways.

  • You are more likely to see there is a problem. At worst, a site visitor will let you know that content is overlapping.

The overflow property

  • The overflow property is how you take control of an element's overflow.

  • It is the way you instruct the browser how it should behave.

  • The default value of overflow is visible. With this default, we can see content when it overflows.

  • To crop content when it overflows, you can set overflow: hidden. This does exactly what it says: it hides overflow. Beware that this can make some content invisible. You should only do this if hiding content won't cause problems.

Different values of overflow in CSS

1). overflow: visible (by default)

image.png

2). overflow: hidden

image.png

3). overflow: scroll

image.png

4). overflow-y: scroll.

image.png

5). overflow-x: scroll.

image.png

6). overflow: auto.

This allows the browser to determine if it should display scrollbars. According to the content available in the box.

image.png