DEV Community

Cover image for Overflow in CSS
Rakshambika
Rakshambika

Posted on

Overflow in CSS

Overflow Property:

Overflow in CSS is used to control what happens when the content inside an element is larger than the element's specified width or height.

Syntax:

selector {
    overflow: value;
}
Enter fullscreen mode Exit fullscreen mode

Why do we use overflow?

Suppose a <div> has a fixed height, but the content inside it is too large. The content may go outside the boundaries of the element.

The overflow property controls whether that extra content should be:

  • Visible
  • Hidden
  • Scrollable
  • Automatically scrollable when necessary

Types of Overflow:

overflow: visible

This is the default value.

The content that exceeds the element's boundary will still be visible.

.box {
    width: 200px;
    height: 100px;
    overflow: visible;
}
Enter fullscreen mode Exit fullscreen mode

overflow: hidden

The content that exceeds the element's boundary will be hidden.

.box {
    width: 200px;
    height: 100px;
    overflow: hidden;
}
Enter fullscreen mode Exit fullscreen mode

overflow: scroll

This always displays scrollbars, even when the content does not necessarily need scrolling.

.box {
    width: 200px;
    height: 100px;
    overflow: scroll;
}
Enter fullscreen mode Exit fullscreen mode

overflow: auto

The browser automatically decides whether scrollbars are required.

.box {
    width: 200px;
    height: 100px;
    overflow: auto;
}
Enter fullscreen mode Exit fullscreen mode

This is commonly preferred when you don't want unnecessary scrollbars.


Top comments (1)

Collapse
 
rajasekar1998 profile image
Raj

correct